70 lines
1.6 KiB
Python
70 lines
1.6 KiB
Python
from collections import defaultdict
|
|
|
|
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
from tools.math import get_factors
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(4225, "input23")
|
|
],
|
|
[
|
|
(905, "input23")
|
|
]
|
|
]
|
|
|
|
def get_commands(self) -> list:
|
|
return [x.split(" ") for x in self.getInput()]
|
|
|
|
def part1(self) -> Any:
|
|
register = defaultdict(int)
|
|
index = 0
|
|
commands = self.get_commands()
|
|
mul_count = 0
|
|
while 0 <= index < len(commands):
|
|
cmd = commands[index]
|
|
|
|
try:
|
|
value = int(cmd[2])
|
|
except ValueError:
|
|
value = register[cmd[2]]
|
|
|
|
match cmd[0]:
|
|
case "set":
|
|
register[cmd[1]] = value
|
|
case "sub":
|
|
register[cmd[1]] -= value
|
|
case "mul":
|
|
mul_count += 1
|
|
register[cmd[1]] *= value
|
|
case "jnz":
|
|
try:
|
|
jumper = int(cmd[1])
|
|
except ValueError:
|
|
jumper = register[cmd[1]]
|
|
if jumper != 0:
|
|
index += value - 1
|
|
|
|
index += 1
|
|
|
|
return mul_count
|
|
|
|
def part2(self) -> Any:
|
|
b = int(self.getInput()[0].split(" ")[2]) * 100 + 100000
|
|
c = b + 17000
|
|
h = 0
|
|
while b <= c:
|
|
if len(get_factors(b)) > 2:
|
|
h += 1
|
|
b += 17
|
|
|
|
return h
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2017, 23)
|
|
day.run(verbose=True)
|