Browse Source

Started day 18 finished day 19

master
Micah Halter 1 month ago
parent
commit
c0d654bf27
11 changed files with 453 additions and 0 deletions
  1. +140
    -0
      18-many-worlds-interpretation/README.md
  2. +54
    -0
      18-many-worlds-interpretation/answer.hs
  3. +81
    -0
      18-many-worlds-interpretation/input
  4. +41
    -0
      18-many-worlds-interpretation/input.test
  5. +3
    -0
      18-many-worlds-interpretation/run.sh
  6. +3
    -0
      18-many-worlds-interpretation/runtest.sh
  7. +88
    -0
      19-tractor-beam/README.md
  8. +37
    -0
      19-tractor-beam/answer.hs
  9. +1
    -0
      19-tractor-beam/input
  10. +3
    -0
      19-tractor-beam/run.sh
  11. +2
    -0
      README.md

+ 140
- 0
18-many-worlds-interpretation/README.md View File

@@ -0,0 +1,140 @@
# Day 17: Set and Forget

[Day 17 Challenge](https://adventofcode.com/2019/day/17)

## Part One

An early warning system detects an incoming solar flare and automatically activates the ship's electromagnetic shield. Unfortunately, this has cut off the Wi-Fi for many small robots that, unaware of the impending danger, are now trapped on exterior scaffolding on the unsafe side of the shield. To rescue them, you'll have to act quickly!

The only tools at your disposal are some wired cameras and a small vacuum robot currently asleep at its charging station. The video quality is poor, but the vacuum robot has a needlessly bright LED that makes it easy to spot no matter where it is.

An Intcode program, the Aft Scaffolding Control and Information Interface (ASCII, your puzzle input), provides access to the cameras and the vacuum robot. Currently, because the vacuum robot is asleep, you can only access the cameras.

Running the ASCII program on your Intcode computer will provide the current view of the scaffolds. This is output, purely coincidentally, as ASCII code: 35 means #, 46 means ., 10 starts a new line of output below the current one, and so on. (Within a line, characters are drawn left-to-right.)

In the camera output, # represents a scaffold and . represents open space. The vacuum robot is visible as ^, v, <, or > depending on whether it is facing up, down, left, or right respectively. When drawn like this, the vacuum robot is always on a scaffold; if the vacuum robot ever walks off of a scaffold and begins tumbling through space uncontrollably, it will instead be visible as X.

In general, the scaffold forms a path, but it sometimes loops back onto itself. For example, suppose you can see the following view from the cameras:

```
..#..........
..#..........
#######...###
#.#...#...#.#
#############
..#...#...#..
..#####...^..
```

Here, the vacuum robot, ^ is facing up and sitting at one end of the scaffold near the bottom-right of the image. The scaffold continues up, loops across itself several times, and ends at the top-left of the image.

The first step is to calibrate the cameras by getting the alignment parameters of some well-defined points. Locate all scaffold intersections; for each, its alignment parameter is the distance between its left edge and the left edge of the view multiplied by the distance between its top edge and the top edge of the view. Here, the intersections from the above image are marked O:

```
..#..........
..#..........
##O####...###
#.#...#...#.#
##O###O###O##
..#...#...#..
..#####...^..
```

For these intersections:

- The top-left intersection is 2 units from the left of the image and 2 units from the top of the image, so its alignment parameter is 2 * 2 = 4.
- The bottom-left intersection is 2 units from the left and 4 units from the top, so its alignment parameter is 2 * 4 = 8.
- The bottom-middle intersection is 6 from the left and 4 from the top, so its alignment parameter is 24.
- The bottom-right intersection's alignment parameter is 40.

To calibrate the cameras, you need the sum of the alignment parameters. In the above example, this is 76.

Run your ASCII program. What is the sum of the alignment parameters for the scaffold intersections?

Your puzzle answer was `7720`.

## Part Two

Now for the tricky part: notifying all the other robots about the solar flare. The vacuum robot can do this automatically if it gets into range of a robot. However, you can't see the other robots on the camera, so you need to be thorough instead: you need to make the vacuum robot visit every part of the scaffold at least once.

The vacuum robot normally wanders randomly, but there isn't time for that today. Instead, you can override its movement logic with new rules.

Force the vacuum robot to wake up by changing the value in your ASCII program at address 0 from 1 to 2. When you do this, you will be automatically prompted for the new movement rules that the vacuum robot should use. The ASCII program will use input instructions to receive them, but they need to be provided as ASCII code; end each line of logic with a single newline, ASCII code 10.

First, you will be prompted for the main movement routine. The main routine may only call the movement functions: A, B, or C. Supply the movement functions to use as ASCII text, separating them with commas (,, ASCII code 44), and ending the list with a newline (ASCII code 10). For example, to call A twice, then alternate between B and C three times, provide the string A,A,B,C,B,C,B,C and then a newline.

Then, you will be prompted for each movement function. Movement functions may use L to turn left, R to turn right, or a number to move forward that many units. Movement functions may not call other movement functions. Again, separate the actions with commas and end the list with a newline. For example, to move forward 10 units, turn left, move forward 8 units, turn right, and finally move forward 6 units, provide the string 10,L,8,R,6 and then a newline.

Finally, you will be asked whether you want to see a continuous video feed; provide either y or n and a newline. Enabling the continuous video feed can help you see what's going on, but it also requires a significant amount of processing power, and may even cause your Intcode computer to overheat.

Due to the limited amount of memory in the vacuum robot, the ASCII definitions of the main routine and the movement functions may each contain at most 20 characters, not counting the newline.

For example, consider the following camera feed:

```
#######...#####
#.....#...#...#
#.....#...#...#
......#...#...#
......#...###.#
......#.....#.#
^########...#.#
......#.#...#.#
......#########
........#...#..
....#########..
....#...#......
....#...#......
....#...#......
....#####......
```

In order for the vacuum robot to visit every part of the scaffold at least once, one path it could take is:

```
R,8,R,8,R,4,R,4,R,8,L,6,L,2,R,4,R,4,R,8,R,8,R,8,L,6,L,2
```

Without the memory limit, you could just supply this whole string to function A and have the main routine call A once. However, you'll need to split it into smaller parts.

One approach is:

- Main routine: A,B,C,B,A,C
(ASCII input: 65, 44, 66, 44, 67, 44, 66, 44, 65, 44, 67, 10)
- Function A: R,8,R,8
(ASCII input: 82, 44, 56, 44, 82, 44, 56, 10)
- Function B: R,4,R,4,R,8
(ASCII input: 82, 44, 52, 44, 82, 44, 52, 44, 82, 44, 56, 10)
- Function C: L,6,L,2
(ASCII input: 76, 44, 54, 44, 76, 44, 50, 10)

Visually, this would break the desired path into the following parts:

```
A, B, C, B, A, C
R,8,R,8, R,4,R,4,R,8, L,6,L,2, R,4,R,4,R,8, R,8,R,8, L,6,L,2

CCCCCCA...BBBBB
C.....A...B...B
C.....A...B...B
......A...B...B
......A...CCC.B
......A.....C.B
^AAAAAAAA...C.B
......A.A...C.B
......AAAAAA#AB
........A...C..
....BBBB#BBBB..
....B...A......
....B...A......
....B...A......
....BBBBA......
```

Of course, the scaffolding outside your ship is much more complex.

As the vacuum robot finds other robots and notifies them of the impending solar flare, it also can't help but leave them squeaky clean, collecting any space dust it finds. Once it finishes the programmed set of movements, assuming it hasn't drifted off into space, the cleaning robot will return to its docking station and report the amount of space dust it collected as a large, non-ASCII value in a single output instruction.

After visiting every part of the scaffold at least once, how much dust does the vacuum robot report it has collected?

Your puzzle answer was `1681189`.

+ 54
- 0
18-many-worlds-interpretation/answer.hs View File

@@ -0,0 +1,54 @@
import System.Environment
import Data.Char
import Data.List
import Data.List.Split
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set

parse :: [String] -> Map.Map (Int, Int) Char
parse strings = Map.fromList [((y, x), char) | (y,row) <- zip [0..] strings, (x,char) <- zip [0..] row ]

cardinal :: (Int, Int) -> [(Int, Int)]
cardinal (y,x) = (y,x) `seq` [(y,x+1), (y,x-1), (y+1,x), (y-1,x)]

numKeys :: Map.Map (Int, Int) Char -> Int
numKeys = Map.size . Map.filter isLower

step :: Map.Map (Int, Int) Char -> ((Int, (Int, Int)), (Set.Set Char, Set.Set Char)) -> [((Int, (Int, Int)), (Set.Set Char, Set.Set Char))]
step m ((steps, loc), (keys, doors)) =
foldl (\t (y,x) -> do
let here = Map.findWithDefault '#' (y,x) m
t ++ (if here == '.' then [((steps+1,(y,x)),(keys,doors))]
else if here == '@' then [((steps+1,(y,x)),(keys,doors))]
else if isLower here then [((steps+1,(y,x)), (Set.insert (toUpper here) keys,doors))]
else if Set.member here keys then [((steps+1,(y,x)),(keys,Set.insert here doors))]
else [])) [] (cardinal loc)

stepAll :: Map.Map (Int, Int) Char -> [((Int, (Int, Int)), (Set.Set Char, Set.Set Char))] -> ((Int, (Int, Int)), (Set.Set Char, Set.Set Char))
stepAll m (((steps, loc), (keys, doors)):rest) = if any (done keyN) nextSteps then head $ filter (done keyN) nextSteps
else stepAll m (rest ++ (filter (\((s,l),kd) -> not $ any (\((s2,l2),kd2) -> l == l2 && kd == kd2) rest)) nextSteps)
where
keyN = numKeys m
nextSteps = step m ((steps, loc), (keys, doors))

done :: Int -> ((Int, (Int, Int)), (Set.Set Char, Set.Set Char)) -> Bool
done numKeys state = numKeys == Set.size (fst $ snd state)

main = do
input <- getArgs
let parsed = parse input

let start = head $ Map.keys $ Map.filter ('@'==) parsed

print $ stepAll parsed [((0, start), (Set.empty, Set.empty))]

-- print parsed
-- let step1 = step parsed ((0, Set.empty), start)
-- let step2 = step parsed (step1!!0)
-- let step3 = step parsed (step2!!0)
-- let step4 = step parsed (step3!!0)
-- let step5 = step parsed (step4!!1)
-- let step6 = step parsed (step5!!1)
-- let step7 = step parsed (step6!!1)
-- let step8 = step parsed (step7!!1)
-- print $ done (numKeys parsed) step8

+ 81
- 0
18-many-worlds-interpretation/input View File

@@ -0,0 +1,81 @@
#################################################################################
#.........#.......#...#...#..v..#.....#.#.........#...#....g#...L...#......z#...#
#.#.#####.#.#####.###.#.#.#.#.###I#.#.#.#.#.#######E#.#.###.#.#####.#.#####.#.###
#.#.....#...#.........#.#...#.....#.#...#.#.#..n....#.#.#.#...#...#.#...#...#..p#
#.#####.#####.#######.#.###########.###.#.#.#.#.#####.#.#.#####.#.#.###.#O###.#.#
#.#...#.#.....#...#...#...........#.#...#.#.#.#.#...#.#.#.......#.#.....#.#...#.#
#.###.#.#######.#.#.###########.###.#####.#.#.###.#.#.#.#.#####.#########.#####.#
#...#.#..y..#...#.#...#.........#...#...#.#.#.....#.#...#.#...#.........#.#.....#
###.#.#####.#.###.#####.#########.###.#.#.#.#######.#####.#.#.###.#.#####.#.#####
#...#....c#...#.#.......#.......#.#...#.#.#.......#....x#.#.#.#...#.#.....#.....#
#.###.#.#######.#########.#####.#.#.###.#.#####.#.#####.###.#.#.#####.#####.###.#
#.#...#.................#.....#.#.....#.#...#...#...#.#...#.#.#.#.....#...#.#...#
#.#.#############.#.#####.###.#.#.#####.#####.#####.#.###.#.#.#.#.#####.#.###.#.#
#.#...#.....#...#.#.#...#...#.#.#.#.....#.....#...#.....#.#.#...#.....#.#.#...#.#
#.###.#.#.#.#.###.###.#.###.#.#.#.#.###.#.###.#.#.#####.#.#.#########.#.#.#.###.#
#...#.#.#.#k..#...#...#...#.#.#.#.#...#.#.#...#.#.#.....#.#.........#.#.#...#.#.#
###.#.###F#####.###.#####.###.#.#####.#.#.#####.#.#.#####.#.#####.###.#.#####.#.#
#.#.#...#...#...#...#...#.....#.....#.#.#.......#.#.....#.#.....#.....#.#.....#.#
#.#.###.###.#.###.#.#.#.###########.#.###.#######.#####.#.#######.#####.#.#####.#
#.P.#.#.....#.#...#.#.#.....#...#...#...#...#...#.#...#.#.....H.#...#...#.....#.#
#.###.#.#####.#.###.#.#####.#.###.#####.#.###.#.#.#.#.#########.###.###.#.###.#.#
#...#...#.....#.#...#.#.....#.....#.....#.#...#...#.#.......#.#.#.#.#...#.#.#...#
###.#####.#####.#######.#####.#####.#.#.#.#.#######.#####.#.#.#.#.#.#.###.#.#####
#.#.#...#.....#.....#...#...#...#.#.#.#.#.#...#.....#.....#.#...#.#.#...#...#...#
#.#.#.#.#####.#####.#.###.#.###.#.#.#.###.###.#.#####.#.#########.#.###U#####.#.#
#.#...#...#...#.....#.#.#.#...#.#...#...#...#...#.#...#.#.....#.....#...#.....#.#
#.#######.#.#.#.#####.#.#.###.#.#.#####.###.#####.#.#####.###.#.#####.###.#####.#
#.....#...#.#.#...#...#...#.#...#...#...#.#.#...#.#.....#.#.#.#.#.....#...#.....#
#.#####.###.#.###.#.###.###.#####.###W#.#.#.#.#.#.#####.#.#.#.#.#.#.###.###.###.#
#...#...#...#.#.#...#.#.....#.....#...#.#.#.#.#.......#...#.#...#.#...#.#.#...#.#
#.#.#.###.###.#.#####.#####.#####.#.#####.#.#.#######.#####.#######.#.#T#.###.#.#
#.#.#.#.....#.....#.....#...#...#.#.....#...#.#.....#.#...........#.#.....#...#.#
#.#.#.#.###.#####.#.#.###.###.#.#.#####.#.###.#.###.###.#####.###.#########.###.#
#.#...#...#...#.#.#.#.#...#...#.#.#.....#...#.....#.......#...#.#.#.....#...#...#
#.#######.###.#.#.#.###.###.###.###.###.###.#############.#.###.#.#.###.#.###.###
#.#.......#.#.#.....#...#...J.#.....#...#.#...#...#...#...#...#...#...#...#...#.#
#.#######.#.#.#######.#######.#######.###.###.#.#.#.#.#######.#.###.#.#####.###.#
#.......#...#.#.......#.....#.....#.#.#.#...#...#...#...#.....#...#.#...#.#...#.#
#######.#####.#.#######.###.#####.#.#.#.#.#############.#.#######.#####.#.###.#.#
#.............#.........#.........#.......................#.............#.......#
#######################################.@.#######################################
#.........#...#.........#.....#...#...........#...........#..q....#.....#.......#
#####.###.#.###D#####.###.#.#.#.###.#.#.#.###.#######.#.#.#####.#.###.#.#.#####.#
#.....#.....#...#.#...#...#.#...#...#.#.#...#.......#.#.#j#...#.#.#...#d#.....#.#
#.#.#######.#.###.#.#.#.###.###.#.###.#.#.#.#######.###.#.#.#.#.#.#.###.###.#.###
#t#.#.....#.#...#.#.#.#...#...#.#.#.#.#.#.#...#....w....#...#...#...#.#...#.#...#
#.###.###.#.###.#.#.#####.###.###.#.#.#.#####.#######.###############.###.#.###.#
#.....#.#.#.#...#.#...Q.#...#.......#.#.#.....#...#...#.......#.#.....#...#...#.#
#.#####.#.###.###.#####.###.#######.#.###.###.#.#.#####.#####.#.#.###.#.###.###.#
#.#.....#...#.#.......#.....#...#...#...#.#...#.#.#.......#...#...#.#...#...#...#
#.#####.###.#.#R#####.#######.#.#######.#.#####.#.#.#######.###.###.#######.#.###
#.....#...#...#.#...#...#.....#.....#...#...#...#...#.......#.........#...#.#...#
#####.#.#.#######.#.#.#.#.#########.#.#.###.#.#######.###############.#.#.#.###.#
#.#...#.#...#.....#.#.#.#...#...#...#.#.#.#...#...#...#.............#...#.#...#.#
#.#.###.###.#S#####.#.#####.#.###.###.#.#.#####.#.#.###.###########.#####.#####.#
#...#...#.#...#...#...#.....#...#.#...#.#....m#.#.#...#.......#.....#...#.....#.#
#.#####.#.#######.###.#.#####.#.#.###.#.#.###.#.#.###.###.#####.#####.#.#####.#.#
#u......#.........#...#...#.#.#.#...#.#.#.#...#.#...#.#...#.....#.....#.....#.#.#
#########.#########.#####.#.#.#.###.#.#.#.#####.###.#.#.###.#######.#####.###.#.#
#.M.....#.......#...#.....#.#.#...#.#.#.#.........#.#.#.#.#.......#..s#.#.#...#.#
#####.#.#.#####.#.###.#####.#.###.#.#.###.#######.#.#.#.#.#######.###.#.#.#.###.#
#.....#.#.....#.#...#.#.....#.#.#...#...#...#...#.#.#...#...#.....#.#.#.#...#...#
#.#########B###.###.#######.#.#.#######.#####.#.###.#######.#######.#.#.#####.#.#
#.#.A...#...#.....#.....#...#.#.#.....#.#.....#...#...#.......#.....#.#.......#.#
#.#.###.#.###.#########.#.###.#.#.#.#.#.#.#######.#.#.#.#####.#.###.#.#######.#.#
#.....#.#...#.........#.#.#...#...#.#...#.....#.....#...#a....#.#...#.....#...#.#
#####.#.#.###########.#.#.#.###.###.###.#.###.#.###############.#.#.#####.#.###.#
#...#.#...#...........#...#...#...#.#...#.#...#.#.......#.......#.#.#.....#.#...#
#.#.#######.#############.###.#####.#####.#.###.#.#####.#.#######.###.#####.#.###
#.#.........#.............#.#.V...#.....#.#.C.#.#.....#.#.#.....#...#.#r....#...#
#.#########.#.#############.#####.#####.#####.#.#####.#.#.#####.###.#.#.#######.#
#.#.........#...#...#.G...#.....#.#...#.#...#.#.#.#...#...#b..#.#...#.Y.#.....#.#
#.#############.#.#.#####.#.#.###.#.#.#.#.#.#.#.#.#.#######.#.#.#.#######.#.###.#
#.#...........#...#.....#...#.#...#.#.#.#.#...#...#.#...K...#...#.#.......#.#...#
#.#X#######.###########.#####.#.###.#.#.#.#########.#####.#######.#.#.#######.###
#.#...#...#...N.#.#...#.#l..#.#...#.#...#.......#...#..f#.......#...#.#......o#.#
#.###.#.#.###.#.#.#.#.#.#.#.#.###.#.###.#.#####.#.###.#.#.#####.#####.#.#######.#
#.#...#.#.....#...#.#.#...#...#...#..i#.#...#.#...#...#.#.#.....#.#.Z.#...#...#.#
#.#.###.###########.#.#######.#.#####.#.###.#.#######.#.###.###.#.#.#####.#.#.#.#
#h....#..........e..#.........#.......#.#.............#.....#.....#.........#...#
#################################################################################

+ 41
- 0
18-many-worlds-interpretation/input.test View File

@@ -0,0 +1,41 @@
#########################################
#.........#...#.........#.....#...#....@#
#####.###.#.###D#####.###.#.#.#.###.#.#.#
#.....#.....#...#.#...#...#.#...#...#.#.#
#.#.#######.#.###.#.#.#.###.###.#.###.#.#
#t#.#.....#.#...#.#.#.#...#...#.#.#.#.#.#
#.###.###.#.###.#.#.#####.###.###.#.#.#.#
#.....#.#.#.#...#.#...Q.#...#.......#.#.#
#.#####.#.###.###.#####.###.#######.#.###
#.#.....#...#.#.......#.....#...#...#...#
#.#####.###.#.#R#####.#######.#.#######.#
#.....#...#...#.#...#...#.....#.....#...#
#####.#.#.#######.#.#.#.#.#########.#.#.#
#.#...#.#...#.....#.#.#.#...#...#...#.#.#
#.#.###.###.#S#####.#.#####.#.###.###.#.#
#...#...#.#...#...#...#.....#...#.#...#.#
#.#####.#.#######.###.#.#####.#.#.###.#.#
#u......#.........#...#...#.#.#.#...#.#.#
#########.#########.#####.#.#.#.###.#.#.#
#.M.....#.......#...#.....#.#.#...#.#.#.#
#####.#.#.#####.#.###.#####.#.###.#.#.###
#.....#.#.....#.#...#.#.....#.#.#...#...#
#.#########B###.###.#######.#.#.#######.#
#.#.A...#...#.....#.....#...#.#.#.....#.#
#.#.###.#.###.#########.#.###.#.#.#.#.#.#
#.....#.#...#.........#.#.#...#...#.#...#
#####.#.#.###########.#.#.#.###.###.###.#
#...#.#...#...........#...#...#...#.#...#
#.#.#######.#############.###.#####.#####
#.#.........#.............#.#.V...#.....#
#.#########.#.#############.#####.#####.#
#.#.........#...#...#.G...#.....#.#...#.#
#.#############.#.#.#####.#.#.###.#.#.#.#
#.#...........#...#.....#...#.#...#.#.#.#
#.#X#######.###########.#####.#.###.#.#.#
#.#...#...#...N.#.#...#.#l..#.#...#.#...#
#.###.#.#.###.#.#.#.#.#.#.#.#.###.#.###.#
#.#...#.#.....#...#.#.#...#...#...#..i#.#
#.#.###.###########.#.#######.#.#####.#.#
#h....#..........e..#.........#.......#.#
#########################################

+ 3
- 0
18-many-worlds-interpretation/run.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh

runhaskell -i$(dirname $(pwd))/00-utils answer.hs `< input`

+ 3
- 0
18-many-worlds-interpretation/runtest.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh

runhaskell -i$(dirname $(pwd))/00-utils answer.hs `< input.test`

+ 88
- 0
19-tractor-beam/README.md View File

@@ -0,0 +1,88 @@
# Day 19: Tractor Beam

[Day 19 Challenge](https://adventofcode.com/2019/day/19)

## Part One

Unsure of the state of Santa's ship, you borrowed the tractor beam technology from Triton. Time to test it out.

When you're safely away from anything else, you activate the tractor beam, but nothing happens. It's hard to tell whether it's working if there's nothing to use it on. Fortunately, your ship's drone system can be configured to deploy a drone to specific coordinates and then check whether it's being pulled. There's even an Intcode program (your puzzle input) that gives you access to the drone system.

The program uses two input instructions to request the X and Y position to which the drone should be deployed. Negative numbers are invalid and will confuse the drone; all numbers should be zero or positive.

Then, the program will output whether the drone is stationary (0) or being pulled by something (1). For example, the coordinate X=0, Y=0 is directly in front of the tractor beam emitter, so the drone control program will always report 1 at that location.

To better understand the tractor beam, it is important to get a good picture of the beam itself. For example, suppose you scan the 10x10 grid of points closest to the emitter:

```
X
0-> 9
0#.........
|.#........
v..##......
...###....
....###...
Y .....####.
......####
......####
.......###
9........##
```

In this example, the number of points affected by the tractor beam in the 10x10 area closest to the emitter is 27.

However, you'll need to scan a larger area to understand the shape of the beam. How many points are affected by the tractor beam in the 50x50 area closest to the emitter? (For each of X and Y, this will be 0 through 49.)

Your puzzle answer was `110`.

## Part Two

You aren't sure how large Santa's ship is. You aren't even sure if you'll need to use this thing on Santa's ship, but it doesn't hurt to be prepared. You figure Santa's ship might fit in a 100x100 square.

The beam gets wider as it travels away from the emitter; you'll need to be a minimum distance away to fit a square of that size into the beam fully. (Don't rotate the square; it should be aligned to the same axes as the drone grid.)

For example, suppose you have the following tractor beam readings:

```
#.......................................
.#......................................
..##....................................
...###..................................
....###.................................
.....####...............................
......#####.............................
......######............................
.......#######..........................
........########........................
.........#########......................
..........#########.....................
...........##########...................
...........############.................
............############................
.............#############..............
..............##############............
...............###############..........
................###############.........
................#################.......
.................########OOOOOOOOOO.....
..................#######OOOOOOOOOO#....
...................######OOOOOOOOOO###..
....................#####OOOOOOOOOO#####
.....................####OOOOOOOOOO#####
.....................####OOOOOOOOOO#####
......................###OOOOOOOOOO#####
.......................##OOOOOOOOOO#####
........................#OOOOOOOOOO#####
.........................OOOOOOOOOO#####
..........................##############
..........................##############
...........................#############
............................############
.............................###########
```

In this example, the 10x10 square closest to the emitter that fits entirely within the tractor beam has been marked O. Within it, the point closest to the emitter (the only highlighted O) is at X=25, Y=20.

Find the 100x100 square closest to the emitter that fits entirely within the tractor beam; within that square, find the point closest to the emitter. What value do you get if you take that point's X coordinate, multiply it by 10000, then add the point's Y coordinate? (In the example above, this would be 250020.)

Your puzzle answer was `17302065`.

+ 37
- 0
19-tractor-beam/answer.hs View File

@@ -0,0 +1,37 @@
import System.Environment
import Data.Char
import Data.List
import Data.List.Split
import qualified Data.Map.Strict as Map

import Intcode


checkXY :: Map.Map Int Int -> (Int, Int) -> Bool
checkXY m (x,y) = [1] == (computeTilHalt $ startingState m [x,y])

checkBox :: Map.Map Int Int -> (Int, Int) -> (Int, Int) -> Bool
checkBox m (x,y) (width,height) = checkXY m (x,y) && checkXY m (x,y+(height-1)) && checkXY m (x-(width-1),y) && checkXY m (x-(width-1),y+99)

maxY :: Map.Map Int Int -> Int -> (Int, Int) -> Int
maxY m x (y,y2) = if y == y2-1 then y2 else maxY m x (if checkXY m (x, checkY) then (y, checkY) else (checkY, y2))
where checkY = (y+y2) `div` 2

maxX :: Map.Map Int Int -> (Int, Int) -> (Int, Int) -> (Int, Int)
maxX m (x,x2) (width,height) = if x == x2-1 then (x2-(width-1), maxY m x2 (0, floor ((fromIntegral x2) / 0.6))) else maxX m (if checkBox m (checkX, y) (width,height) then (x, checkX) else (checkX, x2)) (width,height)
where
checkX = (x+x2) `div` 2
y = maxY m checkX (0, floor ((fromIntegral checkX) / 0.6))

solveA :: Map.Map Int Int -> (Int, Int) -> Int
solveA m (width, height) = length $ filter (checkXY m) $ foldl (\t i -> t ++ (zip (repeat i) [0..(width-1)])) [] [0..(height-1)]

solveB :: Map.Map Int Int -> (Int, Int) -> Int
solveB m (width, height) = (x*10000)+y where (x,y) = maxX m (0, 10000) (width, height)

main = do
input <- getArgs
let parsed = parseProg $ input!!0

print $ solveA parsed (50, 50)
print $ solveB parsed (100,100)

+ 1
- 0
19-tractor-beam/input View File

@@ -0,0 +1 @@
109,424,203,1,21102,11,1,0,1106,0,282,21101,0,18,0,1106,0,259,1201,1,0,221,203,1,21102,1,31,0,1106,0,282,21101,0,38,0,1106,0,259,20102,1,23,2,21202,1,1,3,21101,1,0,1,21101,0,57,0,1105,1,303,2101,0,1,222,20101,0,221,3,21001,221,0,2,21102,1,259,1,21101,0,80,0,1105,1,225,21101,185,0,2,21102,91,1,0,1106,0,303,1202,1,1,223,21001,222,0,4,21102,259,1,3,21101,225,0,2,21102,1,225,1,21101,0,118,0,1106,0,225,20102,1,222,3,21102,1,131,2,21101,133,0,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21101,148,0,0,1105,1,259,2101,0,1,223,21002,221,1,4,21002,222,1,3,21101,0,16,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21101,0,195,0,106,0,109,20207,1,223,2,20101,0,23,1,21102,1,-1,3,21101,0,214,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1201,-4,0,249,22101,0,-3,1,22101,0,-2,2,21201,-1,0,3,21101,0,250,0,1106,0,225,21201,1,0,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,22102,1,-2,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21201,-2,0,3,21101,343,0,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22101,0,-4,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21201,1,0,-4,109,-5,2106,0,0

+ 3
- 0
19-tractor-beam/run.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh

runhaskell -i$(dirname $(pwd))/00-utils answer.hs `< input`

+ 2
- 0
README.md View File

@@ -32,3 +32,5 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
- [Day 15: Oxygen System](15-oxygen-system)
- [Day 16: Flawed Frequency Transmission](16-flawed-frequency-transmission)
- [Day 17: Set and Forget](17-set-and-forget)
- [Day 18: Many-Worlds Interpretation](18-many-worlds-interpretation)
- [Day 19: Tractor Beam](19-tractor-beam)

Loading…
Cancel
Save