-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
41 lines (31 loc) · 1.01 KB
/
part1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from pathlib import Path
def read_value(intcode: list[int], idx: int) -> int:
return intcode[idx]
def write_value(intcode: list[int], idx: int, value: int) -> None:
intcode[idx] = value
intcode = [int(c) for c in Path(Path(__file__).parent, "input").read_text().split(",")]
ip = 0
intcode[1] = 12
intcode[2] = 2
while True:
match intcode[ip]:
case 99:
break
case 1:
write_value(
intcode,
read_value(intcode, ip + 3),
read_value(intcode, read_value(intcode, ip + 1))
+ read_value(intcode, read_value(intcode, ip + 2)),
)
case 2:
write_value(
intcode,
read_value(intcode, ip + 3),
read_value(intcode, read_value(intcode, ip + 1))
* read_value(intcode, read_value(intcode, ip + 2)),
)
case _:
raise ValueError(f"Unknown opcode: {intcode[ip]}")
ip += 4
print(f"Result: {intcode[0]}")