Day 10 part 1 completed
This commit is contained in:
parent
c9d83911ac
commit
c9e877e98b
19
day10/common.py
Normal file
19
day10/common.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from typing import TypeAlias
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class Operation(Enum):
|
||||||
|
NOOP = 'noop'
|
||||||
|
ADDX = 'addx'
|
||||||
|
|
||||||
|
Instruction: TypeAlias = tuple[Operation, int|None]
|
||||||
|
|
||||||
|
def parse(filename: str) -> list[Instruction]:
|
||||||
|
instructions = []
|
||||||
|
with open(filename) as f:
|
||||||
|
while line:=f.readline().strip("\n"):
|
||||||
|
oper = line.split()
|
||||||
|
if len(oper) > 1:
|
||||||
|
instructions.append((Operation(oper[0]), int(oper[1])))
|
||||||
|
else:
|
||||||
|
instructions.append((Operation(oper[0]), None))
|
||||||
|
return instructions
|
33
day10/part1.py
Executable file
33
day10/part1.py
Executable file
@ -0,0 +1,33 @@
|
|||||||
|
#!/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()
|
Loading…
x
Reference in New Issue
Block a user