Skip to content

Commit

Permalink
Merge pull request #155 from ephemient/py/day23
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 23, 2024
2 parents d83af79 + 48a182b commit da20000
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Development occurs in language-specific directories:
|[Day20.hs](hs/src/Day20.hs)|[Day20.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day20.kt)|[day20.py](py/aoc2024/day20.py)|[day20.rs](rs/src/day20.rs)|
|[Day21.hs](hs/src/Day21.hs)|[Day21.kt](kt/aoc2024-lib/src/jvmCodegen/kotlin/com/github/ephemient/aoc2024/codegen/Day21.kt)|[day21.py](py/aoc2024/day21.py)|[day21.rs](rs/src/day21.rs)|
|[Day22.hs](hs/src/Day22.hs)|[Day22.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day22.kt)|[day22.py](py/aoc2024/day22.py)|[day22.rs](rs/src/day22.rs)|
|[Day23.hs](hs/src/Day23.hs)|[Day23.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day23.kt)|||
|[Day23.hs](hs/src/Day23.hs)|[Day23.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day23.kt)|[day23.py](py/aoc2024/day23.py)||
92 changes: 92 additions & 0 deletions py/aoc2024/day23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Day 23: LAN Party
"""

from collections import defaultdict

SAMPLE_INPUT = """
kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn
"""


def _parse(data: str) -> dict[str, set[str]]:
graph = defaultdict(set)
for line in data.splitlines():
if "-" not in line:
continue
a, b = line.split("-", maxsplit=1)
graph[min(a, b)].add(max(a, b))
graph[max(a, b)]
return graph


def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT)
7
"""
graph = _parse(data)
return sum(
a.startswith("t") or b.startswith("t") or c.startswith("t")
for a, bs in graph.items()
for b in bs
for c in graph[b]
if c in bs
)


def part2(data: str) -> str:
"""
>>> part2(SAMPLE_INPUT)
'co,de,ka,ta'
"""
graph = _parse(data)

def _maxcomplete(used: set[str], remaining: set[str]) -> set[str]:
return (
max(
(
_maxcomplete(used | {key}, remaining & graph[key])
for key in remaining
),
key=len,
)
if remaining
else used
)

return ",".join(sorted(_maxcomplete(set(), set(graph))))


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ day19 = "aoc2024.day19:parts"
day20 = "aoc2024.day20:parts"
day21 = "aoc2024.day21:parts"
day22 = "aoc2024.day22:parts"
day23 = "aoc2024.day23:parts"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit da20000

Please sign in to comment.