2022 - Day 23 (Unstable Diffusion)

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3

#Advent of Code
#2022 Day 23, 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 defaultdict
from math import inf

def solve(puzzle_input):
    class Elf:
        elves = []
        positions = set()
        proposals = defaultdict(int)
        min_x = min_y = inf
        max_x = max_y = -inf

        directions = [0, 1, 2, 3]
        round_num = 0

        @classmethod
        def round(cls):
            # Proposal phase
            for elf in cls.elves:
                elf.propose()

            # Movement phase
            cls.positions = set()
            cls.min_x = cls.min_y = inf
            cls.max_x = cls.max_y = -inf
            for elf in cls.elves:
                elf.move()

                Elf.positions.add((elf.x, elf.y))
                if elf.x < Elf.min_x: Elf.min_x = elf.x
                if elf.x > Elf.max_x: Elf.max_x = elf.x
                if elf.y < Elf.min_y: Elf.min_y = elf.y
                if elf.y > Elf.max_y: Elf.max_y = elf.y

            # Cleanup
            cls.proposals.clear()
            cls.round_num += 1

        @classmethod
        def get_area(cls):
            return (cls.max_x - cls.min_x + 1) * (cls.max_y - cls.min_y + 1)

        def __init__(self, x, y):
            self.x = x
            self.y = y
            self.proposal = None

            Elf.elves.append(self)
            Elf.positions.add((x, y))
            if x < Elf.min_x: Elf.min_x = x
            if x > Elf.max_x: Elf.max_x = x
            if y < Elf.min_y: Elf.min_y = y
            if y > Elf.max_y: Elf.max_y = y

        def get_neighbours(self):
            return [neighbour for neighbour in [(self.x + dx, self.y + dy) for dx in range(-1, 2) for dy in range(-1, 2) if not (dx == 0 and dy == 0)] if neighbour in Elf.positions]

        def register_proposal(self, next_x, next_y):
            self.proposal = (next_x, next_y)
            Elf.proposals[(next_x, next_y)] += 1

        def propose(self):
            if not (neighbours := self.get_neighbours()):
                return

            for dir_check in range(4):
                match Elf.directions[(Elf.round_num + dir_check) % 4]:
                    case 0:
                        # North
                        if all(not (self.x + dx, self.y - 1) in neighbours for dx in range(-1, 2)):
                            return self.register_proposal(self.x, self.y - 1)
                    case 1:
                        # South
                        if all(not (self.x + dx, self.y + 1) in neighbours for dx in range(-1, 2)):
                            return self.register_proposal(self.x, self.y + 1)
                    case 2:
                        # West
                        if all(not (self.x - 1, self.y + dy) in neighbours for dy in range(-1, 2)):
                            return self.register_proposal(self.x - 1, self.y)
                    case 3:
                        # East
                        if all(not (self.x + 1, self.y + dy) in neighbours for dy in range(-1, 2)):
                            return self.register_proposal(self.x + 1, self.y)

        def move(self):
            if self.proposal != None and Elf.proposals[self.proposal] == 1:
                self.x, self.y = self.proposal
            self.proposal = None

    for y, row in enumerate(puzzle_input):
        for x, col in enumerate(row):
            if col == "#":
                Elf(x, y)

    for r in range(10):
        Elf.round()

    return Elf.get_area() - len(Elf.elves)

def main():
    puzzle_input = util.read.as_lines()

    tiles = solve(puzzle_input)

    print("The number of empty ground tiles that the rectangle contains is " + str(tiles) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([".....",
                                       "..##.",
                                       "..#..",
                                       ".....",
                                       "..##.",
                                       "....."]), 25)

    def test_ex2(self):
        return self.assertEqual(solve(["....#..",
                                       "..###.#",
                                       "#...#.#",
                                       ".#...##",
                                       "#.###..",
                                       "##.#.##",
                                       ".#..#.."]), 110)

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3

#Advent of Code
#2022 Day 23, 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
from math import inf

def solve(puzzle_input):
    class Elf:
        elves = []
        positions = set()
        proposals = defaultdict(int)

        directions = [0, 1, 2, 3]
        round_num = 0

        @classmethod
        def round(cls):
            # Proposal phase
            for elf in cls.elves:
                elf.propose()

            # Movement phase
            cls.positions = set()
            cls.min_x = cls.min_y = inf
            cls.max_x = cls.max_y = -inf
            elf_moved = False
            for elf in cls.elves:
                this_elf_moved = elf.move()
                elf_moved = elf_moved or this_elf_moved

                Elf.positions.add((elf.x, elf.y))

            # Cleanup
            cls.proposals.clear()
            cls.round_num += 1

            return elf_moved

        def __init__(self, x, y):
            self.x = x
            self.y = y
            self.proposal = None

            Elf.elves.append(self)
            Elf.positions.add((x, y))

        def get_neighbours(self):
            return [neighbour for neighbour in [(self.x + dx, self.y + dy) for dx in range(-1, 2) for dy in range(-1, 2) if not (dx == 0 and dy == 0)] if neighbour in Elf.positions]

        def register_proposal(self, next_x, next_y):
            self.proposal = (next_x, next_y)
            Elf.proposals[(next_x, next_y)] += 1

        def propose(self):
            if not (neighbours := self.get_neighbours()):
                return

            for dir_check in range(4):
                match Elf.directions[(Elf.round_num + dir_check) % 4]:
                    case 0:
                        # North
                        if all(not (self.x + dx, self.y - 1) in neighbours for dx in range(-1, 2)):
                            return self.register_proposal(self.x, self.y - 1)
                    case 1:
                        # South
                        if all(not (self.x + dx, self.y + 1) in neighbours for dx in range(-1, 2)):
                            return self.register_proposal(self.x, self.y + 1)
                    case 2:
                        # West
                        if all(not (self.x - 1, self.y + dy) in neighbours for dy in range(-1, 2)):
                            return self.register_proposal(self.x - 1, self.y)
                    case 3:
                        # East
                        if all(not (self.x + 1, self.y + dy) in neighbours for dy in range(-1, 2)):
                            return self.register_proposal(self.x + 1, self.y)

        def move(self):
            moving = False
            if self.proposal != None and Elf.proposals[self.proposal] == 1:
                self.x, self.y = self.proposal
                moving = True
            self.proposal = None
            return moving

    for y, row in enumerate(puzzle_input):
        for x, col in enumerate(row):
            if col == "#":
                Elf(x, y)

    r = 1
    while True:
        if not Elf.round():
            return r
        r += 1

def main():
    puzzle_input = util.read.as_lines()

    r = solve(puzzle_input)

    print("The number of the first round where no elf moves is " + str(r) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["....#..",
                                       "..###.#",
                                       "#...#.#",
                                       ".#...##",
                                       "#.###..",
                                       "##.#.##",
                                       ".#..#.."]), 20)

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