A quick one, I felt like I could have done it quicker if I wasn't on my laptop using a mouse without a mousepad.
Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 7:58 | 2:37 | 10:35 |
This one is a kind of "just do it" type of problem. I'll explain anyway.
I first created a function to compute the difference between each step, then I created another function to keep doing that over and over until a list of all zeros is reached. Finally, I loop through these differences and solve for X where x - val_i = val_i+1
, rearrange x = val_i + val_i+1
. Then sum them up and there you have it!
from helpers.datagetter import aocd_data_in
din, aocd_submit = aocd_data_in(split=True, numbers=True)
ans = 0
def get_diff(l):
o = []
for i in range(len(l)-1):
o.append(l[i+1] - l[i])
return o
def get_diffs(l):
o = [l]
while any([x != 0 for x in l]):
l = get_diff(l)
o.append(l)
return o
for line in din:
diffs = get_diffs(line)
diffs[-1].append(0)
for i in range(len(diffs)-2, -1, -1):
diffs[i].append(diffs[i+1][-1] + diffs[i][-1])
ans += diffs[0][-1]
aocd_submit(ans)
A very simple modification for Part 2, I was just having trouble rearranging val_i - x = val_i+1
at midnight. I was happy with the delta, but it could have been way better if I was thinking better! Just switch that and the values to be inserted at the beginning and that's all you need.
I could have just kept the appends the same and just changed which values I was referencing for the current "row" but the way it is currently done keeps it in proper order with the problem, so I'm fine with that.
from helpers.datagetter import aocd_data_in
din, aocd_submit = aocd_data_in(split=True, numbers=True)
ans = 0
def get_diff(l):
o = []
for i in range(len(l)-1):
o.append(l[i+1] - l[i])
return o
def get_diffs(l):
o = [l]
while any([x != 0 for x in l]):
l = get_diff(l)
o.append(l)
return o
for line in din:
diffs = get_diffs(line)
diffs[-1].insert(0, 0)
for i in range(len(diffs)-2, -1, -1):
diffs[i].insert(0, diffs[i][0] - diffs[i+1][0])
ans += diffs[0][0]
aocd_submit(ans)