Skip to content

Commit

Permalink
AoC 2023 Day 15 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 15, 2023
1 parent f67517c commit b8d5c07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- @BEGIN:ImplementationsTable:2023@ -->
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | | | | | | | | | | | |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | [](src/main/python/AoC2023_15.py) | | | | | | | | | | |
| java | [](src/main/java/AoC2023_01.java) | [](src/main/java/AoC2023_02.java) | [](src/main/java/AoC2023_03.java) | [](src/main/java/AoC2023_04.java) | [](src/main/java/AoC2023_05.java) | [](src/main/java/AoC2023_06.java) | [](src/main/java/AoC2023_07.java) | [](src/main/java/AoC2023_08.java) | [](src/main/java/AoC2023_09.java) | [](src/main/java/AoC2023_10.java) | [](src/main/java/AoC2023_11.java) | [](src/main/java/AoC2023_12.java) | [](src/main/java/AoC2023_13.java) | [](src/main/java/AoC2023_14.java) | | | | | | | | | | | |
| bash | | | | | | | | | | | | | | | | | | | | | | | | | |
| c++ | | | | | | | | | | | | | | | | | | | | | | | | | |
Expand Down
48 changes: 39 additions & 9 deletions src/main/python/AoC2023_15.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#

import sys
from collections import defaultdict

from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
from aoc.common import log

Input = list[str]
Output1 = int
Expand All @@ -23,24 +25,52 @@ class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return list(input_data)[0].split(",")

def hash(self, s: str) -> int:
ans = 0
for ch in s:
ans += ord(ch)
ans *= 17
ans %= 256
return ans

def part_1(self, steps: Input) -> Output1:
ans = 0
for step in steps:
prev = 0
for ch in step:
prev += ord(ch)
prev *= 17
prev %= 256
ans += prev
ans += self.hash(step)
return ans

def part_2(self, input: Input) -> Output2:
return 0
def part_2(self, steps: Input) -> Output2:
boxes = defaultdict[int, list[tuple[str, int]]](list)
for step in steps:
if "=" in step:
label, fl = step.split("=")
box = self.hash(label)
lst = boxes[box]
for x in lst:
if x[0] == label:
idx = lst.index(x)
lst[idx] = (label, int(fl))
break
else:
lst.append((label, int(fl)))
else:
label = step[:-1]
box = self.hash(label)
lst = boxes[box]
for x in lst:
if x[0] == label:
lst.remove(x)
log(boxes)
ans = 0
for box in boxes:
for i, x in enumerate(boxes[box]):
ans += (box + 1) * (i + 1) * x[1]
return ans

@aoc_samples(
(
("part_1", TEST, 1320),
# ("part_2", TEST, "TODO"),
("part_2", TEST, 145),
)
)
def samples(self) -> None:
Expand Down

0 comments on commit b8d5c07

Please sign in to comment.