Files
AdventOfCode/2022/day10/code.py
T

41 lines
1014 B
Python
Executable File

#!/usr/bin/env python3
import sys
def show_crt(crt):
pixels = ''
for x in range(240):
p = '#' if crt[x] == 1 else '.'
pixels = pixels + p
if (x % 40) == 39:
print(pixels)
pixels = ''
if __name__ == '__main__':
ops = [tuple(line.strip('\n').split(' ')) for line in open(sys.argv[1])]
reg = 1
cycle = 1
strength = 0
crt = [0] * 240
for op in ops:
if (cycle % 40) == 20: strength = strength + reg * cycle
if (cycle % 40) in [reg,reg+1,reg+2]: crt[cycle-1] = 1
if op[0] == 'noop': cycle = cycle + 1
else:
cycle = cycle + 1
if (cycle % 40) == 20: strength = strength + reg * cycle
if (cycle % 40) in [reg,reg+1,reg+2]: crt[cycle-1] = 1
cycle = cycle + 1
reg = reg + int(op[1])
# challenge 1
res1 = str(strength)
print("challenge 1:" + "\n" + res1 + "\n")
# challenge 2
print("challenge 2:" + "\n")
show_crt(crt)