Historian Hysteria

Advent of Code 2024 [Day 1]

01-12-2024

The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.

As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they’ll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

You haven’t even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian’s office.

Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?

Throughout the Chief’s office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don’t miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.

There’s just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren’t very similar. Maybe you can help The Historians reconcile their lists?

For example:

3   4
4   3
2   5
1   3
3   9
3   3

Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.

Within each pair, figure out how far apart the two numbers are; you’ll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.

In the example list above, the pairs and distances would be as follows:

To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!

Your actual left and right lists contain many location IDs. What is the total distance between your lists?

async function main() {
  const input = await Deno.readTextFile("input.txt");
  const lines = input.trim().split("\n");
  const leftList = [];
  const rightList = [];

  for (const line of lines) {
    const [left, right] = line.split(/\s+/).map(Number);
    leftList.push(left);
    rightList.push(right);
  }

  leftList.sort((a, b) => a - b);
  rightList.sort((a, b) => a - b);

  let totalDistance = 0;
  for (let i = 0; i < leftList.length; i++) {
    totalDistance += Math.abs(leftList[i] - rightList[i]);
  }
  console.log("Total distance:", totalDistance);
}

main().catch((err) => console.error(err));

Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.

Or are they?

The Historians can’t agree on which group made the mistakes or how to read most of the Chief’s handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren’t location IDs at all but rather misinterpreted handwriting.

This time, you’ll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.

Here are the same example lists again:

3   4
4   3
2   5
1   3
3   9
3   3

For these example lists, here is the process of finding the similarity score:

So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).

Once again consider your left and right lists. What is their similarity score?

async function main() {
  const input = await Deno.readTextFile("input.txt");
  const lines = input.trim().split("\n");
  const leftList = [];
  const rightList = [];

  for (const line of lines) {
    const [left, right] = line.split(/\s+/).map(Number);
    leftList.push(left);
    rightList.push(right);
  }

  const rightOccurrences = {};
  for (const num of rightList) {
    rightOccurrences[num] = (rightOccurrences[num] || 0) + 1;
  }

  let similarityScore = 0;
  for (const num of leftList) {
    const occurrencesInRight = rightOccurrences[num] || 0;
    similarityScore += num * occurrencesInRight;
  }
  console.log("Similarity score:", similarityScore);
}

main().catch((err) => console.error(err));
Click to show the input
57643   17620
19062   47340
11105   16109
72032   30050
16289   65967
42361   35795
45873   16124
16167   65832
57216   47340
15920   71163
22534   30837
38721   64603
27502   32313
17094   50986
84849   67526
20884   64392
21987   59516
39419   98547
10839   29910
77870   15917
94474   57604
29876   18768
72281   16109
52066   21561
11847   24044
83711   11404
47340   29941
54869   62664
63495   62664
87421   46184
12673   51492
21401   77546
13800   70131
35534   46986
51304   41199
51515   39683
25160   55295
39133   88509
68329   62664
56917   84849
68841   33033
27693   78397
82953   22601
47353   27226
70002   59980
74154   16109
86034   84849
95639   62349
45722   59516
69049   22317
51692   17620
39166   65967
30495   55388
81255   17869
60454   41199
26224   81511
74983   16124
55752   90759
22465   24501
57854   22943
94700   59516
83099   31049
26617   32773
23277   52176
34125   18754
61685   11874
49752   44170
82839   26115
28748   62664
19753   73220
76703   16124
41076   60454
94607   98100
88668   47340
56446   90759
93070   65832
25427   65524
51967   16964
23531   92947
44724   76864
32642   60237
54203   22536
62977   69799
31242   64392
94016   75284
55953   84849
35527   83890
54207   16964
44027   46184
82186   60112
72150   28927
68617   89252
57085   65832
18164   39683
28818   61894
94464   96385
73344   17681
76429   27054
25559   84215
24143   65907
12888   93195
33654   52859
13309   67042
90824   55187
42199   61494
99525   33401
60919   59516
14832   74668
83140   90759
68116   75402
35265   16124
30971   57216
84947   98547
35362   15902
98472   65832
32851   30050
47123   23601
37165   73599
64938   97672
39094   16964
32542   84849
70739   89107
59747   65832
51960   87068
75826   16109
96621   92566
80372   60656
15595   92864
17009   59796
87108   16124
70344   85636
12951   89107
25963   41199
60994   12246
26308   96930
58823   37556
18153   72254
20975   11570
42013   30141
53539   39683
40729   63401
10964   17620
76208   18768
55734   12636
29742   14160
40999   33033
61760   47123
57734   85993
32323   17620
53967   71169
41453   60454
53895   39683
99823   30156
12983   20098
71421   47829
76964   33049
82841   87433
55517   47340
75661   17620
22232   16081
36803   33033
83930   32773
54392   29132
67783   27226
78834   34040
46044   60454
97734   41199
16374   82621
23972   39787
80360   33654
48876   34378
11260   16124
53596   71313
60950   65832
37956   90734
87063   64392
66760   90734
24341   90734
13907   56662
39084   64270
30798   22591
30370   22943
57538   93797
41199   60454
79187   25199
43099   18768
13070   20881
39307   45484
89252   40350
94161   81798
91119   39683
97429   39599
64997   20578
32145   98547
13342   90759
82088   58672
61898   62664
72616   78897
17954   71652
58825   68383
44525   30658
28074   33033
17636   16124
94958   61724
48971   47340
85361   48120
82391   68794
69603   23741
64066   13582
65092   45163
49086   32773
42231   98547
56734   81797
76829   30156
99378   42745
59516   22285
18672   47123
68792   97672
46265   17620
28964   77566
14875   61068
95708   23528
38828   65832
56799   51315
40443   92737
64615   32773
27557   27226
89585   76621
67867   61305
28621   65979
88258   32773
73478   16124
98662   32773
38098   90759
76219   18768
23664   32773
70807   82839
64392   16109
39683   87325
47297   91952
25007   37572
90841   32773
82423   60454
56193   59516
83332   82839
17826   59516
95927   82839
62664   88503
33750   27771
51457   90759
47908   27341
67000   30050
66855   14734
20055   52372
60010   16109
30431   95964
12321   30156
53585   88159
55043   18768
20000   73383
28871   41199
82812   89107
13060   71616
70027   36310
80606   30050
55151   27226
63800   64392
35192   66432
78917   29589
92483   32773
10457   20728
70963   57216
67927   91119
36688   67070
93141   64392
93362   16124
81614   94940
81186   66565
56418   60166
64292   70331
43290   65832
10656   27226
38812   40982
33000   16124
18100   17109
37989   85483
14064   82711
14399   16964
22066   72604
49658   47123
29682   59516
59980   19363
91466   89107
70211   30342
35905   27226
67755   17620
75456   10664
54074   65832
23976   91119
22224   80267
78250   16081
53819   26866
99580   53011
49653   89107
13703   18768
39210   33033
13265   19583
70869   74044
42643   64392
97800   80872
33668   90759
62475   21888
64693   83264
31715   99185
83449   13990
33033   91104
39765   60517
93890   60670
23400   17620
92189   93768
63780   16109
73093   50153
13323   89107
69167   61436
48878   55630
13718   57216
27903   94896
69456   16109
46468   60454
52145   30596
20909   89107
22536   67083
80514   43159
98390   36556
78956   60454
23497   55983
15348   47123
61890   90734
89951   31879
20728   79394
49303   30713
30422   57216
73676   33033
49698   81919
76046   17620
45242   84849
25389   40202
32448   47299
59354   82839
70423   89252
89306   65967
59766   27268
37037   98547
65021   18895
54230   18768
43969   63446
50630   65832
54139   88509
85677   88746
47692   37399
21467   22536
83644   67594
21789   97672
13648   90759
28017   57216
74883   49798
16109   72646
46184   30050
36000   44597
33728   22536
44888   59516
11222   30050
36641   78012
42426   18768
93707   90734
82249   39683
86185   79158
63476   45090
52464   24645
33497   47340
51699   31223
67877   36634
16955   69854
49928   61017
16708   62664
91134   29644
24080   98547
16031   91119
49022   74628
88710   44253
22505   27651
22483   33033
10981   76873
74260   90620
53783   62664
67774   16124
56303   66706
87017   74124
25003   57216
54386   20728
55011   32773
35564   45090
43205   90759
90317   96125
65967   57216
27412   88804
26888   67612
50833   83419
90825   16964
68203   18108
51609   69179
69744   65832
56356   89107
89107   54941
88509   65832
92237   90734
44066   36406
80980   58864
98957   60659
22264   41199
60969   33654
17620   40115
90800   93572
62862   64392
27780   18768
66419   60454
70524   86222
26486   27226
72422   98298
90508   30050
28259   41199
14239   39683
21310   86711
35507   62664
77638   83310
10307   33033
71173   98232
39165   57216
92119   86108
52470   17765
18950   42325
55271   29040
25924   56949
55619   20728
48809   18768
32300   53100
60316   16153
44763   74270
25666   16964
81291   67521
60771   93797
81653   16964
67405   89107
79747   52151
47382   33033
29989   34981
50331   44690
17848   60454
89637   18768
98116   64392
80694   45090
95798   10999
78442   99645
45090   94611
23385   62664
86919   59516
71255   33033
82002   97672
89066   16109
98896   86008
27000   64545
99089   90734
13394   18768
20748   89107
88835   47340
87101   16109
61684   82505
87896   16081
97799   20728
63040   62664
85411   89107
34131   74658
65406   83937
91155   99556
65832   33674
81208   55861
85395   46184
16926   99827
35793   30050
12707   57216
67126   52814
92795   88509
91840   84607
54768   65833
52180   17819
53100   30156
73239   69810
49380   49485
80253   76262
53209   49131
11121   33033
44463   39683
53271   84849
69448   76937
36173   63218
60547   75774
54971   27226
44418   65832
66775   19905
71561   21069
32773   91318
76204   81715
98547   16081
24876   22287
33855   90759
28306   16554
30050   12914
21242   90734
19505   14213
54591   48135
72512   73973
10186   72386
98061   42401
83084   32773
38957   53473
56756   98547
36706   32773
82325   47179
24632   90734
55788   34600
21147   25888
97814   89467
76627   57216
83496   45700
35495   16109
57929   30156
44008   37332
68284   20370
58203   59516
33603   91682
91850   39247
55632   16124
48086   33894
96381   10569
77170   84849
39336   85833
74736   15527
92977   93797
74412   47340
99801   58501
51368   17620
68476   27226
21734   45090
85160   91119
67622   80512
33195   32773
99282   17620
40016   59575
70136   10061
60848   67727
79546   47112
71409   16964
48029   34780
75919   45090
26206   47123
26139   17620
98726   47340
75480   16333
60703   16081
56290   16109
20547   97672
85264   17620
72745   41885
11648   15733
89927   64392
41626   22536
40096   98547
11676   97594
38937   27226
95166   92683
45499   90759
44553   19438
58227   32773
78021   33033
31576   78113
25095   59274
62956   43830
87587   48890
37318   26768
31960   84849
44972   30050
47152   55462
48034   69405
99245   57216
51671   20728
31412   92094
19038   53100
30040   16109
81022   24270
28727   89381
63343   62053
44062   90734
52098   20786
32337   69696
10627   47340
86438   91119
11359   97672
79827   78893
50991   73474
41506   64392
64427   58282
30314   38013
55147   51690
24076   97672
55139   30156
84755   65832
77418   39683
65841   83092
89026   17102
43842   17620
69091   92274
45281   97447
76721   57216
85918   30383
75795   30156
25664   55704
97570   15961
52566   56257
56772   46184
66023   70725
70877   60352
58489   17620
97243   16081
90065   96590
15509   87622
16396   64392
52851   96477
27226   46960
90734   16124
69265   45713
52153   56385
62294   56305
60456   47123
22359   59516
72820   22831
16060   88509
48347   11949
93708   16109
40506   97672
34923   16152
82726   16124
75211   45090
42580   26606
55998   60454
36766   57216
49855   37173
95715   64233
88333   46280
43573   94103
58068   43319
65789   67256
26337   16081
41559   39630
94353   22943
60210   48776
31864   81825
43486   89107
89008   29137
40463   22547
69990   30050
43940   56443
77221   18768
75112   98547
10957   78948
74710   12120
59368   89107
32133   33654
75434   72998
14918   89107
78742   51635
47615   59414
92968   71551
34542   22943
38734   20291
10118   56801
43713   55518
82778   90734
90759   30050
65951   35684
60584   97672
74575   65365
79698   39683
25005   97672
87205   41199
61821   69895
91262   36850
59774   16124
25663   56501
59643   18768
91239   18768
67918   27226
90338   27226
46014   25578
83831   87690
61964   47340
93797   89252
31368   90759
25677   59638
98724   62664
45567   23553
58366   44041
97070   22943
42402   45061
30156   39747
21792   97672
88704   43711
86958   65967
87735   69189
46436   97038
67398   89243
25402   95547
75281   39683
10908   56544
13665   32898
14289   22943
78567   19318
58378   27226
70508   16670
27417   79914
39259   31265
75173   18768
67055   58536
94446   58240
13538   93924
47472   98146
73403   95703
80112   90734
53479   39683
68774   79739
15947   88002
98908   95443
34832   33654
49962   97672
37504   59516
75818   52244
61492   45090
46636   36056
60015   16109
91286   46184
37897   91119
92495   32773
86458   93797
39276   61612
22884   56365
71464   45090
32095   87760
54595   27226
24423   94229
22187   91119
82210   48731
19488   88509
65566   68008
77921   62589
17295   92247
91582   45090
24317   89107
68159   94508
81663   79967
78357   23724
36492   65832
53202   85336
64614   36692
13683   30156
46365   82672
61031   18768
52374   45090
24823   97672
90438   32489
14871   87616
65089   20993
37779   16124
39308   12698
32476   80660
61005   17620
22943   46184
87147   89107
25126   13389
16978   93342
60815   72087
84082   93797
73857   67867
18643   64984
90549   61047
30337   32773
62788   84849
36450   91530
18667   27226
31868   17135
34793   50284
87706   89107
24983   76729
49759   58359
88262   76480
75939   64392
19997   88556
62343   56462
45616   97672
45860   93888
68344   11535
55812   68813
58211   30050
57040   57926
60518   32773
71099   57155
24428   57216
54582   90308
55276   55066
58848   20728
75393   97672
11569   68342
88739   60454
92898   83347
91074   19081
68506   78420
28953   64392
40480   68489
78476   33654
52052   90759
38383   69088
94501   58424
11804   92541
88802   72137
14159   20700
31951   25669
71802   90759
51736   93797
40245   33196
76174   79841
65410   39683
77053   79537
47630   62664
22202   73334
67248   27226
18768   84849
75554   41199
91793   16109
15692   97672
78398   96505
64570   60454
85672   37902
32952   93797
72503   46027
62594   47340
14375   98105
98715   68010
68922   64392
14526   19785
47522   65951
58875   93129
90570   60939
36853   47817
72716   53863
64202   27240
18894   93797
16184   62664
18595   45090
68531   45090
29435   94452
48615   33033
90773   97672
65539   16964
37091   16124
85835   90759
93470   32585
71460   16320
67920   93186
71775   80052
31403   74944
68444   89107
57842   65832
12734   35998
14195   41229
63687   73891
19494   65967
28761   70887
64063   50742
90511   17620
47699   90759
99339   74495
64610   87080
30124   39683
44098   39683
92946   60454
63078   71049
69198   19253
81067   66283
27276   62664
60707   16124
92750   10833
15611   93797
80632   22536
81434   30050
31643   89107
12277   46282
94027   98547
65878   46619
88364   49881
66635   33033
70736   46184
94291   73977
96756   21223
74141   98354
99623   64392
47960   28999
67373   91119
44303   97672
24370   41607
41710   47340
27632   22536
14584   38508
12530   20728
99369   33033
47344   18768
69327   16541
51570   11107
29199   46184
16964   61946
95237   29761
22379   90759
85798   41199
25053   27180
61660   62664
41800   95048
16124   35616
63219   28270
81668   39683
75520   79737
99726   16081
38012   93797
38268   13109
92610   26416
27885   91119
21596   24011
67364   97672
75268   98704
91852   21659
62389   84076
10196   53612
92074   47123
29408   31697
21662   32940
13219   89107
13114   21512
48531   31805
71249   20665
67554   79621
86260   59516
87497   70849
16230   46623
38758   90759
84298   30050
84242   16124
95389   86465
77666   73820
79652   44579
83324   62664
53778   57216
43407   83484
99150   59782
16081   29507
73980   17620
63828   58641
47913   59516
67136   97390
97672   90734
98377   75087
36910   16109
61140   93797
69501   96356
91030   45251
37044   16124
71591   39683
87886   64392
51030   65967
12076   32551
82030   60454
28642   95851
45176   73711
44774   92783
35230   73053
85460   78500
95631   66420
77427   84991
26839   90759
98740   64392
55636   51814
14543   16109
71485   61007
78254   68306
28704   60454