generated from public/aoc_template
day08
This commit is contained in:
parent
f6f2e1344b
commit
42bd5367a0
69
day08.py
Normal file
69
day08.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
from tools.aoc import AOCDay
|
||||||
|
from tools.coordinate import Coordinate
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class Day(AOCDay):
|
||||||
|
inputs = [
|
||||||
|
[
|
||||||
|
(40, "input8_test"),
|
||||||
|
(140008, "input8"),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
(25272, "input8_test"),
|
||||||
|
(9253260633, "input8"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
def parse_input(self) -> tuple[dict[float, Coordinate], dict[Coordinate, int]]:
|
||||||
|
boxes = [Coordinate(*map(int, line.split(","))) for line in self.getInput()]
|
||||||
|
dists = {}
|
||||||
|
for i, box in enumerate(boxes):
|
||||||
|
for box2 in boxes[i + 1 :]:
|
||||||
|
dists[box.getDistanceTo(box2)] = (box, box2)
|
||||||
|
|
||||||
|
members = {box: i for i, box in enumerate(boxes)}
|
||||||
|
return dists, members
|
||||||
|
|
||||||
|
def solve(self, part2: bool = False) -> int:
|
||||||
|
dists, members = self.parse_input()
|
||||||
|
circuits = {i: [box] for box, i in members.items()}
|
||||||
|
connections_needed = 10 if self.is_test() else 1000
|
||||||
|
connections_done = 0
|
||||||
|
last_pair = None
|
||||||
|
|
||||||
|
for _, (box0, box1) in sorted(dists.items()):
|
||||||
|
connections_done += 1
|
||||||
|
if (not part2 and connections_done > connections_needed) or len(circuits) == 1:
|
||||||
|
break
|
||||||
|
|
||||||
|
if members[box0] == members[box1]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
merger = members[box1]
|
||||||
|
for box in circuits[merger]:
|
||||||
|
members[box] = members[box0]
|
||||||
|
circuits[members[box0]].append(box)
|
||||||
|
del circuits[merger]
|
||||||
|
last_pair = (box0, box1)
|
||||||
|
|
||||||
|
if part2:
|
||||||
|
return last_pair[0].x * last_pair[1].x
|
||||||
|
else:
|
||||||
|
ans = 1
|
||||||
|
for _, v in sorted(circuits.items(), key=lambda k: len(k[1]), reverse=True)[:3]:
|
||||||
|
ans *= len(v)
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
def part1(self) -> Any:
|
||||||
|
return self.solve()
|
||||||
|
|
||||||
|
|
||||||
|
def part2(self) -> Any:
|
||||||
|
return self.solve(part2=True)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
day = Day(2025, 8)
|
||||||
|
day.run(verbose=True)
|
||||||
1000
inputs/input8
Normal file
1000
inputs/input8
Normal file
File diff suppressed because it is too large
Load Diff
20
inputs/input8_test
Normal file
20
inputs/input8_test
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689
|
||||||
Loading…
Reference in New Issue
Block a user