diff --git a/2022/d10/ex1/ex1.py b/2022/d10/ex1/ex1.py deleted file mode 100755 index 20f8fd5..0000000 --- a/2022/d10/ex1/ex1.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python - -import dataclasses -import sys -from collections.abc import Iterable, Iterator - - -@dataclasses.dataclass -class CPU: - x: int = dataclasses.field(default=1, init=False) - cycle: int = dataclasses.field(default=0, init=False) - - def execute(self, instructions: Iterable[str]) -> Iterator[int]: - for instr in instructions: - yield from self.execute_once(instr) - - def execute_once(self, instruction: str) -> Iterator[int]: - if instruction == "noop": - yield from self.__do_cycle() - else: - assert instruction.startswith("addx") - yield from self.__do_cycle(2) - self.x += int(instruction.split()[1]) - - def __do_cycle(self, cycles: int = 1) -> Iterator[int]: - for _ in range(cycles): - self.cycle += 1 - yield self.cycle - - -def solve(input: list[str]) -> int: - cpu = CPU() - return sum(cycle * cpu.x for cycle in cpu.execute(input) if (cycle % 40) == 20) - - -def main() -> None: - input = sys.stdin.read().splitlines() - print(solve(input)) - - -if __name__ == "__main__": - main() diff --git a/2022/d10/ex1/input b/2022/d10/ex1/input deleted file mode 100644 index f93ec2c..0000000 --- a/2022/d10/ex1/input +++ /dev/null @@ -1,143 +0,0 @@ -noop -noop -noop -addx 5 -addx 1 -addx 4 -addx 1 -noop -addx 4 -noop -addx 1 -addx 4 -addx 8 -addx -7 -addx 3 -addx 1 -noop -addx 4 -addx 2 -addx 5 -addx -1 -noop -addx -37 -noop -noop -addx 3 -addx 2 -addx 13 -addx 12 -addx -15 -addx -2 -addx 2 -addx -11 -addx 18 -addx 2 -addx -15 -addx 16 -addx 5 -addx 2 -addx 5 -noop -noop -noop -addx 3 -addx -2 -addx -38 -noop -addx 3 -addx 4 -noop -noop -noop -noop -noop -addx 5 -addx 5 -noop -noop -addx 21 -addx -17 -addx 6 -noop -noop -noop -noop -addx 5 -noop -noop -noop -noop -noop -addx 3 -addx 5 -addx -38 -noop -noop -addx 5 -addx -2 -addx 1 -addx 7 -noop -addx 22 -addx -18 -addx -11 -addx 27 -addx -13 -addx 2 -addx 5 -addx -8 -addx 9 -addx 2 -noop -addx 7 -noop -addx 1 -noop -addx -38 -noop -addx 2 -addx 5 -addx -3 -noop -addx 8 -addx 11 -addx -6 -noop -addx 24 -addx -31 -addx 10 -addx 2 -addx 5 -addx 3 -noop -addx 2 -addx -29 -addx 21 -addx 11 -addx 5 -addx -39 -addx 4 -addx -2 -addx 2 -addx 7 -noop -addx -1 -addx 2 -noop -addx 4 -noop -addx 1 -addx 2 -addx 5 -addx 2 -noop -noop -addx -6 -addx 9 -addx -18 -addx 25 -addx 3 -noop -addx -17 -noop diff --git a/2022/d10/ex2/ex2.py b/2022/d10/ex2/ex2.py deleted file mode 100755 index cdcb4ac..0000000 --- a/2022/d10/ex2/ex2.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python - -import dataclasses -import sys -from collections.abc import Iterable, Iterator - - -@dataclasses.dataclass -class CPU: - x: int = dataclasses.field(default=1, init=False) - cycle: int = dataclasses.field(default=0, init=False) - - def execute(self, instructions: Iterable[str]) -> Iterator[int]: - for instr in instructions: - yield from self.execute_once(instr) - - def execute_once(self, instruction: str) -> Iterator[int]: - if instruction == "noop": - yield from self.__do_cycle() - else: - assert instruction.startswith("addx") - yield from self.__do_cycle(2) - self.x += int(instruction.split()[1]) - - def __do_cycle(self, cycles: int = 1) -> Iterator[int]: - for _ in range(cycles): - self.cycle += 1 - yield self.cycle - - -@dataclasses.dataclass -class CRT: - width: int = dataclasses.field(default=40, init=False) - height: int = dataclasses.field(default=6, init=False) - pixels: list[list[bool]] = dataclasses.field(init=False) - - def __post_init__(self): - self.pixels = [[False for _ in range(self.width)] for _ in range(self.height)] - - def do_pixel(self, cycle: int, x_reg: int) -> None: - cycle -= 1 # Simpler modulo computation - x, y = cycle % 40, cycle // 40 % 6 - self.pixels[y][x] = abs(x - x_reg) <= 1 - - def draw(self) -> str: - return "\n".join( - "".join("#" if pixel else " " for pixel in line) for line in self.pixels - ) - - -def solve(input: list[str]) -> str: - cpu = CPU() - crt = CRT() - for cycle in cpu.execute(input): - crt.do_pixel(cycle, cpu.x) - return crt.draw() - - -def main() -> None: - input = sys.stdin.read().splitlines() - print(solve(input)) - - -if __name__ == "__main__": - main() diff --git a/2022/d10/ex2/input b/2022/d10/ex2/input deleted file mode 100644 index f93ec2c..0000000 --- a/2022/d10/ex2/input +++ /dev/null @@ -1,143 +0,0 @@ -noop -noop -noop -addx 5 -addx 1 -addx 4 -addx 1 -noop -addx 4 -noop -addx 1 -addx 4 -addx 8 -addx -7 -addx 3 -addx 1 -noop -addx 4 -addx 2 -addx 5 -addx -1 -noop -addx -37 -noop -noop -addx 3 -addx 2 -addx 13 -addx 12 -addx -15 -addx -2 -addx 2 -addx -11 -addx 18 -addx 2 -addx -15 -addx 16 -addx 5 -addx 2 -addx 5 -noop -noop -noop -addx 3 -addx -2 -addx -38 -noop -addx 3 -addx 4 -noop -noop -noop -noop -noop -addx 5 -addx 5 -noop -noop -addx 21 -addx -17 -addx 6 -noop -noop -noop -noop -addx 5 -noop -noop -noop -noop -noop -addx 3 -addx 5 -addx -38 -noop -noop -addx 5 -addx -2 -addx 1 -addx 7 -noop -addx 22 -addx -18 -addx -11 -addx 27 -addx -13 -addx 2 -addx 5 -addx -8 -addx 9 -addx 2 -noop -addx 7 -noop -addx 1 -noop -addx -38 -noop -addx 2 -addx 5 -addx -3 -noop -addx 8 -addx 11 -addx -6 -noop -addx 24 -addx -31 -addx 10 -addx 2 -addx 5 -addx 3 -noop -addx 2 -addx -29 -addx 21 -addx 11 -addx 5 -addx -39 -addx 4 -addx -2 -addx 2 -addx 7 -noop -addx -1 -addx 2 -noop -addx 4 -noop -addx 1 -addx 2 -addx 5 -addx 2 -noop -noop -addx -6 -addx 9 -addx -18 -addx 25 -addx 3 -noop -addx -17 -noop