Taddeus Kroes преди 4 години
родител
ревизия
f636b509a9
променени са 2 файла, в които са добавени 860 реда и са изтрити 0 реда
  1. 80 0
      2021/19_beacon.py
  2. 780 0
      2021/input/19

+ 80 - 0
2021/19_beacon.py

@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+import sys
+from itertools import combinations, starmap
+from collections import deque, Counter
+
+def parse(f):
+    for line in f:
+        if line.startswith('---'):
+            scan = []
+        elif line == '\n':
+            yield scan
+        else:
+            scan.append(tuple(map(int, line.split(','))))
+    yield scan
+
+def rotx(x, y, z):
+    return x, -z, y
+
+def rotz(x, y, z):
+    return -y, x, z
+
+ALL_ROTATIONS = (
+    rotz, rotz, rotz,
+    rotx,
+    rotz, rotz, rotz,
+    rotx,
+    rotz, rotz, rotz,
+    lambda x, y, z: rotx(*rotz(*rotz(x, y, z))),
+    rotz, rotz, rotz,
+    rotx,
+    rotz, rotz, rotz,
+    lambda x, y, z: rotx(*rotx(*rotx(x, y, z))),
+    rotz, rotz, rotz,
+)
+
+def rotations(scan):
+    yield scan
+    for rot in ALL_ROTATIONS:
+        scan = list(starmap(rot, scan))
+        yield scan
+
+def largest_intersection(scan1, scan2):
+    return Counter((x2 - x1, y2 - y1, z2 - z1)
+                    for x1, y1, z1 in scan1
+                    for x2, y2, z2 in scan2).most_common(1)[0]
+
+def move(scan, dx, dy, dz):
+    return [(x + dx, y + dy, z + dz) for x, y, z in scan]
+
+def find_scanner(base, rotated_scans):
+    for scan in rotated_scans:
+        diff, count = largest_intersection(scan, base)
+        if count >= 12:
+            return diff, move(scan, *diff)
+    return None, None
+
+def join(scans):
+    joined = set(scans[0])
+    remaining = deque(list(rotations(scan)) for scan in scans[1:])
+    scanners = [(0, 0, 0)]
+
+    while remaining:
+        rotated_scans = remaining.popleft()
+        scanner, moved = find_scanner(joined, rotated_scans)
+        if scanner:
+            joined |= set(moved)
+            scanners.append(scanner)
+        else:
+            remaining.append(rotated_scans)
+
+    return joined, scanners
+
+def max_distance(scanners):
+    return max(abs(x2 - x1) + abs(y2 - y1) + abs(z2 - z1)
+               for (x1, y1, z1), (x2, y2, z2) in combinations(scanners, 2))
+
+scans = list(parse(sys.stdin))
+joined, scanners = join(scans)
+print(len(joined))
+print(max_distance(scanners))

+ 780 - 0
2021/input/19

@@ -0,0 +1,780 @@
+--- scanner 0 ---
+-757,414,492
+-593,762,-478
+-608,779,-508
+-761,323,468
+-583,-536,626
+539,660,453
+745,-932,544
+419,509,439
+62,-24,118
+338,-793,-584
+-775,808,-521
+708,771,-798
+-90,-99,12
+400,-811,-639
+850,-816,468
+715,652,-760
+-820,276,460
+737,-883,437
+716,659,-718
+-619,-458,-521
+-551,-451,-643
+477,574,399
+-653,-464,-508
+-596,-505,704
+438,-706,-637
+-589,-621,598
+
+--- scanner 1 ---
+-386,481,-530
+568,822,748
+56,78,41
+-653,837,672
+-825,-465,-698
+-842,-495,-683
+517,-321,717
+401,-495,-638
+442,717,-695
+-661,-765,361
+558,611,-702
+-871,-508,-516
+-339,473,-471
+-709,-772,450
+441,-376,741
+-477,505,-534
+388,-630,-740
+473,639,-708
+-528,845,807
+-610,763,777
+583,809,645
+502,-587,734
+395,-508,-672
+-37,-6,-116
+-623,-735,471
+505,810,757
+
+--- scanner 2 ---
+-695,364,-810
+-642,483,453
+745,-960,-441
+743,235,-394
+15,-139,-14
+-452,-639,536
+635,652,646
+810,-702,549
+700,-762,505
+-453,-534,569
+621,-860,-431
+695,501,604
+-510,489,384
+-706,370,-596
+-536,388,405
+-628,402,-625
+633,540,465
+802,-735,390
+606,-923,-539
+578,271,-459
+105,-2,-130
+-517,-596,-542
+-571,-576,-529
+-486,-440,473
+654,315,-514
+-443,-634,-618
+
+--- scanner 3 ---
+812,-526,-776
+447,879,691
+-2,88,14
+-853,-613,-699
+868,-612,-712
+-746,793,-620
+-860,-681,-577
+491,-498,660
+-529,-701,302
+502,814,567
+-540,613,383
+445,628,-447
+355,810,596
+403,-351,685
+-807,800,-831
+-33,-43,-159
+-731,776,-886
+583,616,-432
+-582,553,534
+-602,-816,250
+471,485,-398
+872,-622,-827
+-568,-778,372
+377,-461,620
+-721,615,495
+-847,-579,-712
+
+--- scanner 4 ---
+-383,-542,-722
+703,879,-598
+587,-459,579
+-424,322,707
+-398,371,564
+113,1,1
+629,-610,639
+-400,-767,313
+-455,-643,378
+777,869,-619
+-308,-672,382
+793,544,399
+-536,356,576
+828,-723,-592
+-389,-498,-592
+768,-571,577
+854,755,392
+-621,489,-516
+-611,512,-432
+849,-622,-566
+-443,-593,-655
+753,753,-624
+778,810,416
+780,-628,-698
+-654,590,-372
+
+--- scanner 5 ---
+366,-674,-398
+646,574,-495
+810,-712,775
+-776,-616,-664
+709,499,-484
+538,832,519
+-851,-513,601
+330,-736,-449
+-832,-477,488
+754,549,-531
+534,-704,-421
+589,756,371
+-769,699,-884
+742,-698,801
+-608,351,427
+616,920,401
+-821,-527,-723
+-149,-42,-32
+-775,702,-638
+-576,337,535
+-750,-560,-579
+-717,658,-756
+632,-708,837
+-18,44,105
+-824,-294,581
+-538,374,539
+
+--- scanner 6 ---
+-670,706,-858
+342,376,641
+405,-867,-344
+-768,-540,-416
+-591,518,863
+-711,414,845
+-776,-509,-579
+281,-922,466
+-464,-713,728
+378,352,-541
+288,342,-625
+287,-929,-439
+-757,-508,-659
+-18,-130,-81
+-577,685,-840
+371,-842,552
+-550,433,837
+-523,-701,877
+303,376,-402
+247,-928,631
+-659,-686,773
+333,388,704
+445,389,626
+-433,677,-834
+358,-840,-351
+
+--- scanner 7 ---
+-608,662,-491
+-421,395,723
+537,400,-654
+-801,-707,334
+-716,-378,-908
+-606,481,-603
+-691,-669,364
+722,-794,-779
+-712,-755,327
+-551,-443,-872
+-64,-119,-13
+50,75,-130
+590,472,837
+655,-609,-741
+-668,620,-642
+-445,471,778
+569,546,847
+-428,456,512
+595,496,721
+733,-580,-773
+563,421,-755
+669,469,-660
+-627,-498,-828
+799,-779,496
+860,-721,542
+820,-593,474
+
+--- scanner 8 ---
+529,-663,-800
+519,-548,-804
+-429,-739,586
+271,298,-446
+-718,433,-595
+505,549,874
+562,-663,915
+615,-599,-744
+-729,576,-465
+351,484,-448
+-529,-792,673
+305,358,-378
+-68,-144,62
+498,443,758
+528,-676,921
+-836,389,447
+-828,282,496
+-796,-643,-252
+-491,-794,568
+-652,604,-655
+492,471,843
+-653,-636,-220
+460,-798,928
+-941,313,563
+-115,1,182
+-609,-662,-266
+
+--- scanner 9 ---
+819,-614,-852
+-394,-498,662
+-568,774,-852
+-488,737,-781
+-672,-417,-703
+-664,-337,-815
+652,-627,-823
+498,457,699
+595,-492,407
+-568,636,624
+645,572,-483
+575,387,738
+47,-73,82
+-546,514,691
+697,-627,420
+-442,625,-847
+-356,-496,767
+-671,-375,-696
+460,335,665
+745,655,-394
+663,-614,414
+865,584,-431
+637,-657,-806
+-700,627,697
+-294,-551,749
+
+--- scanner 10 ---
+12,120,-29
+-342,-679,-939
+530,-282,501
+-809,684,-468
+489,-308,416
+572,350,621
+-403,-504,359
+577,584,627
+403,-417,-730
+-66,14,-156
+414,664,-519
+598,-372,-703
+650,-300,397
+-506,-519,372
+-884,650,-644
+-765,612,-643
+-394,-441,371
+462,-268,-718
+-374,674,418
+-308,-514,-945
+398,839,-455
+-344,592,336
+363,708,-493
+-479,717,344
+623,433,604
+-432,-593,-859
+
+--- scanner 11 ---
+891,-554,-721
+-666,637,-444
+-607,-541,-415
+29,2,4
+162,154,-54
+-404,-217,531
+532,510,-451
+713,-606,830
+928,-640,-742
+-654,574,-435
+-681,-584,-520
+-609,441,448
+-689,-459,-385
+-398,-451,536
+743,571,-458
+921,-592,-831
+534,611,565
+477,586,-475
+781,-478,790
+408,661,539
+-653,652,-547
+759,-664,769
+-524,415,489
+418,587,560
+-421,-300,582
+-628,433,609
+
+--- scanner 12 ---
+-377,614,-406
+-552,-831,694
+527,-699,-733
+-754,618,829
+-411,-958,-621
+745,422,-840
+509,-816,-841
+-425,703,-413
+849,417,-798
+605,657,787
+-689,453,793
+-338,-820,700
+772,341,-775
+649,784,719
+670,-589,575
+-539,-918,-553
+-353,-897,-575
+-463,593,-443
+463,-695,-855
+130,-179,-79
+513,-525,626
+-634,529,749
+703,610,736
+45,-46,33
+661,-442,574
+-523,-753,662
+
+--- scanner 13 ---
+909,550,-668
+112,-89,-14
+-273,384,725
+824,456,386
+-373,374,601
+-793,-530,-506
+-594,-593,-472
+787,-689,-321
+-657,472,-591
+717,-801,-298
+821,591,-739
+-310,482,586
+-637,485,-537
+493,-740,530
+749,-693,-284
+724,419,337
+474,-627,566
+-531,-606,790
+-33,37,38
+-471,-562,680
+501,-647,347
+618,425,401
+-699,-626,-473
+944,544,-718
+-498,-447,782
+-599,561,-463
+
+--- scanner 14 ---
+551,-821,-732
+-578,-311,780
+558,-481,467
+-688,548,475
+743,667,230
+628,740,282
+-667,-431,700
+384,-850,-719
+-866,523,-640
+608,409,-597
+-743,-376,744
+-94,25,-89
+458,-392,550
+-849,538,-906
+566,-804,-762
+506,422,-732
+-493,-640,-462
+-603,-602,-492
+705,701,352
+-582,-688,-455
+338,-429,479
+-873,462,-851
+540,416,-719
+-626,687,514
+-686,729,363
+
+--- scanner 15 ---
+-702,-746,443
+739,592,-374
+597,814,559
+763,-590,-410
+-670,658,-388
+682,788,518
+727,655,-365
+-335,568,609
+-345,642,630
+-858,-671,-565
+-459,719,-381
+-404,643,-405
+438,-883,401
+-843,-533,-553
+701,796,679
+-892,-559,-696
+409,-907,638
+-725,-756,527
+-624,-789,383
+430,-889,545
+726,577,-477
+-80,-67,-39
+-461,618,635
+773,-674,-320
+58,23,81
+739,-451,-335
+
+--- scanner 16 ---
+-755,578,-603
+487,691,-754
+593,698,-638
+20,82,-38
+-923,-807,452
+382,-791,415
+-559,792,522
+-834,-736,364
+-836,-723,-539
+552,845,568
+-922,-847,-618
+-610,838,475
+-739,456,-638
+394,-761,-691
+678,891,498
+358,-716,554
+382,-752,-628
+738,925,566
+565,704,-723
+348,-806,638
+-667,922,511
+-779,551,-493
+-916,-813,-453
+-153,145,64
+-861,-689,357
+395,-649,-613
+
+--- scanner 17 ---
+664,-630,-754
+805,704,436
+-431,686,597
+-587,-662,388
+-631,-532,382
+569,439,-825
+400,-738,620
+-717,-556,-781
+-461,651,805
+-984,750,-627
+423,-822,610
+-711,-688,-850
+459,413,-747
+674,775,343
+780,-635,-778
+790,-656,-704
+-840,657,-649
+-53,56,-150
+-812,-662,-829
+451,333,-808
+-112,-109,-68
+-936,553,-605
+-682,-599,442
+708,813,465
+501,-882,653
+-541,612,682
+
+--- scanner 18 ---
+552,283,-704
+606,-679,-568
+33,2,166
+471,-600,469
+15,-169,53
+-565,601,731
+-501,373,-422
+-680,574,833
+846,372,668
+634,306,-684
+-534,478,-421
+563,-505,400
+703,258,-627
+-665,-549,-505
+-466,-662,726
+618,-629,465
+552,-774,-505
+-521,541,841
+-617,-644,-611
+-605,-701,664
+804,401,618
+-686,-650,-479
+-424,-813,666
+893,472,557
+-597,279,-455
+546,-730,-635
+
+--- scanner 19 ---
+835,-511,-678
+895,455,462
+650,-578,-640
+-505,-664,948
+-359,-696,-553
+161,-95,23
+790,538,-336
+749,472,-391
+888,283,439
+30,-35,133
+-384,431,727
+-455,-638,822
+-462,405,617
+820,-434,597
+-380,-670,-654
+784,307,460
+699,385,-327
+621,-500,-672
+-401,325,609
+-428,-564,-559
+-560,649,-682
+762,-373,643
+-604,545,-778
+-431,-643,938
+634,-413,575
+-582,742,-826
+
+--- scanner 20 ---
+-600,-782,533
+596,577,-670
+620,550,-780
+-646,690,-519
+286,-347,543
+763,-848,-655
+-541,-753,-645
+413,595,320
+321,586,395
+-752,697,-635
+-75,63,-4
+677,-724,-734
+-518,-812,451
+-554,-650,-660
+-526,-677,576
+-882,525,309
+-724,703,-550
+-890,466,262
+-24,138,-191
+461,-391,471
+-175,5,-154
+462,-272,509
+-553,-762,-582
+-858,529,400
+373,551,219
+580,455,-635
+757,-814,-820
+
+--- scanner 21 ---
+-377,-514,-910
+844,-627,-723
+759,-599,-717
+-445,-454,-944
+-402,821,411
+929,642,-755
+749,-810,733
+734,-797,630
+812,-798,742
+-294,-810,772
+-514,787,542
+-376,772,554
+875,-705,-710
+876,388,726
+-447,-640,-864
+916,507,-653
+800,333,702
+-433,-834,697
+-803,392,-696
+-517,-844,787
+922,520,-687
+-743,443,-600
+22,-64,-168
+160,4,-35
+815,599,719
+-799,507,-686
+
+--- scanner 22 ---
+817,529,-684
+728,823,688
+-473,-503,418
+-72,102,143
+-547,657,684
+559,-544,861
+-476,801,651
+821,-620,-435
+699,781,638
+-521,742,740
+628,870,577
+-465,-410,601
+526,-703,853
+622,-557,-401
+-721,-457,-213
+727,-524,-402
+-528,962,-575
+499,-588,751
+-580,834,-534
+-689,-349,-227
+66,6,64
+794,611,-664
+842,680,-607
+-446,-523,637
+-522,716,-578
+-759,-461,-250
+
+--- scanner 23 ---
+-352,-697,-348
+432,569,514
+-44,-72,111
+-406,627,809
+-581,-621,391
+-473,788,-390
+819,-664,596
+877,399,-834
+566,-655,-584
+782,-784,667
+366,523,626
+473,-689,-636
+-378,729,728
+-410,628,711
+730,-564,679
+-673,698,-393
+-334,-673,-384
+-432,-525,427
+361,649,477
+-528,-485,420
+-310,-680,-500
+151,90,68
+-518,655,-370
+946,312,-770
+592,-590,-584
+865,328,-773
+
+--- scanner 24 ---
+-519,780,-365
+634,802,-649
+-674,902,562
+105,13,-47
+-667,878,446
+-662,-634,635
+-479,684,-358
+465,-735,700
+-522,-563,-663
+704,539,455
+430,-788,740
+757,-463,-300
+-606,-678,-646
+677,-509,-447
+794,739,452
+-657,-639,463
+-706,-723,627
+735,834,-666
+-595,-597,-832
+575,-753,793
+661,694,432
+-62,53,53
+633,806,-747
+678,-478,-454
+-691,860,410
+-615,631,-329
+
+--- scanner 25 ---
+183,2,-88
+656,638,481
+894,-843,430
+-325,-646,403
+-658,-347,-552
+619,806,-682
+657,512,589
+562,876,-811
+5,-20,46
+-679,-329,-645
+-650,-353,-408
+-315,-562,313
+536,-608,-751
+586,835,-891
+-759,588,-782
+701,481,532
+-639,600,608
+867,-836,383
+-349,-785,328
+567,-771,-743
+956,-748,312
+535,-528,-732
+-702,558,533
+-554,511,-793
+-765,497,-720
+-786,627,617
+
+--- scanner 26 ---
+568,-612,-772
+-494,-703,-776
+689,-640,-900
+512,-573,342
+765,568,275
+-699,525,741
+508,448,-580
+558,403,-627
+-458,-494,437
+58,44,-10
+-695,623,-517
+-620,-556,487
+-452,-603,-788
+-689,415,705
+929,541,305
+675,-619,407
+479,-562,414
+-703,347,679
+-665,791,-494
+-527,-407,477
+128,-52,-173
+583,511,-591
+698,-517,-736
+-727,827,-545
+-409,-773,-839
+841,619,423
+
+--- scanner 27 ---
+743,-494,-529
+20,142,-6
+374,-668,657
+-568,-551,-817
+374,719,-731
+-538,-500,730
+-515,-415,-788
+-385,-491,820
+-427,-538,747
+-393,697,-399
+411,815,380
+-365,799,-480
+530,822,418
+420,-486,700
+-702,757,526
+-419,-455,-781
+403,-501,670
+-626,716,631
+-397,698,-497
+157,78,120
+379,593,-582
+523,823,460
+445,639,-694
+832,-594,-612
+-640,682,457
+795,-714,-541