2017 - Day 22 (Sporifica Virus)

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

#Advent of Code
#2017 Day 22, 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, steps=10000):
    count = 0
    grid = puzzle_input[:]
    direction = "u"
    x = int(len(grid) / 2)
    y = int(len(grid) / 2)

    left  = {"u": "l",
             "l": "d",
             "d": "r",
             "r": "u"}
    right = {"u": "r",
             "r": "d",
             "d": "l",
             "l": "u"}

    for s in range(steps):
        if grid[y][x] == ".":
            direction = left[direction]
            grid[y] = grid[y][:x] + "#" + grid[y][x+1:]
            count += 1
        else:
            direction = right[direction]
            grid[y] = grid[y][:x] + "." + grid[y][x+1:]

        if direction == "u":
            if y == 0:
                grid.insert(0, "."*len(grid[0]))
                y += 1
            y -= 1
        elif direction == "d":
            if y == len(grid) - 1:
                grid.append("."*len(grid[0]))
            y += 1
        elif direction == "l":
            if x == 0:
                for l in range(len(grid)):
                    grid[l] = "." + grid[l]
                x += 1
            x -= 1
        elif direction == "r":
            if x == len(grid[0]) - 1:
                for l in range(len(grid)):
                    grid[l] = grid[l] + "."
            x += 1

    return count

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

    infections = solve(puzzle_input)

    print("The number of bursts of activity that caused an infection is " + str(infections) + ".")

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

    def test_ex2(self):
        self.assertEqual(solve(["..#",
                                "#..",
                                "..."], 70), 41)

    def test_ex3(self):
        self.assertEqual(solve(["..#",
                                "#..",
                                "..."], 10000), 5587)

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

#Advent of Code
#2017 Day 22, 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, steps=10000000):
    count = 0
    grid = puzzle_input[:]
    direction = "u"
    x = int(len(grid) / 2)
    y = int(len(grid) / 2)

    left  = {"u": "l",
             "l": "d",
             "d": "r",
             "r": "u"}
    right = {"u": "r",
             "r": "d",
             "d": "l",
             "l": "u"}
    rev   = {"u": "d",
             "d": "u",
             "l": "r",
             "r": "l"}

    for s in range(steps):
        if grid[y][x] == ".":
            direction = left[direction]
            grid[y] = grid[y][:x] + "W" + grid[y][x+1:]
        elif grid[y][x] == "W":
            grid[y] = grid[y][:x] + "#" + grid[y][x+1:]
            count += 1
        elif grid[y][x] == "#":
            direction = right[direction]
            grid[y] = grid[y][:x] + "F" + grid[y][x+1:]
        else:
            direction = rev[direction]
            grid[y] = grid[y][:x] + "." + grid[y][x+1:]


        if direction == "u":
            if y == 0:
                grid.insert(0, "."*len(grid[0]))
                y += 1
            y -= 1
        elif direction == "d":
            if y == len(grid) - 1:
                grid.append("."*len(grid[0]))
            y += 1
        elif direction == "l":
            if x == 0:
                for l in range(len(grid)):
                    grid[l] = "." + grid[l]
                x += 1
            x -= 1
        elif direction == "r":
            if x == len(grid[0]) - 1:
                for l in range(len(grid)):
                    grid[l] = grid[l] + "."
            x += 1

    return count

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

    infections = solve(puzzle_input)

    print("The number of bursts of activity that caused an infection is " + str(infections) + ".")

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

    def test_ex2(self):
        self.assertEqual(solve(["..#",
                                "#..",
                                "..."], 10000000), 2511944)

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