34 lines
899 B
Python
Executable File
34 lines
899 B
Python
Executable File
#!/usr/bin/env python3
|
|
from common import parse, Operation, Instruction
|
|
|
|
def tick(instruction: Instruction, clock: int, x: int) -> tuple[int,int]:
|
|
match instruction[0]:
|
|
case Operation.NOOP:
|
|
return clock+1, x
|
|
case Operation.ADDX:
|
|
return clock+2, x+ instruction[1]
|
|
|
|
def solve(input: list[Instruction])->int:
|
|
clock = 0
|
|
x = 1
|
|
current_x = x
|
|
important_ticks = [20,60,100,140,180,220]
|
|
signal_strengths = []
|
|
for instruction in input:
|
|
clock, x = tick(instruction, clock, x)
|
|
if important_ticks != [] and clock >= important_ticks[0]:
|
|
print(important_ticks[0], current_x)
|
|
signal_strengths.append(current_x * important_ticks.pop(0))
|
|
current_x = x
|
|
print(signal_strengths)
|
|
return sum(signal_strengths)
|
|
|
|
|
|
|
|
def main():
|
|
print(solve(parse("input")))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|