-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2015_23.py
76 lines (56 loc) 路 1.96 KB
/
AoC2015_23.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/env python3
#
# Advent of Code 2015 Day 23
#
from typing import NamedTuple
from aoc import my_aocd
from aoc.vm import Program, Instruction, VirtualMachine
class Instruction_(NamedTuple):
operation: str
operands: tuple[str]
def _parse(inputs: tuple[str]) -> list[Instruction_]:
return [Instruction_(input_[:3], input_[4:].split(", "))
for input_ in inputs]
def _build_program(inss: list[Instruction_]) -> Program:
def translate_instruction(ins: Instruction_) -> Instruction:
if ins.operation == "hlf":
return Instruction.DIV(ins.operands[0], 2)
elif ins.operation == "tpl":
return Instruction.MUL(ins.operands[0], "3")
elif ins.operation == "inc":
return Instruction.ADD(ins.operands[0], 1)
elif ins.operation == "jmp":
return Instruction.JMP(int(ins.operands[0]))
elif ins.operation == "jie":
return Instruction.JIE(ins.operands[0], int(ins.operands[1]))
elif ins.operation == "jio":
return Instruction.JI1(ins.operands[0], int(ins.operands[1]))
else:
raise ValueError("Invalid instruction")
return Program([translate_instruction(ins) for ins in inss])
def part_1(inputs: tuple[str]) -> int:
program = _build_program(_parse(inputs))
VirtualMachine().run_program(program)
return program.registers["b"]
def part_2(inputs: tuple[str]) -> int:
program = _build_program(_parse(inputs))
program.instructions.insert(0, Instruction.SET("a", 1))
VirtualMachine().run_program(program)
return program.registers["b"]
TEST = """\
inc b
jio a, +2
tpl b
inc b
""".splitlines()
def main() -> None:
my_aocd.print_header(2015, 23)
assert part_1(TEST) == 4
assert part_2(TEST) == 2
inputs = my_aocd.get_input(2015, 23, 47)
result1 = part_1(inputs)
print(f"Part 1: {result1}")
result2 = part_2(inputs)
print(f"Part 2: {result2}")
if __name__ == '__main__':
main()