Files
aoc/2022/10/j.py
2023-11-30 19:20:34 +02:00

54 lines
877 B
Python

import os
import sys
input = open(os.path.dirname(__file__) +
"/input.txt", "r").readlines()
cost = {
'noop': 1,
'addx': 2
}
H = 6
W = 40
screen = [[False for i in range(W)] for j in range(H)]
cycle = 1
X = 1
strs = []
line = 1
for l in input:
cmd = l.split()
for i in range(cost[cmd[0]]):
pos = cycle % 40 - 1
# p1
if ((cycle+20) % 40) == 0:
strenth = cycle*X
strs.append(strenth)
# p2
if cycle % 40 == 0:
line += 1
if (pos == X-1 or pos == X or pos == X+1):
screen[line-1][pos] = True
cycle += 1
if cmd[0] == 'addx':
X += int(cmd[1])
# p1
print(sum(strs))
# p2
for y in range(H):
for x in range(W):
if (screen[y][x]):
print("#", end="")
else:
print(".", end="")
print()