Advent of Code 2024

AOC Day 01

Description

--- Day 1: Historian Hysteria ---

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:

The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
The third-smallest number in both lists is 3, so the distance between them is 0.
The next numbers to pair up are 3 and 4, a distance of 1.
The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.

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?

input.txt

63721   98916
83871   23584
55026   62690
21948   69139
11201   33499
96077   62705
41231   87261
71203   13285
53280   86974
31172   11658
56516   98916
59380   19138
96772   61094
63969   69126
20073   82512
20834   38571
92030   96010
84089   39901
18339   22036
28056   25937
75715   99430
70846   98374
56817   62289
97037   22364
32411   51681
41809   98156
76944   37563
58189   69139
21939   82512
45097   86974
62705   26019
95791   61094
31801   84428
69238   88614
39850   13908
26002   39901
64599   33304
44590   98374
78335   51888
82512   52047
57887   78631
27860   51468
72024   12311
47520   51468
97347   59780
13728   65706
52302   74556
33471   98916
14163   40376
20346   83807
80912   17961
95998   65684
32219   24194
46236   13285
98428   88835
40577   61608
91716   98121
60156   70703
54527   51961
42782   12309
42240   83795
15918   50087
42622   14360
22114   99603
87281   54967
91581   99603
52780   23792
46822   82512
30877   29405
33424   11774
32272   82512
78679   62052
36612   29914
42681   61094
48481   52039
69769   62690
64189   40376
54255   66788
54850   66572
12528   22130
86174   40376
65976   80500
63915   28775
66788   83807
83422   66572
90402   99603
85348   80418
14682   98916
45822   28775
13157   20346
38402   31258
80935   10753
65070   20317
32676   34871
31427   15594
42781   47864
55644   44957
30989   42965
38756   62052
39293   97881
30968   62690
56882   29405
65462   68541
76422   50715
34547   24194
72282   62690
92342   42296
25897   20849
14452   82122
75858   15046
75577   85634
20594   77936
39912   68541
69139   31502
64342   61039
71898   31258
53098   20346
39892   37035
10887   98374
97900   94957
67382   97046
10705   40376
68234   89731
51652   54595
51440   62705
97584   97240
21827   63629
96359   84707
15434   61734
93030   97129
56574   67650
24020   67035
23745   68753
40655   66572
97113   16063
81714   15465
27278   54255
36489   66572
30802   98374
26081   72575
61525   18852
82667   66788
73634   90961
64265   61589
21183   62690
77550   68541
29208   12408
54092   85880
40518   54255
89148   50707
50039   64048
38834   73718
62607   75650
15015   78049
97975   29405
92080   42574
83720   22415
93384   69139
40696   44729
97526   23792
61734   92736
38878   98762
20162   77446
77727   69139
27458   18662
36125   20197
75944   18725
86346   22364
44849   75485
65394   60670
39257   34871
17370   35661
61046   66572
73445   78735
23792   95066
33670   28775
31800   69139
37622   60352
34456   98916
82819   39901
36275   77446
71616   33673
14730   28900
62052   60624
55307   34871
75279   74526
89572   31258
66278   27816
84982   57963
99221   19953
86616   56531
31692   98916
51468   24249
48579   38018
52462   97340
80487   28775
99371   35813
70905   66987
39381   33563
62589   23792
24144   83850
16708   29405
43869   28900
39310   53542
85314   15046
95478   28900
61190   98374
21175   82122
13285   72243
99081   70483
10815   39901
93293   86144
95585   98933
65731   50087
99562   23792
93416   66572
49380   22364
95099   29405
94262   62052
50620   98916
16427   34871
18103   13285
17044   86336
35640   61734
27106   34871
99272   28807
68345   44179
47215   14224
99510   34186
59344   66788
56087   23535
54047   80026
18016   63772
72531   71440
59402   40376
92026   34871
56899   79559
45736   36788
43217   91863
77064   98157
56018   20317
76327   28900
72821   19407
63500   62937
68547   66572
42193   37130
81823   22364
72027   82450
59894   57408
22894   26775
49624   34871
85677   13285
59165   28469
15814   98374
18197   93778
48375   14885
94778   98916
86385   97997
41779   73577
52971   34871
91842   61256
22568   61256
14237   54358
76119   55268
54946   28775
71933   65810
86928   24194
10238   98916
80990   69427
28001   39901
47286   32918
70609   66079
15760   36747
36567   94015
11786   34931
18890   78631
46903   39901
50399   74710
89746   66572
31258   50430
49217   99603
71388   98374
65381   63073
26294   20317
97188   98374
36571   22130
68437   35813
40142   31258
51693   29405
29472   67550
85983   99480
83807   38797
36204   40376
22611   40376
66861   24683
86715   62690
24390   12106
78554   74773
16233   91468
46647   17362
18129   98916
74379   24194
39901   94180
28130   89936
15255   53500
90359   98374
36114   24194
69761   78631
40961   96415
32323   35661
72742   53446
18224   51468
79717   66788
63337   62052
83257   77446
70340   20417
37092   23073
11465   91804
93712   51468
70424   20346
79670   47774
53326   99603
61413   35813
28509   77446
10392   51646
20411   79767
10834   34900
46987   77824
84749   14347
64003   60388
37510   23792
67074   15046
86583   22130
74774   17706
27254   32069
24780   62690
74592   66572
19956   50397
87787   78631
27551   86335
27313   62690
11764   13285
16566   68541
52387   43204
32314   30981
50548   23792
66572   98374
23813   14490
10031   73014
98916   99603
85669   51468
41290   31893
79434   77446
41146   75460
21093   31258
22150   39901
77726   76376
86750   35661
52067   34432
22075   52433
80197   29405
43183   40376
48981   51468
49114   65262
17821   83795
66079   72103
61094   54261
22863   86785
79221   35813
37160   86690
43323   60348
27816   98916
23797   13285
52087   83795
69710   62690
37097   62690
58946   35813
25528   78631
95945   39426
10344   83292
25204   22035
20851   44476
49675   18087
60917   98916
38511   66788
50625   13566
92213   42204
14434   35517
16747   79017
98256   82122
13429   23798
10505   22364
56299   51468
11971   98374
82974   95791
71227   93187
17329   20899
86233   48880
64887   13285
67572   72363
42339   16540
37301   88128
99216   13285
21173   13285
39433   80508
56739   98916
19158   66788
99366   97494
78631   76828
64445   49058
84071   27319
76591   94653
72285   39901
86827   68541
97195   50087
94820   39901
51240   66572
19704   13285
82252   54471
90511   81325
39890   82624
28900   23792
89837   29405
27432   20051
12464   68541
51911   49067
70069   51468
38185   96194
61979   61734
44809   34871
50816   74433
88128   62052
68302   11847
65790   14562
18639   61545
43973   50325
32245   71996
32770   78631
80007   24194
81848   17710
48925   27816
36951   78631
44300   83864
39815   88563
33287   87639
81599   40376
83780   34010
76105   37309
66186   57599
30330   15408
85410   76902
32775   62690
96368   35661
36267   31258
63258   50558
29106   28298
82546   62690
11099   61734
78293   25816
96305   15046
29590   35661
62248   69139
28271   78631
28395   61256
64484   97925
25386   35813
27975   55981
62690   55244
36937   36761
14288   78631
58395   15040
71622   20346
29580   10650
19543   29405
91211   62052
67129   23792
71075   89904
35814   78631
24320   66399
68616   62052
81607   13285
16440   69113
20317   34871
34990   86694
46814   17420
48773   18642
16983   12462
76286   22364
43760   50461
89012   98374
76734   83795
82889   81347
94844   83039
87086   42228
79066   34871
60930   62690
14671   98362
15046   98916
23105   64417
15963   26384
17578   66788
19749   35813
73717   77781
55768   78631
81328   86974
98374   30582
74371   98916
62198   66788
18216   29620
83718   35167
97216   66806
21853   23792
95659   45644
54690   19553
99854   54434
60953   82122
29405   20346
77446   61734
74477   61881
59384   34871
22201   82512
41680   22364
70388   33526
79474   35813
95676   61734
81066   86974
50184   69139
92334   73193
63301   74886
88694   25546
29122   40376
35513   61734
34871   51468
27159   78631
92093   22364
97523   46887
75320   75413
50553   66788
69335   39901
31428   36441
63138   38796
65691   66589
18442   61734
95290   40828
30463   84566
13760   20346
63642   38111
38068   99603
88985   51468
86911   13890
81763   98405
60314   34871
79229   98374
54222   37457
41191   35661
94686   14344
48723   40376
53331   51468
82320   32023
13879   64243
77615   78631
21373   81177
99603   28900
74982   17203
61957   82512
49736   29405
71158   16258
46734   68257
91305   13285
15307   77446
88888   75540
96953   82122
73049   34871
25806   66509
28775   22364
88849   84256
73085   31258
82002   45833
92219   61094
94438   62690
74931   10907
19013   56348
61330   51468
45424   32416
85351   51468
43176   78631
92511   31258
74316   50766
52865   80456
90670   19338
60903   50287
13027   23792
70012   37373
28645   95137
29890   52066
90210   22364
83928   91550
84224   95653
52748   22364
25385   89306
29258   61754
22425   81852
83606   23792
86286   33987
78304   78631
46454   77446
68130   78631
84341   34871
32536   98374
46176   51468
18381   52328
74842   12157
99320   34871
36587   51215
65504   11768
62256   20755
62433   28900
18797   64268
46028   56766
34433   39901
55237   69139
67094   83807
59294   29927
18052   66788
37438   65122
89900   22130
86138   66808
17409   61734
41410   77446
36675   98987
93185   40326
68737   97123
12420   28900
28431   13285
79903   61734
85029   49719
87114   38161
10277   97814
91030   77446
60032   40376
26941   71642
40711   62052
66007   98916
36232   22130
77300   66788
82122   74191
93403   86974
10869   26173
32606   39901
36002   90403
37936   61256
36223   14536
90084   98370
13588   29405
92588   23792
76528   68541
91855   98758
65053   77446
31950   15046
20160   83646
98329   13285
59162   20346
78821   86974
73311   15046
83143   14519
61256   47938
68103   43062
62427   72651
50329   77446
93857   91804
50509   31563
12726   98916
91094   51468
62068   97829
88270   51468
98814   42679
96608   66788
21920   22364
85592   59267
56819   39901
60434   69139
68767   20590
23123   31258
64662   78921
26559   45234
72799   82122
83414   62705
35661   22364
22499   53665
90831   52136
31956   56969
53393   20346
30160   91715
40872   68541
96530   20724
73972   46762
23784   77789
51604   14275
24516   33815
51694   36170
54999   40276
71340   77446
33559   50566
97770   86974
57181   98916
11456   95791
43991   35813
39647   99603
31291   48238
24411   66572
50087   15046
36907   81744
84965   43494
80699   26639
70287   28114
30928   62052
29829   27652
45493   29405
96002   68340
86510   83248
58569   14674
95602   83044
60548   62973
87844   29873
64650   24194
91993   61256
25193   12039
93597   69445
44397   75998
16429   68541
36998   98916
46382   35813
73943   56489
18980   38254
52184   29113
27861   53315
78208   52158
39137   95325
71411   96871
11297   50795
24795   10762
45672   85223
20458   45112
68342   86974
48555   68541
14057   89657
45348   83795
69091   85340
32017   91553
47365   29405
69846   42783
42794   66079
55767   35661
79443   18679
97731   28900
78728   46714
51662   61502
86974   40376
66784   69988
21713   26696
67541   89573
63234   48739
75844   11283
72169   63197
68116   27816
39756   58055
49060   52912
86033   20346
77374   32945
54054   15032
10242   66300
39964   39901
16823   98374
35949   64043
45790   71364
85734   62690
39953   67342
19397   36827
30706   47034
62392   27816
36736   76359
41284   27040
91564   18615
27419   49560
35658   83795
88035   27816
58920   61094
24194   53105
41569   61734
88977   67702
59178   31337
91804   24194
68538   51029
45699   22539
64646   39901
49570   66788
42702   66572
10875   16521
54579   34871
30993   82512
46568   99603
36007   61734
41554   40376
28737   86974
47642   54509
95772   28900
91043   31258
84273   98374
75931   71232
40158   35777
40376   95791
52356   68460
63119   77046
85547   24461
48564   15619
61344   14922
20517   68541
69816   52014
33687   82149
39886   57790
85582   77421
16170   48400
32839   24194
59842   14809
40767   22364
14572   86974
94812   29405
29781   20317
25093   51468
22523   15286
69772   49833
24511   89853
95422   63827
18619   62557
71465   56921
80392   23792
75031   22130
88990   61046
62453   63513
10363   61094
33426   40376
34338   98916
19106   22364
16632   21897
93602   80485
70657   83807
87290   93753
48905   69139
19137   58766
74790   78631
17479   66515
59784   40376
35921   69139
63281   99603
49035   54255
42278   49216
88656   61094
18166   68541
15171   27816
25018   66572
58709   66788
26678   52650
51627   82122
71638   28900
52703   93929
54658   29405
63402   77446
62250   36480
20727   96945
66177   13629
11052   95791
16012   70360
14890   58523
68314   61256
27581   61256
68541   66572
96645   99603
79922   66079
45897   37920
46727   62690
92327   50087
97244   19990
97604   92430
10996   21728
54944   66788
51431   75661
44593   70853
57290   22130
44100   51468
88066   95791
64545   62690
15185   98374
81005   83795
24581   59570
18803   39901
31017   35731
52752   16550
55973   83795
41888   77446
54026   23792
45685   23792
39267   15380
74192   99479
48209   75402
43467   78806
83468   68541
22364   44940
22179   52910
61634   23792
45998   72739
41096   35813
17074   39901
34245   99603
38149   66572
87211   34871
39320   39901
35813   69139
72525   29405
17308   20346
26336   35661
16331   31258
18176   61256
28796   86974
91989   40376
76266   72180
79522   97407
46148   98374
91891   90085
29539   88770
97919   99153
14625   62052
33094   31225
75423   62690
64206   66079
91522   17031
36889   86974
98905   35912
67253   22364
86480   62690
47746   68541
54727   40171
39202   68929
83795   17632
19065   77446
73523   79097
63938   59651
64774   22364
86179   83795
19996   75565
49609   66760
22417   31817
90246   15046
25158   27060
27237   61256
20154   11191
80548   61256
83429   39901
82411   39901
36646   99815
89469   34241
69714   73572
17832   65920
98578   62023
26264   78372
23951   10088
91003   39552
14615   99603
78726   83194
31384   44477
22130   45848
83121   81284
87043   42282
25669   40376
61080   98374
29216   68226
19808   31190
12025   77446
49018   46070
71086   35813
26776   48041
94075   35661
51192   34871
18698   61094
77232   66079
18652   44005
47380   86974
91250   69139
54224   23792
22462   23279
96949   25734
18113   60075
76499   35842
13733   89585
47074   51199
30881   61256
41926   82925
51892   88913
56351   49034
39241   85194
43972   20620

main.test.ts

import { distance, parseLists, part1, part2 } from "./main";

describe("Day 01", () => {
    describe("distance", () => {
        it('checks distances', () => {
            expect(distance(1, 3)).toBe(2);
            expect(distance(2, 3)).toBe(1);
            expect(distance(3, 3)).toBe(0);
            expect(distance(3, 4)).toBe(1);
            expect(distance(3, 5)).toBe(2);
            expect(distance(4, 9)).toBe(5);
        })
    })

    describe("parseLists", () => {
        it('parses lists', () => {
            const result = parseLists(`3   4
4   3
2   5
1   3
3   9
3   3`)

            const [list1, list2] = result;
            expect(list1).toEqual([3, 4, 2, 1, 3, 3])
            expect(list2).toEqual([4, 3, 5, 3, 9, 3])
        })
    })

    describe("part1", () => {
        it('solves the problem', () => {
            const solution = part1(`3   4
4   3
2   5
1   3
3   9
3   3`)
            expect(solution).toBe(11);
        });
    })

    describe("part2", () => {
        it('solves the problem', () => {
            const solution = part2(`3   4
4   3
2   5
1   3
3   9
3   3`)
            expect(solution).toBe(31);
        });
    })
})

main.ts

import { readFileSync } from "fs";

// Generic Utilities
function zip<T>(arr1: T[], arr2: T[]): [T, T][] {
    return arr1.map((item, index) => [item, arr2[index]]);
}

function sum(total: number, entry: number): number {
    return total + entry
}

function solve<T>(inputFile: string, solution: (input: string) => T): T {
    const data = readFileSync(inputFile, "utf-8").trim()
    const solved = solution(data)
    return solved
}

// Functions for this problem
export function distance(a: number, b: number): number {
    return Math.abs(a - b)
}

function parseNumbers(line: string): number[] {
    return line.split(/\s+/).map(num => parseInt(num))
}

function buildLists(prevLists: [number[], number[]], numbers: number[]): [number[], number[]] {
    const [first, second] = numbers
    return [
        [...prevLists[0], first],
        [...prevLists[1], second]
    ]
}

export function parseLists(input: string): [number[], number[]] {
    return input.trim().split("\n").map(parseNumbers).reduce(buildLists, [[], []])
}

// Solutions
export function part1(input: string): number {
    let [list1, list2] = parseLists(input);

    list1.sort()
    list2.sort()

    return zip(list1, list2).map(([a, b]) => distance(a, b)).reduce(sum)
}

// Part 2

function entryCount(value: number, otherList: number[]): number {
    return otherList.filter(v => v == value).length
}

export function part2(input: string): number {
    let [list1, list2] = parseLists(input);

    return list1.map(i => i * entryCount(i, list2)).reduce(sum)
}

console.log("Part 1: ", solve("src/day-01/input.txt", part1)) //?
console.log("Part 2: ", solve("src/day-01/input.txt", part2)) //?