2018 - Day 18 (Settlers of The North Pole)

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

#Advent of Code
#2018 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

def solve(puzzle_input):
    prev_area = puzzle_input[:]
    area = []
    for minute in range(10):
        area = []
        for y, row in enumerate(prev_area):
            area.append("")
            for x, col in enumerate(row):
                neighbours = [prev_area[y_offset][x_offset]
                              for x_offset in range(x-1,x+2)
                              for y_offset in range(y-1,y+2)
                              if  (x_offset, y_offset) != (x, y)
                              and 0 <= x_offset < len(prev_area[0])
                              and 0 <= y_offset < len(prev_area)]
                if col == "." and neighbours.count("|") >= 3:
                    area[-1] += "|"
                elif col == "|" and neighbours.count("#") >= 3:
                    area[-1] += "#"
                elif col == "#" and (neighbours.count("#") == 0 or neighbours.count("|") == 0):
                    area[-1] += "."
                else:
                    area[-1] += col
        prev_area = area[:]

    area = "".join(area)
    return area.count("|") * area.count("#")

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

    resource_value = solve(puzzle_input)

    print("The total resource value of the collection area after 10 minutes is " + str(resource_value) + ".")

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

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

#Advent of Code
#2018 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

def solve(puzzle_input):
    prev_area = puzzle_input[:]
    area = []
    seen = ["".join(puzzle_input)]
    for minute in range(1_000_000_000):
        area = []
        for y, row in enumerate(prev_area):
            area.append("")
            for x, col in enumerate(row):
                neighbours = [prev_area[y_offset][x_offset]
                              for x_offset in range(x-1,x+2)
                              for y_offset in range(y-1,y+2)
                              if  (x_offset, y_offset) != (x, y)
                              and 0 <= x_offset < len(prev_area[0])
                              and 0 <= y_offset < len(prev_area)]
                if col == "." and neighbours.count("|") >= 3:
                    area[-1] += "|"
                elif col == "|" and neighbours.count("#") >= 3:
                    area[-1] += "#"
                elif col == "#" and (neighbours.count("#") == 0 or neighbours.count("|") == 0):
                    area[-1] += "."
                else:
                    area[-1] += col
        prev_area = area[:]

        temp = "".join(area)
        if temp in seen:
            minute += 1

            seen = seen[seen.index(temp):]
            target = ((1_000_000_000 - minute) % len(seen))

            temp = seen[target]
            break
        else:
            seen.append(temp)

    return temp.count("|") * temp.count("#")

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

    resource_value = solve(puzzle_input)

    print("The total resource value of the collection area after 1,000,000,000 minutes is " + str(resource_value) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

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