59 lines
916 B
Python
59 lines
916 B
Python
import os
|
|
|
|
input = open(os.path.dirname(__file__) +
|
|
"/input.txt", "r").readlines()
|
|
|
|
cost = {
|
|
'noop': 1,
|
|
'addx': 2
|
|
}
|
|
|
|
H = 6
|
|
W = 40
|
|
|
|
screen = []
|
|
for y in range(H):
|
|
tmp = ''
|
|
for x in range(W):
|
|
tmp += '.'
|
|
screen.append(tmp)
|
|
|
|
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):
|
|
|
|
cur = screen[line-1]
|
|
screen[line-1] = cur[:pos] + "#" + cur[pos+1:]
|
|
|
|
cycle += 1
|
|
|
|
if cmd[0] == 'addx':
|
|
X += int(cmd[1])
|
|
|
|
# p1
|
|
print(sum(strs))
|
|
|
|
# p2
|
|
for y in range(H):
|
|
tmp = ''
|
|
for x in range(W):
|
|
tmp += screen[y][x]
|
|
print(tmp)
|