summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 20:36:08 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 20:36:34 +0100
commit5e72d702c3fd3b52549e4f6184926b37e1b87a4e (patch)
tree06e316cf0b2a8909550605438786e20c9a93664c
parente35d13c79571290242e8cc82023b0fef4f6e4919 (diff)
download2022-5e72d702c3fd3b52549e4f6184926b37e1b87a4e.tar.gz
2022-5e72d702c3fd3b52549e4f6184926b37e1b87a4e.tar.bz2
2022-5e72d702c3fd3b52549e4f6184926b37e1b87a4e.zip
Day 10
-rw-r--r--day10/__init__.py69
1 files changed, 27 insertions, 42 deletions
diff --git a/day10/__init__.py b/day10/__init__.py
index e6d385a..740ff08 100644
--- a/day10/__init__.py
+++ b/day10/__init__.py
@@ -11,55 +11,41 @@ class Assignment(BaseAssignment, ABC):
11 super().__init__(path) 11 super().__init__(path)
12 self.x_history = {1: 1} 12 self.x_history = {1: 1}
13 13
14 def addx(self, value: str) -> Iterator[int]: 14 def parse_instructions(self, instructions: Iterator[str]) -> Iterator[int]:
15 yield 0
16 yield int(value)
17
18 def noop(self) -> Iterator:
19 yield 0
20
21 def x_after(self, cycles: int, instructions: List[str]):
22 try:
23 return self.x_history[cycles]
24 except KeyError:
25 pass
26
27 cycle = 1
28 x = 1 15 x = 1
29 16
30 while cycle <= cycles and len(instructions) > 0: 17 for instruction in instructions:
31 instruction, *instructions = instructions
32 instruction, *args = instruction.split(" ") 18 instruction, *args = instruction.split(" ")
33 19
34 for val in getattr(self, instruction)(*args): 20 yield x
35 x += val
36 cycle += 1
37 self.x_history[cycle] = x
38 21
39 return self.x_history[cycles] 22 if instruction != "noop":
23 yield x
24 x += int(*args)
40 25
41 26
42class AssignmentOne(Assignment): 27class AssignmentOne(Assignment):
43 example_result = 13140 28 example_result = 13140
44 29
45 def run(self, input: Iterator) -> int: 30 def run(self, input: Iterator) -> int:
46 instructions = list(input)
47
48 return sum( 31 return sum(
49 [ 32 [
50 cycles * self.x_after(cycles, instructions) 33 cycle * x
51 for cycles in [220, 180, 140, 100, 60, 20] 34 for cycle, x in enumerate(self.parse_instructions(input), start=1)
35 if cycle in {20, 60, 100, 140, 180, 220}
52 ] 36 ]
53 ) 37 )
54 38
55 39
56class AssignmentTwo(Assignment): 40class AssignmentTwo(Assignment):
57 example_result = """##..##..##..##..##..##..##..##..##..##.. 41 example_result = (
58###...###...###...###...###...###...###. 42 "██ ██ ██ ██ ██ ██ ██ ██ ██ ██ \n"
59####....####....####....####....####.... 43 "███ ███ ███ ███ ███ ███ ███ \n"
60#####.....#####.....#####.....#####..... 44 "████ ████ ████ ████ ████ \n"
61######......######......######......#### 45 "█████ █████ █████ █████ \n"
62#######.......#######.......#######.....""" 46 "██████ ██████ ██████ ████\n"
47 "███████ ███████ ███████ "
48 )
63 49
64 def render_pixel(self, row: int, col: int, instructions: List[str]): 50 def render_pixel(self, row: int, col: int, instructions: List[str]):
65 cycle = row * 40 + col 51 cycle = row * 40 + col
@@ -70,16 +56,15 @@ class AssignmentTwo(Assignment):
70 return "." 56 return "."
71 57
72 def run(self, input: Iterator) -> str: 58 def run(self, input: Iterator) -> str:
73 instructions = list(input) 59 rows = []
60 row = []
74 61
75 screen = [ 62 for cycle, x in enumerate(self.parse_instructions(instructions=input), start=1):
76 [ 63 col = (cycle - 1) % 40
77 self.render_pixel(row, col, instructions) 64 row.append("█" if col in {x - 1, x, x + 1} else " ")
78 for col in reversed(range(1, 41))
79 ]
80 for row in reversed(range(6))
81 ]
82 65
83 return "\n".join( 66 if cycle % 40 == 0:
84 ["".join(reversed([str(i) for i in row])) for row in reversed(screen)] 67 rows.append(row)
85 ) 68 row = []
69
70 return "\n".join("".join(row) for row in rows)