2024 - Day 1 (Historian Hysteria)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

#Advent of Code
#2024 Day 1, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

import re

def solve(puzzle_input: list[str]) -> int:
    left: list[int] = []
    right: list[int] = []

    for line in puzzle_input:
        left_n, right_n = re.match(r"(\d+)\s+(\d+)", line).groups()
        left.append(int(left_n))
        right.append(int(right_n))

    return sum(abs(l - r) for l, r in zip(sorted(left), sorted(right)))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The total distance between the lists is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["3   4",
                                       "4   3",
                                       "2   5",
                                       "1   3",
                                       "3   9",
                                       "3   3"]), 11)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3

#Advent of Code
#2024 Day 1, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import defaultdict
import re

def solve(puzzle_input: list[str]) -> int:
    left: list[int] = []
    right: defaultdict[int, int] = defaultdict(int)

    for line in puzzle_input:
        left_n, right_n = re.match(r"(\d+)\s+(\d+)", line).groups()
        left.append(int(left_n))
        right[int(right_n)] += 1

    return sum(l * right[l] for l in left)

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The similarity score of the lists is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["3   4",
                                       "4   3",
                                       "2   5",
                                       "1   3",
                                       "3   9",
                                       "3   3"]), 31)

if __name__ == "__main__":
    run(main)
Return to solution index