Skip to content

Commit

Permalink
AoC 2023 Day 3 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 3, 2023
1 parent d9f5a0d commit 5c71fce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 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) | | | | | | | | | | | | | | | | | | | | | | | |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | | | | | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2023_01.java) | [](src/main/java/AoC2023_02.java) | | | | | | | | | | | | | | | | | | | | | | | |
| bash | | | | | | | | | | | | | | | | | | | | | | | | | |
| c++ | | | | | | | | | | | | | | | | | | | | | | | | | |
Expand Down
61 changes: 40 additions & 21 deletions src/main/python/AoC2023_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
# Advent of Code 2023 Day 3
#

import sys
import re
import sys
from typing import NamedTuple
from collections import defaultdict
from math import prod

from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
from aoc.common import log
from aoc.grid import CharGrid, Cell

Input = CharGrid
Output1 = int
Output2 = int

from aoc.grid import Cell
from aoc.grid import CharGrid

TEST = """\
467..114..
Expand All @@ -31,22 +30,28 @@
"""


class Solution(SolutionBase[CharGrid, Output1, Output2]):
class EnginePart(NamedTuple):
part: str
number: int
start: int
end: int


Input = list[EnginePart]
Output1 = int
Output2 = int


class Solution(SolutionBase[list[EnginePart], Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
g = [[c for c in line] for line in input_data]
return CharGrid(g)

def part_1(self, grid: CharGrid) -> Output1:
ans = 0
grid = CharGrid(g)
engine_parts = []
for r, row in enumerate(grid.values):
s = "".join(row)
mm = re.finditer(r"[0-9]+", "".join(row))
matches = [_ for _ in mm]
# if len(matches) == 0:
# continue
log(matches)
for m in matches:
log(m.span())
found = False
start = m.span()[0]
end = m.span()[1]
Expand All @@ -57,17 +62,31 @@ def part_1(self, grid: CharGrid) -> Output1:
found = True
break
if found:
ans += int(s[start:end])
number = int(s[start:end])
engine_parts.append(
EnginePart(v, number, n.row, n.col)
)
break
return ans
return engine_parts

def part_1(self, engine_parts: list[EnginePart]) -> Output1:
return sum(ep.number for ep in engine_parts)

def part_2(self, input: CharGrid) -> Output2:
return 0
def part_2(self, engine_parts: list[EnginePart]) -> Output2:
gears = [ep for ep in engine_parts if ep.part == "*"]
d = defaultdict(list)
for g in gears:
d[(g.start, g.end)].append(g.number)
ans = 0
for x in d:
if len(d[x]) == 2:
ans += prod(d[x])
return ans

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

0 comments on commit 5c71fce

Please sign in to comment.