2019 - Day 19 (Tractor Beam)

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

#Advent of Code
#2019 Day 19, 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 util.intcode_2019 import IntcodeComputer

def solve(puzzle_input):
    computer = IntcodeComputer().load_memory(puzzle_input)

    def query(coords):
        computer.reset().queue_inputs(coords)
        while not computer.outputs:
            computer.step()
        return computer.outputs[0]

    return sum(query((x, y)) for x in range(50) for y in range(50))

def main():
    puzzle_input = util.read.as_int_list(",")

    points = solve(puzzle_input)

    print("The number of points affected by the tractor beam in the 50x50 area closest to the emitter is " + str(points) + ".")

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

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

#Advent of Code
#2019 Day 19, 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 util.intcode_2019 import IntcodeComputer

def solve(puzzle_input):
    computer = IntcodeComputer().load_memory(puzzle_input)
    grid = {}

    def query(coords):
        computer.reset().queue_inputs(coords)
        while not computer.outputs:
            computer.step()
        result = computer.outputs[0]
        return ".#"[result]

    def get(x, y):
        if y not in grid:
            grid[y] = {}
        try:
            result = grid[y][x]
        except KeyError:
            result = query((x, y))
            grid[y][x] = result
        return result

    def search(start, step):
        x, y = 0, start
        # The start of the next row is never to the left of the current row
        start_of_row = 0

        while True:
            while get(x, y) == ".":
                x += 1
            start_of_row = x

            while True:
                if get(x, y) == ".":
                    break

                # After reading some discussions on /r/AdventOfCode, I realised
                # that the entire row and column don't need to be checked, only
                # the ends (as there won't be a break in the middle of a row/col)
                # (Reduced runtime from ~66s to ~40s on my machine)

                if get(x + 99, y) == "#":
                    # Split row/col checks, so can skip to next row faster if
                    # the row is no longer wide enough to fit the square
                    # (Reduced runtime from ~40s to ~31s)
                    if get(x, y + 99) == "#":
                        if step == 100:
                            # Coarse search, return result of fine search
                            return search(y - 100, 1)
                        else:
                            # Fine search
                            return (x * 10000) + y
                else:
                    # The row is no longer wide enough to fit the square
                    # Skip to the next row
                    break

                x += 1

            # In the majority of cases, we won't need this row
            # again, as we always look forwards into the grid
            del grid[y]
            x, y = start_of_row, y + step


    # Perform a coarse search by skipping 100 rows at a time
    # Then, backtrack 100 rows and search one row at a time
    # to find the final answer

    # A visual inspection of Part 1 shows that the tractor beam
    # is not wide enough to contain the square
    # This, combined with the gap in the beam in the first few rows
    # means we can start a few rows later

    return search(100, 100)

def main():
    puzzle_input = util.read.as_int_list(",")

    value = solve(puzzle_input)

    print("The value of the closest coordinate in the 100x100 square is " + str(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