From c9e877e98ba69c6ff11ea99e2c3aecbce7704ec1 Mon Sep 17 00:00:00 2001 From: Fedaya Date: Mon, 2 Dec 2024 14:57:20 +0100 Subject: [PATCH] Day 10 part 1 completed --- day10/common.py | 19 +++++++++++++++++++ day10/part1.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 day10/common.py create mode 100755 day10/part1.py diff --git a/day10/common.py b/day10/common.py new file mode 100644 index 0000000..bacea7c --- /dev/null +++ b/day10/common.py @@ -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 diff --git a/day10/part1.py b/day10/part1.py new file mode 100755 index 0000000..220ef2c --- /dev/null +++ b/day10/part1.py @@ -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()