day19: takes 20s to run (per part), but it gets the job done. Added to the list of "needs improvement later"

This commit is contained in:
Stefan Harmuth 2021-12-19 09:11:38 +01:00
parent 9bcdd10ed7
commit f909b2d97b
4 changed files with 1473 additions and 0 deletions

90
day19.py Normal file
View File

@ -0,0 +1,90 @@
from collections import defaultdict
from itertools import combinations
from tools.aoc import AOCDay
from tools.coordinate import Coordinate, DistanceAlgorithm
from tools.grid import Grid, GridTransformation
from typing import Any, List, Union
ROTATION_PATH = [
GridTransformation.ROTATE_X,
GridTransformation.ROTATE_X,
GridTransformation.ROTATE_Z,
GridTransformation.ROTATE_X,
GridTransformation.ROTATE_X,
GridTransformation.ROTATE_Z
]
def overlap(beacon1: Grid, beacon2: Grid) -> Union[Coordinate, None]:
diffs = defaultdict(int)
for c1 in beacon1.getActiveCells():
for c2 in beacon2.getActiveCells():
# Interestingly adding an if statement here to break out early when already 12 matches are found
# slows the loop down so much, that the end result is slower compared to just counting everything
# and checking the results afterwards.
diffs[c1 - c2] += 1
for d in diffs:
if diffs[d] >= 12:
return d
def converge(scanner_grids: List[Grid]) -> (Grid, List[Coordinate]):
main_grid = scanner_grids.pop()
diff_list = [Coordinate(0, 0, 0)]
while scanner_grids:
found = False
for i, scanner in enumerate(scanner_grids):
for rotation in ROTATION_PATH:
for y_rotation in range(4):
if diff := overlap(main_grid, scanner):
#print("Grid overlap:", main_grid.name, scanner.name, "=>", len(scanner_grids) - 1, "to go.")
scanner_grids.pop(i)
diff_list.append(diff)
for c in scanner.getActiveCells():
main_grid.set(c + diff)
found = True
break
scanner.transform(GridTransformation.ROTATE_Y)
scanner.transform(rotation)
if found:
break
if found:
break
assert found, "No overlapping Grids found"
return main_grid, diff_list
class Day(AOCDay):
test_solutions_p1 = [79]
test_solutions_p2 = [3621]
def part1(self) -> Any:
scanner_grids = []
for scanner_input in self.getMultiLineInputAsArray():
scanner_grid = Grid()
scanner_grid.name = scanner_input[0]
for l in scanner_input[1:]:
scanner_grid.set(Coordinate(*map(int, l.split(","))))
scanner_grids.append(scanner_grid)
teh_grid, _ = converge(scanner_grids)
return len(teh_grid.getActiveCells())
def part2(self) -> Any:
scanner_grids = []
for scanner_input in self.getMultiLineInputAsArray():
scanner_grid = Grid()
scanner_grid.name = scanner_input[0]
for l in scanner_input[1:]:
scanner_grid.set(Coordinate(*map(int, l.split(","))))
scanner_grids.append(scanner_grid)
_, diff_list = converge(scanner_grids)
max_diff = 0
for s1, s2 in combinations(diff_list, 2):
max_diff = max(max_diff, s1.getDistanceTo(s2, includeDiagonals=False, algorithm=DistanceAlgorithm.MANHATTAN))
return max_diff

1111
inputs/input19 Normal file

File diff suppressed because it is too large Load Diff

136
inputs/test_input19_1_0 Normal file
View File

@ -0,0 +1,136 @@
--- scanner 0 ---
404,-588,-901
528,-643,409
-838,591,734
390,-675,-793
-537,-823,-458
-485,-357,347
-345,-311,381
-661,-816,-575
-876,649,763
-618,-824,-621
553,345,-567
474,580,667
-447,-329,318
-584,868,-557
544,-627,-890
564,392,-477
455,729,728
-892,524,684
-689,845,-530
423,-701,434
7,-33,-71
630,319,-379
443,580,662
-789,900,-551
459,-707,401
--- scanner 1 ---
686,422,578
605,423,415
515,917,-361
-336,658,858
95,138,22
-476,619,847
-340,-569,-846
567,-361,727
-460,603,-452
669,-402,600
729,430,532
-500,-761,534
-322,571,750
-466,-666,-811
-429,-592,574
-355,545,-477
703,-491,-529
-328,-685,520
413,935,-424
-391,539,-444
586,-435,557
-364,-763,-893
807,-499,-711
755,-354,-619
553,889,-390
--- scanner 2 ---
649,640,665
682,-795,504
-784,533,-524
-644,584,-595
-588,-843,648
-30,6,44
-674,560,763
500,723,-460
609,671,-379
-555,-800,653
-675,-892,-343
697,-426,-610
578,704,681
493,664,-388
-671,-858,530
-667,343,800
571,-461,-707
-138,-166,112
-889,563,-600
646,-828,498
640,759,510
-630,509,768
-681,-892,-333
673,-379,-804
-742,-814,-386
577,-820,562
--- scanner 3 ---
-589,542,597
605,-692,669
-500,565,-823
-660,373,557
-458,-679,-417
-488,449,543
-626,468,-788
338,-750,-386
528,-832,-391
562,-778,733
-938,-730,414
543,643,-506
-524,371,-870
407,773,750
-104,29,83
378,-903,-323
-778,-728,485
426,699,580
-438,-605,-362
-469,-447,-387
509,732,623
647,635,-688
-868,-804,481
614,-800,639
595,780,-596
--- scanner 4 ---
727,592,562
-293,-554,779
441,611,-461
-714,465,-776
-743,427,-804
-660,-479,-426
832,-632,460
927,-485,-438
408,393,-506
466,436,-512
110,16,151
-258,-428,682
-393,719,612
-211,-452,876
808,-476,-593
-575,615,604
-485,667,467
-680,325,-822
-627,-443,-432
872,-547,-609
833,512,582
807,604,487
839,-516,451
891,-625,532
-652,-548,-490
30,-46,-14

136
inputs/test_input19_2_0 Normal file
View File

@ -0,0 +1,136 @@
--- scanner 0 ---
404,-588,-901
528,-643,409
-838,591,734
390,-675,-793
-537,-823,-458
-485,-357,347
-345,-311,381
-661,-816,-575
-876,649,763
-618,-824,-621
553,345,-567
474,580,667
-447,-329,318
-584,868,-557
544,-627,-890
564,392,-477
455,729,728
-892,524,684
-689,845,-530
423,-701,434
7,-33,-71
630,319,-379
443,580,662
-789,900,-551
459,-707,401
--- scanner 1 ---
686,422,578
605,423,415
515,917,-361
-336,658,858
95,138,22
-476,619,847
-340,-569,-846
567,-361,727
-460,603,-452
669,-402,600
729,430,532
-500,-761,534
-322,571,750
-466,-666,-811
-429,-592,574
-355,545,-477
703,-491,-529
-328,-685,520
413,935,-424
-391,539,-444
586,-435,557
-364,-763,-893
807,-499,-711
755,-354,-619
553,889,-390
--- scanner 2 ---
649,640,665
682,-795,504
-784,533,-524
-644,584,-595
-588,-843,648
-30,6,44
-674,560,763
500,723,-460
609,671,-379
-555,-800,653
-675,-892,-343
697,-426,-610
578,704,681
493,664,-388
-671,-858,530
-667,343,800
571,-461,-707
-138,-166,112
-889,563,-600
646,-828,498
640,759,510
-630,509,768
-681,-892,-333
673,-379,-804
-742,-814,-386
577,-820,562
--- scanner 3 ---
-589,542,597
605,-692,669
-500,565,-823
-660,373,557
-458,-679,-417
-488,449,543
-626,468,-788
338,-750,-386
528,-832,-391
562,-778,733
-938,-730,414
543,643,-506
-524,371,-870
407,773,750
-104,29,83
378,-903,-323
-778,-728,485
426,699,580
-438,-605,-362
-469,-447,-387
509,732,623
647,635,-688
-868,-804,481
614,-800,639
595,780,-596
--- scanner 4 ---
727,592,562
-293,-554,779
441,611,-461
-714,465,-776
-743,427,-804
-660,-479,-426
832,-632,460
927,-485,-438
408,393,-506
466,436,-512
110,16,151
-258,-428,682
-393,719,612
-211,-452,876
808,-476,-593
-575,615,604
-485,667,467
-680,325,-822
-627,-443,-432
872,-547,-609
833,512,582
807,604,487
839,-516,451
891,-625,532
-652,-548,-490
30,-46,-14