From 476fb277a441b13d884542ef26c3dd192334efe4 Mon Sep 17 00:00:00 2001 From: felixm Date: Mon, 25 Dec 2023 10:41:47 -0500 Subject: [PATCH] Do day 15. --- README.md | 5 ++-- d15.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ d19.py | 30 +++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 d15.py create mode 100644 d19.py diff --git a/README.md b/README.md index 1246e53..69e1255 100644 --- a/README.md +++ b/README.md @@ -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 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 14: -- Day 15: +- Day 14: 5:55 for first and then 48:00; straightforward but slow, ofc +- 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 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 @@ -22,3 +22,4 @@ 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 made it meaner by using different clock directions for example and input. +- Day 19: diff --git a/d15.py b/d15.py new file mode 100644 index 0000000..351a022 --- /dev/null +++ b/d15.py @@ -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() diff --git a/d19.py b/d19.py new file mode 100644 index 0000000..8b33504 --- /dev/null +++ b/d19.py @@ -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()