2019 - Day 10 (Monitoring Station)

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

#Advent of Code
#2019 Day 10, 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 fractions import Fraction

def solve(puzzle_input):
    width  = len(puzzle_input[0])
    height = len(puzzle_input)

    asteroids = [(x, y) for x in range(width) for y in range(height) if puzzle_input[y][x] == "#"]
    detections = {}

    for x in range(width):
        for y in range(height):
            if puzzle_input[y][x] == ".":
                continue

            detections[(x, y)] = 0
            for tx, ty in asteroids:
                if (x, y) == (tx, ty):
                    continue

                if x == tx:
                    dy, dx = 1, 0
                else:
                    f = Fraction(ty-y, tx-x)
                    dy, dx = abs(f.numerator), abs(f.denominator)
                if tx < x:
                    dx *= -1
                if ty < y:
                    dy *= -1

                a, b = x + dx, y + dy
                while (0 <= a < width) and (0 <= b < height):
                    if puzzle_input[b][a] == "#":
                        if (a, b) == (tx, ty):
                            detections[(x, y)] = detections[(x, y)] + 1
                        break
                    a += dx
                    b += dy

    return max(detections.values())

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

    asteroids = solve(puzzle_input)

    print("The number of other asteroids can be detected from the best location is " + str(asteroids) + ".")

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

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

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

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

    def test_ex5(self):
        self.assertEqual(solve([".#..##.###...#######",
                                "##.############..##.",
                                ".#.######.########.#",
                                ".###.#######.####.#.",
                                "#####.##.#.##.###.##",
                                "..#####..#.#########",
                                "####################",
                                "#.####....###.#.#.##",
                                "##.#################",
                                "#####.##.###..####..",
                                "..######..##.#######",
                                "####.##.####...##..#",
                                ".#####..#.######.###",
                                "##...#.##########...",
                                "#.##########.#######",
                                ".####.#.###.###.#.##",
                                "....##.##.###..#####",
                                ".#.#.###########.###",
                                "#.#.#.#####.####.###",
                                "###.##.####.##.#..##"]),
                                210)

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

#Advent of Code
#2019 Day 10, 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 fractions import Fraction
from math import atan2

def solve(puzzle_input):
    width  = len(puzzle_input[0])
    height = len(puzzle_input)

    asteroids = [(x, y) for x in range(width) for y in range(height) if puzzle_input[y][x] == "#"]
    detections = {}
    gradients = set()

    for x in range(width):
        for y in range(height):
            if puzzle_input[y][x] == ".":
                continue

            detections[(x, y)] = 0
            for tx, ty in asteroids:
                if (x, y) == (tx, ty):
                    continue

                if x == tx:
                    dy, dx = 1, 0
                else:
                    f = Fraction(ty-y, tx-x)
                    dy, dx = abs(f.numerator), abs(f.denominator)
                if tx < x:
                    dx *= -1
                if ty < y:
                    dy *= -1
                gradients.add((dx, dy))

                a, b = x + dx, y + dy
                while (0 <= a < width) and (0 <= b < height):
                    if puzzle_input[b][a] == "#":
                        if (a, b) == (tx, ty):
                            detections[(x, y)] = detections[(x, y)] + 1
                        break
                    a += dx
                    b += dy

    laser_x, laser_y = sorted(detections.keys(), key=lambda z: detections[z])[-1]

    def find_angle(coords):
        dx, dy = coords
        grad = atan2(dy, dx)
        if dx >= 0 and dy < 0:
            pass
        elif dx >= 0 and dy >= 0:
            grad += 10
        elif dx < 0 and dy >= 0:
            grad += 20
        else:
            grad += 30
        return grad
    gradients = sorted(gradients, key=find_angle)

    vaporised = 0
    while True:
        for dx, dy in gradients:
            x, y = laser_x + dx, laser_y + dy
            
            while (0 <= x < width) and (0 <= y < height):
                if puzzle_input[y][x] == "#":
                    puzzle_input[y] = puzzle_input[y][:x] + "." + puzzle_input[y][x+1:]
                    vaporised += 1
                    break
                x += dx
                y += dy

            if vaporised == 200:
                return (x * 100) + y

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

    value = solve(puzzle_input)

    print("The value x * 100 + y of the 200th asteroid to be vaporised is " + str(value) + ".")

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

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