Compare commits
4 commits
39917c0e4b
...
b11765c45e
| Author | SHA1 | Date | |
|---|---|---|---|
| b11765c45e | |||
| 2a5f9dd3c7 | |||
| 9b7ef1bc6d | |||
| f26111e42e |
4 changed files with 393 additions and 0 deletions
42
2022/d10/ex1/ex1.py
Executable file
42
2022/d10/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,42 @@
|
|||
#!/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()
|
||||
143
2022/d10/ex1/input
Normal file
143
2022/d10/ex1/input
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
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
|
||||
65
2022/d10/ex2/ex2.py
Executable file
65
2022/d10/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,65 @@
|
|||
#!/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()
|
||||
143
2022/d10/ex2/input
Normal file
143
2022/d10/ex2/input
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue