Do day 15.

This commit is contained in:
2023-12-25 10:41:47 -05:00
parent aa4730cad7
commit 476fb277a4
3 changed files with 104 additions and 2 deletions

View File

@@ -13,8 +13,8 @@
- Day 11: 68:00; okay but not elegant and way too slow ofc; x-ray solution would have been neat - Day 11: 68:00; okay but not elegant and way too slow ofc; x-ray solution would have been neat
- Day 12: 52:00 and 22:00 for leaderboard; had the right idea and I am good at this type of problem - Day 12: 52:00 and 22:00 for leaderboard; had the right idea and I am good at this type of problem
- Day 13: 90:00; pretty straightforward but way too slow - Day 13: 90:00; pretty straightforward but way too slow
- Day 14: - Day 14: 5:55 for first and then 48:00; straightforward but slow, ofc
- Day 15: - Day 15: 4:30 and 31:20; more reading comprehension than programming
- Day 16: 00:27:30 745; best placement so far, of course still horribly slow - Day 16: 00:27:30 745; best placement so far, of course still horribly slow
- Day 17: a couple of hours; I realized that I need A* after a while; reused - Day 17: a couple of hours; I realized that I need A* after a while; reused
implementation from Project Euler but improved with heapq which was super fun implementation from Project Euler but improved with heapq which was super fun
@@ -22,3 +22,4 @@
but didn't realize that I have to compute the outer edges for a while and but didn't realize that I have to compute the outer edges for a while and
after I did, I still got clockwise/counter-clockwise issues. They could have after I did, I still got clockwise/counter-clockwise issues. They could have
made it meaner by using different clock directions for example and input. made it meaner by using different clock directions for example and input.
- Day 19:

71
d15.py Normal file
View File

@@ -0,0 +1,71 @@
from lib import *
from collections import OrderedDict
EXAMPLE = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"
def hash(word):
# Init to 0
# Determine the ASCII code for the current character of the string.
# Increase the current value by the ASCII code you just determined. Set
# the current value to itself multiplied by 17. Set the current value to
# the remainder of dividing itself by 256.
h = 0
for c in word:
v = ord(c)
h += v
h *= 17
h %= 256
return h
def solve(input: Input, second=False):
res = 0
line = input.lines()[0]
for word in line.split(","):
h = hash(word)
res += h
if not second:
return res
boxes = [OrderedDict() for _ in range(256)]
for word in line.split(","):
h = hash(word)
if '=' in word:
label, focal = word.split('=')
box = boxes[hash(label)]
if label in box:
box[label] = (label, focal)
else:
box[label] = (label, focal)
elif '-' in word:
label = word.replace('-', '')
box = boxes[hash(label)]
if label in box:
del box[label]
else:
raise Exception()
res = 0
for i, b in enumerate(boxes):
for j, label in enumerate(b):
label, focal = b[label]
r = (i + 1) * (j + 1) * int(focal)
res += r
return res
def main():
DAY_INPUT = "i15.txt"
print("Example 1:", solve(Input(EXAMPLE)))
print("Solution 1:", solve(Input(DAY_INPUT)))
assert solve(Input(DAY_INPUT)) == 511343
# 4:30
print("Example 2:", solve(Input(EXAMPLE), True))
print("Solution 2:", solve(Input(DAY_INPUT), True))
assert solve(Input(DAY_INPUT), True) == 294474
# 31:20
return
if __name__ == "__main__":
main()

30
d19.py Normal file
View File

@@ -0,0 +1,30 @@
from lib import *
EXAMPLE = """
"""
def solve(i: Input, second=False):
res = 0
i.stats()
# g = i.grid2()
# ls = i.lines()
# ps = i.paras()
return res
def main():
DAY_INPUT = "i19.txt"
print("Example 1:", solve(Input(EXAMPLE)))
return
print("Solution 1:", solve(Input(DAY_INPUT)))
return
print("Example 2:", solve(Input(EXAMPLE), True))
return
print("Solution 2:", solve(Input(DAY_INPUT), True))
return
if __name__ == "__main__":
main()