Advent of Code 2022 day 10
I’m learning to properly read questions and to not take too many shortcuts. Part 2 has an unexpected and interesting spin. Coded with morale support from Sam.
with open("10.txt") as f:
lines = f.read().splitlines()
cycles = range(20, 221, 40)
values = [1]
val = 1
for line in lines:
if line == "noop":
values.append(val)
elif line.startswith("addx"):
values.append(val)
val += int(line.split(" ")[1])
values.append(val)
sum = 0
for cycle in cycles:
print(cycle, values[cycle-1])
sum += cycle*values[cycle-1]
print(sum)
# part 2
def is_in(x, values):
if abs(values[x] - x%40) < 2:
return "#"
return "."
print(
"\n".join(
"".join(is_in(x, values) for x in range(y, y+40))
for y in range(0, 240, 40)
)
)