2020 - Day 11 (Seating System)

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
#!/usr/bin/env python3

#Advent of Code
#2020 Day 11, 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 util.two_d_world

def solve(puzzle_input):
    vectors = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
    world = util.two_d_world.World(".")
    world.load_from_lists(puzzle_input, filter_func=lambda x: x != ".")

    # Precalculate neighbours
    neighbour_seats = {}
    for x, y in world.keys():
        neighbour_seats[(x, y)] = [(x + dx, y + dy) for dx, dy in vectors if (x + dx, y + dy) in world.keys()]

    def adjacent(x, y):
        return sum(world[(check_x, check_y)] == "#" for check_x, check_y in neighbour_seats[(x, y)])

    def step():
        output = {}
        changed = False
        for x, y in world.keys():
            neighbours = adjacent(x, y)
            if world[(x, y)] == "L" and neighbours == 0:
                output[(x, y)] = "#"
                changed = True
            elif world[(x, y)] == "#" and neighbours >= 4:
                output[(x, y)] = "L"
                changed = True
            else:
                output[(x, y)] = world[(x, y)]
        return output, changed

    while True:
        new_state, changed = step()
        if not changed:
            return sum(seat == "#" for seat in world.values())
        world.load_from_dict(new_state)

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

    seats = solve(puzzle_input)

    print("The number of occupied seats is " + str(seats) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["L.LL.LL.LL",
                                       "LLLLLLL.LL",
                                       "L.L.L..L..",
                                       "LLLL.LL.LL",
                                       "L.LL.LL.LL",
                                       "L.LLLLL.LL",
                                       "..L.L.....",
                                       "LLLLLLLLLL",
                                       "L.LLLLLL.L",
                                       "L.LLLLL.LL"]), 37)

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
#!/usr/bin/env python3

#Advent of Code
#2020 Day 11, 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 util.two_d_world

def solve(puzzle_input):
    vectors = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
    world = util.two_d_world.World(".")
    world.load_from_lists(puzzle_input, filter_func=lambda x: x != ".")

    def look_for_seat(x, y, dx, dy):
        if world.in_bounds(x, y):
            if (x, y) in world.keys():
                return (x, y)
            return look_for_seat(x + dx, y + dy, dx, dy)
        return None

    # Precalculate lines of sight
    visible_seats = {}
    for x, y in world.keys():
        visible_seats[(x, y)] = [result for dx, dy in vectors if (result := look_for_seat(x + dx, y + dy, dx, dy))]

    def visible(x, y):
        return sum(world[(check_x, check_y)] == "#" for check_x, check_y in visible_seats[(x, y)])

    def step():
        output = {}
        changed = False
        for x, y in world.keys():
            seen_seats = visible(x, y)
            if world[(x, y)] == "L" and seen_seats == 0:
                output[(x, y)] = "#"
                changed = True
            elif world[(x, y)] == "#" and seen_seats >= 5:
                output[(x, y)] = "L"
                changed = True
            else:
                output[(x, y)] = world[(x, y)]
        return output, changed

    while True:
        new_state, changed = step()
        if not changed:
            return sum(seat == "#" for seat in world.values())
        world.load_from_dict(new_state)

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

    seats = solve(puzzle_input)

    print("The number of occupied seats is " + str(seats) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["L.LL.LL.LL",
                                       "LLLLLLL.LL",
                                       "L.L.L..L..",
                                       "LLLL.LL.LL",
                                       "L.LL.LL.LL",
                                       "L.LLLLL.LL",
                                       "..L.L.....",
                                       "LLLLLLLLLL",
                                       "L.LLLLLL.L",
                                       "L.LLLLL.LL"]), 26)

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