-
Notifications
You must be signed in to change notification settings - Fork 4
/
day-10.py
62 lines (53 loc) · 2.43 KB
/
day-10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
with open('inputs/day-10.txt') as file:
cmds = file.read().split('\n')
cmds = [y.split(' ') for y in cmds]
# Part 1
def increaseCycle(cycle, n):
for _ in range(n):
cycle += 1
if (cycle+20) % 40 == 0:
signalStrengths.append(cycle*x)
return cycle
x, cycle = 1, 0
signalStrengths = []
for c in cmds:
if c[0] == 'addx':
cycle = increaseCycle(cycle, 2)
x += int(c[1])
elif c[0] == 'noop':
cycle = increaseCycle(cycle, 1)
print('Day 10 Part 1:', sum(signalStrengths))
# Part 2
def moveSprite(x):
sprite = ['.'] * 40
for i in range(x-1,x+2):
if i < 40 and i >= 0:
sprite[i] = '#'
return sprite
def nextCycle():
if sprite[cycle-((cycle//40)*40)-1] == '#':
nextPixel = '#'
else:
nextPixel = '.'
crt.append(nextPixel)
x, cycle = 1, 0
sprite = ['#'] * 3 + ['.'] * 37
crt = []
for c in cmds:
if c[0] == 'addx':
for _ in range(2):
cycle += 1
nextCycle()
x += int(c[1])
sprite = moveSprite(x)
elif c[0] == 'noop':
cycle += 1
nextCycle()
print('Day 10 Part 2:')
for i, p in enumerate(crt):
print(p,end='')
if (i+1) % 40 == 0:
print('\n',end='')
# One-line Solutions
print('Day 10 Part 1:', sum(signalStrengths) if not (signalStrengths := []) and (x := 1) and not (cycle := 0) and [[(c[0] == 'addx') and [(cycle := cycle+1) and ((cycle+20) % 40 == 0 and signalStrengths.append(cycle*x)) for _ in range(2)] and (x := x+int(c[1]))] and [(c[0] == 'noop') and [(cycle := cycle+1) and ((cycle+20) % 40 == 0 and signalStrengths.append(cycle*x))]] for c in [y.split(' ') for y in open('inputs/day-10.txt').read().split('\n')]] else '')
print('Day 10 Part 2:\n',''.join(v[:-1]) if not (v := []) and (x := 1) and not (cycle := 0) and (sprite := ['#']*3 + ['.']*37) and not (crt := []) and [((c[0] == 'addx' and [(cycle := cycle+1) and ([crt.append('#' if sprite[cycle-((cycle//40)*40)-1] == '#' else '.')] or 1) for _ in range(2)] and ((x := x+int(c[1])) or 1) and (sprite := ['.']*40) and [sprite.pop(i) and sprite.insert(i, '#') for i in range(x-1,x+2) if i < 40 and i >= 0]) or (c[0] == 'noop' and [(cycle := cycle+1) and crt.append('#' if sprite[cycle-((cycle//40)*40)-1] == '#' else '.')])) for c in [y.split(' ') for y in open('inputs/day-10.txt').read().split('\n')]] and [not v.append(p) and ((i+1) % 40 == 0 and v.append('\n')) for i, p in enumerate(crt)] else '', sep='')