Skip to content

Commit fde304c

Browse files
committed
day10
1 parent c28d5d5 commit fde304c

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed

2022/10/__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def cast_input(inputs: str):
2+
results: list[tuple[int, int]] = []
3+
for line in inputs.splitlines():
4+
if line == 'noop':
5+
results.append((1, 0))
6+
elif line.startswith('addx '):
7+
results.append((2, int(line.rsplit(maxsplit=1).pop())))
8+
return results
9+
10+
11+
def part1(inputs: list[tuple[int, int]]):
12+
result = 0
13+
cycle, value = 1, 1
14+
for c, x in inputs:
15+
for v in [0] * (c - 1) + [x]:
16+
value += v
17+
cycle += 1
18+
if cycle % 40 == 20:
19+
result += cycle * value
20+
return result
21+
22+
23+
def render(c: list[str], wide: int, letters: int):
24+
def split(seq, amt: int):
25+
return [seq[i : i + amt] for i in range(0, len(seq), amt)]
26+
27+
lines = ((' ' * 3).join(split(x, wide // letters)) for x in split(''.join(c), wide))
28+
return '\n' + '\n'.join(lines)
29+
30+
31+
def part2(inputs: list[tuple[int, int]]):
32+
RADIUS = 3 // 2 # the sprite is 3 pixels wide
33+
WIDE, HIGH, LETTERS = 40, 6, 8
34+
LIT, DARK = '#.'
35+
crt = [LIT] * (WIDE * HIGH)
36+
cycle, value = 1, 1
37+
for c, x in inputs:
38+
for v in [0] * (c - 1) + [x]:
39+
value += v
40+
if abs(value - cycle % WIDE) > RADIUS:
41+
crt[cycle] = DARK
42+
cycle += 1
43+
if cycle == WIDE * HIGH:
44+
return render(crt, WIDE, LETTERS)

2022/10/input

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
noop
2+
noop
3+
addx 5
4+
noop
5+
noop
6+
addx 6
7+
addx 4
8+
addx -4
9+
addx 4
10+
addx -6
11+
addx 11
12+
addx -1
13+
addx 2
14+
addx 4
15+
addx 3
16+
noop
17+
addx 2
18+
addx -30
19+
addx 2
20+
addx 33
21+
noop
22+
addx -37
23+
noop
24+
noop
25+
noop
26+
addx 3
27+
addx 2
28+
addx 5
29+
addx 20
30+
addx 7
31+
addx -24
32+
addx 2
33+
noop
34+
addx 7
35+
addx -2
36+
addx -6
37+
addx 13
38+
addx 3
39+
addx -2
40+
addx 2
41+
noop
42+
addx -5
43+
addx 10
44+
addx 5
45+
addx -39
46+
addx 1
47+
addx 5
48+
noop
49+
addx 3
50+
noop
51+
addx -5
52+
addx 10
53+
addx -2
54+
addx 2
55+
noop
56+
noop
57+
addx 7
58+
noop
59+
noop
60+
noop
61+
noop
62+
addx 3
63+
noop
64+
addx 3
65+
addx 2
66+
addx 8
67+
addx -1
68+
addx -20
69+
addx 21
70+
addx -38
71+
addx 5
72+
addx 2
73+
noop
74+
noop
75+
noop
76+
addx 8
77+
noop
78+
noop
79+
addx -2
80+
addx 2
81+
addx -7
82+
addx 14
83+
addx 5
84+
noop
85+
noop
86+
noop
87+
addx -16
88+
addx 17
89+
addx 2
90+
addx -12
91+
addx 19
92+
noop
93+
noop
94+
addx -37
95+
noop
96+
noop
97+
noop
98+
addx 3
99+
addx 2
100+
addx 2
101+
addx 5
102+
addx 20
103+
addx -19
104+
addx 2
105+
noop
106+
noop
107+
noop
108+
addx 5
109+
addx 19
110+
addx -12
111+
addx 3
112+
addx -2
113+
addx 2
114+
addx -18
115+
addx 25
116+
addx -14
117+
addx -22
118+
addx 1
119+
noop
120+
noop
121+
noop
122+
addx 3
123+
addx 5
124+
addx -4
125+
addx 7
126+
addx 4
127+
noop
128+
addx 1
129+
noop
130+
noop
131+
addx 2
132+
addx -6
133+
addx 15
134+
addx -1
135+
addx 4
136+
noop
137+
noop
138+
addx 1
139+
addx 4
140+
addx -33
141+
noop
142+
addx 21
143+
noop

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Advent of code Python solutions and CLI tool.
1717
| Day🎄07 | No Space Left On Device |||
1818
| Day🎄08 | Treetop Tree House |||
1919
| Day🎄09 | Rope Bridge |||
20-
| Day🎄10 | | | |
20+
| Day🎄10 | Cathode-Ray Tube | | |
2121
| Day🎄11 | | | |
2222
| Day🎄12 | | | |
2323
| Day🎄13 | | | |

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy