2023 - Day 18 (Lavaduct Lagoon)

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3

#Advent of Code
#2023 Day 18, 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

from collections import deque
import re

def solve(puzzle_input: list[str]) -> int:
    instruction_pattern: re.Pattern = re.compile(r"([UDLR]) (\d+) \((#[0-9a-f]{6})\)")

    hole: set[tuple[int, int]] = {(0, 0)}
    current_x, current_y = (0, 0)
    min_x, min_y = 0, 0
    max_x, max_y = 0, 0

    for line in puzzle_input:
        direction, distance, colour = instruction_pattern.match(line).groups()
        distance = int(distance)
        match direction:
            case "R": dx, dy = 1, 0
            case "D": dx, dy = 0, 1
            case "L": dx, dy = -1, 0
            case "U": dx, dy = 0, -1

        for steps in range(1, distance + 1):
            hole.add((current_x + (dx * steps), current_y + (dy * steps)))

        current_x += dx * distance
        current_y += dy * distance
        min_x = min(min_x, current_x)
        max_x = max(max_x, current_x)
        min_y = min(min_y, current_y)
        max_y = max(max_y, current_y)

    # Give one cube border on all sides to allow flood fill to not get trapped
    min_x -= 1
    min_y -= 1
    max_x += 1
    max_y += 1

    outside: set[tuple[int, int]] = set()
    queue: deque[tuple[int, int]] = deque([(min_x, min_y)])
    while queue:
        current_x, current_y = queue.popleft()
        outside.add((current_x, current_y))

        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            if min_x <= current_x + dx <= max_x and min_y <= current_y + dy <= max_y:
                if  (current_x + dx, current_y + dy) not in hole\
                and (current_x + dx, current_y + dy) not in outside\
                and (current_x + dx, current_y + dy) not in queue:
                    queue.append((current_x + dx, current_y + dy))

    total_area = (max_x + 1 - min_x) * (max_y + 1 - min_y)
    outside_area = len(outside)

    return total_area - outside_area

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

    return "The number of cubic metres of lava the lagoon could hold is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["R 6 (#70c710)",
                                       "D 5 (#0dc571)",
                                       "L 2 (#5713f0)",
                                       "D 2 (#d2c081)",
                                       "R 2 (#59c680)",
                                       "D 2 (#411b91)",
                                       "L 5 (#8ceee2)",
                                       "U 2 (#caa173)",
                                       "L 1 (#1b58a2)",
                                       "U 2 (#caa171)",
                                       "R 2 (#7807d2)",
                                       "U 3 (#a77fa3)",
                                       "L 2 (#015232)",
                                       "U 2 (#7a21e3)"]), 62)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3

#Advent of Code
#2023 Day 18, 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

import re

def solve(puzzle_input: list[str]) -> int:
    instruction_pattern: re.Pattern = re.compile(r"([UDLR]) (\d+) \((#[0-9a-f]{6})\)")

    # I didn't know how to approach part 2 of this puzzle, and so read some
    # discussions between some friends where they mentioned both Pick's algorithm
    # and the Shoelace formula. I hadn't heard of either of these before today,
    # so thank you, Day 18, for a learning opportunity!
    #
    # Pick's theorem: https://en.wikipedia.org/wiki/Pick%27s_theorem
    # Shoelace formula: https://en.wikipedia.org/wiki/Shoelace_formula

    vertices: list[tuple[int, int]] = [(0, 0)]
    current_x, current_y = (0, 0)
    perimeter_length = 1

    for line in puzzle_input:
        _, _, colour = instruction_pattern.match(line).groups()
        distance = int(colour[1:6], 16)
        match colour[6]:
            case "0": dx, dy = 1, 0
            case "1": dx, dy = 0, 1
            case "2": dx, dy = -1, 0
            case "3": dx, dy = 0, -1

        current_x = current_x + (dx * distance)
        current_y = current_y + (dy * distance)
        vertices.append((current_x, current_y))
        perimeter_length += distance

    # Below is a slightly modified form of Pick's theorem
    # Normally, this is stated as
    # A = i + b/2 - 1
    # A := area
    # i := number of integer points on the interior
    # b := number of integer points on the boundary
    #
    # (The following was determined after reading through
    # some discussions on the subreddit:)
    # The number of boundary points is counted above as
    # the vertices are found (perimeter_length), and the
    # area is the result of the Shoelace formula. However,
    # the answer we are trying to find is actually i + b.
    # Therefore, we can rearrange the Pick's theorem as follows:
    #
    # A = i + b/2 - 1
    # A + 1 = i + b/2
    # A + b/2 + 1 = i + b/2 + b/2
    # i + b = A + b/2 + 1

    return int(0.5 * sum(
        (xi * yi1) - (xi1 * yi)
        for (xi, yi), (xi1, yi1) in zip(vertices, vertices[1:])
    )) + int(0.5 * perimeter_length) + 1

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

    return "The number of cubic metres of lava the lagoon could hold is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["R 6 (#70c710)",
                                       "D 5 (#0dc571)",
                                       "L 2 (#5713f0)",
                                       "D 2 (#d2c081)",
                                       "R 2 (#59c680)",
                                       "D 2 (#411b91)",
                                       "L 5 (#8ceee2)",
                                       "U 2 (#caa173)",
                                       "L 1 (#1b58a2)",
                                       "U 2 (#caa171)",
                                       "R 2 (#7807d2)",
                                       "U 3 (#a77fa3)",
                                       "L 2 (#015232)",
                                       "U 2 (#7a21e3)"]), 952408144115)

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