-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
34 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
--- Day 15: Lens Library --- | ||
https://adventofcode.com/2023/day/15 | ||
""" | ||
from aocd import data | ||
|
||
|
||
def keyfunc(s): | ||
i = 0 | ||
for char in s: | ||
i += ord(char) | ||
i *= 17 | ||
i %= 256 | ||
return i | ||
|
||
|
||
hashmap = [{} for i in range(256)] | ||
for op in data.split(","): | ||
match op.strip("-").split("="): | ||
case [k, v]: hashmap[keyfunc(k)][k] = int(v) | ||
case [k]: hashmap[keyfunc(k)].pop(k, None) | ||
|
||
print("answer_a:", sum(keyfunc(op) for op in data.split(","))) | ||
print("answer_b:", sum(i*sum(k*v for k,v in enumerate(box.values(),1)) for i,box in enumerate(hashmap,1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 | ||
1320 | ||
- |