2024 - Day 2 (Red-Nosed Reports)

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
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3

#Advent of Code
#2024 Day 2, 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

def solve(puzzle_input: list[str]) -> int:
    def is_safe(report: list[int]) -> bool:
        # None if not yet determined
        ascending: bool | None = None

        for a, b in zip(report, report[1:]):
            if ascending is None:
                ascending = a < b

            if a == b:
                return False
            elif ascending and a > b:
                return False
            elif not ascending and a < b:
                return False
            elif not (-3 <= b - a <= 3):
                return False

        # If no delta between adjacent pairs of numbers
        # fails any test, then the report is safe
        return True

    return len(list(filter(is_safe, [[int(x) for x in line.split()] for line in puzzle_input])))

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

    return "The number of safe reports is {}.", solve(puzzle_input)

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

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3

#Advent of Code
#2024 Day 2, 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

def solve(puzzle_input: list[str]) -> int:
    def is_safe_inner(report: list[int], discounted_level: int | None) -> bool:
        adjusted_report: list[int] = [x for i, x in enumerate(report) if i != discounted_level]

        # None if not yet determined
        ascending: bool | None = None

        for i, (a, b) in enumerate(zip(adjusted_report, adjusted_report[1:])):
            if ascending is None:
                ascending = a < b

            if a == b:
                return False
            elif ascending and a > b:
                return False
            elif not ascending and a < b:
                return False
            elif not (-3 <= b - a <= 3):
                return False

        # If no delta between adjacent pairs of numbers
        # fails any test, then the report is safe
        return True

    def is_safe(report: list[int]) -> bool:
        # Check from -1 (do not discount any levels) to end of report
        return any(is_safe_inner(report, i) for i in range(-1, len(report)))

    return len(list(filter(is_safe, [[int(x) for x in line.split()] for line in puzzle_input])))

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

    return "The number of safe reports is {}.", solve(puzzle_input)

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

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