Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 5:53 | 15:40 | 21:33 |
That was really fast! I finished 164th worldwide for Part 1. I made registers a dictionary to future-proof the code, but it turned out it wasn't needed. I wonder if this will be used in any future problems like IntCode was.
from helpers.datagetter import data_in, submit
ans = 0
din = data_in(split=True, numbers=False)
cycles = 0
regs = {"X": 1}
def cycle():
global regs, cycles, ans
cycles += 1
if cycles in [x*40+20 for x in range(6)]:
ans += regs["X"] * cycles
for line in din:
if line.startswith("noop"):
cycle()
else:
instr = line.split(" ")[0]
num = int(line.split(" ")[1])
if instr == "addx":
cycle()
cycle()
regs["X"] += num
print(ans)
submit(ans)
I took so long for Part 2 because of a silly error. I initially defined the screen using screen = [["."]*40]*6
, but unfortunately all of the lists defined by *
are just different references to the same list. So when I updated one, it would update them all. Unfortunately, I spent longer catching this than I would have liked. I'm going to make a helper for defining X*Y*...
lists for future use. Or use numpy, idk.
from helpers.datagetter import data_in
din = data_in(split=True, numbers=False)
cycles = 0
regs = {"X": 1}
screen = []
for i in range(6):
a = []
for j in range(40):
a.append(".")
screen.append(a)
posx, posy = 0, 0
def cycle():
global regs, cycles, posx, posy
if posx == regs["X"]-1 or posx == regs["X"] or posx == regs["X"]+1:
screen[posy][posx] = "#"
posx += 1
cycles += 1
if posx == 40:
posx = 0
posy += 1
for line in din:
if line.startswith("noop"):
cycle()
else:
instr = line.split(" ")[0]
num = int(line.split(" ")[1])
if instr == "addx":
cycle()
cycle()
regs["X"] += num
for thing in screen:
print("".join(thing))