Browse Source

Finished day 21

master
Micah Halter 1 month ago
parent
commit
663c70309e
12 changed files with 604 additions and 4 deletions
  1. +9
    -0
      00-utils/Intcode.hs
  2. +1
    -4
      17-set-and-forget/answer.hs
  3. +215
    -0
      20-donut-maze/README.md
  4. +55
    -0
      20-donut-maze/answer.hs
  5. +123
    -0
      20-donut-maze/input
  6. +37
    -0
      20-donut-maze/input.test
  7. +3
    -0
      20-donut-maze/run.sh
  8. +116
    -0
      21-springdroid-adventure/README.md
  9. +39
    -0
      21-springdroid-adventure/answer.hs
  10. +1
    -0
      21-springdroid-adventure/input
  11. +3
    -0
      21-springdroid-adventure/run.sh
  12. +2
    -0
      README.md

+ 9
- 0
00-utils/Intcode.hs View File

@@ -1,5 +1,7 @@
module Intcode
( parseProg
, parseInput
, parseOutput
, compute
, computeTilHalt
, startingState
@@ -8,12 +10,19 @@ module Intcode
import Data.List as List
import Data.Maybe
import Data.Ord
import Data.Char
import Data.List.Split
import qualified Data.Map.Strict as Map

parseProg :: String -> Map.Map Int Int
parseProg str = Map.fromList $ List.zip [0..] $ map (read :: String -> Int) $ splitOn "," str

parseInput :: [String] -> [Int]
parseInput = foldl (\out i -> out ++ i ++ [ord '\n']) [] . map (map ord)

parseOutput :: [Int] -> String
parseOutput = map chr . reverse

compute :: (Map.Map Int Int, Int, Int) -> ([Int], [Int]) -> ((Map.Map Int Int, Int, Int), ([Int], [Int]))
compute (x, y, z) (input_orig, output) =
if op == 99

+ 1
- 4
17-set-and-forget/answer.hs View File

@@ -19,9 +19,6 @@ isIntersection (x, y) m = and (map (\k -> 35 == (Map.findWithDefault (-1) k m))
calibrate :: Map.Map (Int, Int) Int -> Int
calibrate m = Map.foldlWithKey (\total (x,y) i -> total + (if (isIntersection (x, y) m) && i == 35 then (x*y) else 0)) 0 m

getInput :: [String] -> [Int]
getInput = foldl (\out i -> out ++ i ++ [ord '\n']) [] . map (map ord)

main = do
input <- getArgs
let parsed = parseProg $ input!!0
@@ -36,7 +33,7 @@ main = do
"R,12,L,12,R,6",
"R,10,R,12,L,12",
"n"]
print (flip (!!) 0 $ computeTilHalt $ startingState (Map.insert 0 2 parsed) $ getInput directions)
print (flip (!!) 0 $ computeTilHalt $ startingState (Map.insert 0 2 parsed) $ parseInput directions)


-- L10R10L10L10R10R12L12L10R10L10L10R10R12L12R12L12R6R12L12R6R10R12L12L10R10L10L10R10R12L12R12L12R6

+ 215
- 0
20-donut-maze/README.md View File

@@ -0,0 +1,215 @@
# Day 20: Donut Maze

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

## Part One

You notice a strange pattern on the surface of Pluto and land nearby to get a closer look. Upon closer inspection, you realize you've come across one of the famous space-warping mazes of the long-lost Pluto civilization!

Because there isn't much space on Pluto, the civilization that used to live here thrived by inventing a method for folding spacetime. Although the technology is no longer understood, mazes like this one provide a small glimpse into the daily life of an ancient Pluto citizen.

This maze is shaped like a donut. Portals along the inner and outer edge of the donut can instantly teleport you from one side to the other. For example:

```
A
A
#######.#########
#######.........#
#######.#######.#
#######.#######.#
#######.#######.#
##### B ###.#
BC...## C ###.#
##.## ###.#
##...DE F ###.#
##### G ###.#
#########.#####.#
DE..#######...###.#
#.#########.###.#
FG..#########.....#
###########.#####
Z
Z
```

This map of the maze shows solid walls (#) and open passages (.). Every maze on Pluto has a start (the open tile next to AA) and an end (the open tile next to ZZ). Mazes on Pluto also have portals; this maze has three pairs of portals: BC, DE, and FG. When on an open tile next to one of these labels, a single step can take you to the other tile with the same label. (You can only walk on . tiles; labels and empty space are not traversable.)

One path through the maze doesn't require any portals. Starting at AA, you could go down 1, right 8, down 12, left 4, and down 1 to reach ZZ, a total of 26 steps.

However, there is a shorter path: You could walk from AA to the inner BC portal (4 steps), warp to the outer BC portal (1 step), walk to the inner DE (6 steps), warp to the outer DE (1 step), walk to the outer FG (4 steps), warp to the inner FG (1 step), and finally walk to ZZ (6 steps). In total, this is only 23 steps.

Here is a larger example:

```
A
A
#################.#############
#.#...#...................#.#.#
#.#.#.###.###.###.#########.#.#
#.#.#.......#...#.....#.#.#...#
#.#########.###.#####.#.#.###.#
#.............#.#.....#.......#
###.###########.###.#####.#.#.#
#.....# A C #.#.#.#
####### S P #####.#
#.#...# #......VT
#.#.#.# #.#####
#...#.# YN....#.#
#.###.# #####.#
DI....#.# #.....#
#####.# #.###.#
ZZ......# QG....#..AS
###.### #######
JO..#.#.# #.....#
#.#.#.# ###.#.#
#...#..DI BU....#..LF
#####.# #.#####
YN......# VT..#....QG
#.###.# #.###.#
#.#...# #.....#
###.### J L J #.#.###
#.....# O F P #.#...#
#.###.#####.#.#####.#####.###.#
#...#.#.#...#.....#.....#.#...#
#.#####.###.###.#.#.#########.#
#...#.#.....#...#.#.#.#.....#.#
#.###.#####.###.###.#.#.#######
#.#.........#...#.............#
#########.###.###.#############
B J C
U P P
```

Here, AA has no direct path to ZZ, but it does connect to AS and CP. By passing through AS, QG, BU, and JO, you can reach ZZ in 58 steps.

In your maze, how many steps does it take to get from the open tile marked AA to the open tile marked ZZ?

Your puzzle answer was `664`.

## Part Two

Strangely, the exit isn't open when you reach it. Then, you remember: the ancient Plutonians were famous for building recursive spaces.

The marked connections in the maze aren't portals: they physically connect to a larger or smaller copy of the maze. Specifically, the labeled tiles around the inside edge actually connect to a smaller copy of the same maze, and the smaller copy's inner labeled tiles connect to yet a smaller copy, and so on.

When you enter the maze, you are at the outermost level; when at the outermost level, only the outer labels AA and ZZ function (as the start and end, respectively); all other outer labeled tiles are effectively walls. At any other level, AA and ZZ count as walls, but the other outer labeled tiles bring you one level outward.

Your goal is to find a path through the maze that brings you back to ZZ at the outermost level of the maze.

In the first example above, the shortest path is now the loop around the right side. If the starting level is 0, then taking the previously-shortest path would pass through BC (to level 1), DE (to level 2), and FG (back to level 1). Because this is not the outermost level, ZZ is a wall, and the only option is to go back around to BC, which would only send you even deeper into the recursive maze.

In the second example above, there is no path that brings you to ZZ at the outermost level.

Here is a more interesting example:

```
Z L X W C
Z P Q B K
###########.#.#.#.#######.###############
#...#.......#.#.......#.#.......#.#.#...#
###.#.#.#.#.#.#.#.###.#.#.#######.#.#.###
#.#...#.#.#...#.#.#...#...#...#.#.......#
#.###.#######.###.###.#.###.###.#.#######
#...#.......#.#...#...#.............#...#
#.#########.#######.#.#######.#######.###
#...#.# F R I Z #.#.#.#
#.###.# D E C H #.#.#.#
#.#...# #...#.#
#.###.# #.###.#
#.#....OA WB..#.#..ZH
#.###.# #.#.#.#
CJ......# #.....#
####### #######
#.#....CK #......IC
#.###.# #.###.#
#.....# #...#.#
###.### #.#.#.#
XF....#.# RF..#.#.#
#####.# #######
#......CJ NM..#...#
###.#.# #.###.#
RE....#.# #......RF
###.### X X L #.#.#.#
#.....# F Q P #.#.#.#
###.###########.###.#######.#########.###
#.....#...#.....#.......#...#.....#.#...#
#####.#.###.#######.#######.###.###.#.#.#
#.......#.......#.#.#.#.#...#...#...#.#.#
#####.###.#####.#.#.#.#.###.###.#.###.###
#.......#.....#.#...#...............#...#
#############.#.#.###.###################
A O F N
A A D M
```

One shortest path through the maze is the following:

- Walk from AA to XF (16 steps)
- Recurse into level 1 through XF (1 step)
- Walk from XF to CK (10 steps)
- Recurse into level 2 through CK (1 step)
- Walk from CK to ZH (14 steps)
- Recurse into level 3 through ZH (1 step)
- Walk from ZH to WB (10 steps)
- Recurse into level 4 through WB (1 step)
- Walk from WB to IC (10 steps)
- Recurse into level 5 through IC (1 step)
- Walk from IC to RF (10 steps)
- Recurse into level 6 through RF (1 step)
- Walk from RF to NM (8 steps)
- Recurse into level 7 through NM (1 step)
- Walk from NM to LP (12 steps)
- Recurse into level 8 through LP (1 step)
- Walk from LP to FD (24 steps)
- Recurse into level 9 through FD (1 step)
- Walk from FD to XQ (8 steps)
- Recurse into level 10 through XQ (1 step)
- Walk from XQ to WB (4 steps)
- Return to level 9 through WB (1 step)
- Walk from WB to ZH (10 steps)
- Return to level 8 through ZH (1 step)
- Walk from ZH to CK (14 steps)
- Return to level 7 through CK (1 step)
- Walk from CK to XF (10 steps)
- Return to level 6 through XF (1 step)
- Walk from XF to OA (14 steps)
- Return to level 5 through OA (1 step)
- Walk from OA to CJ (8 steps)
- Return to level 4 through CJ (1 step)
- Walk from CJ to RE (8 steps)
- Return to level 3 through RE (1 step)
- Walk from RE to IC (4 steps)
- Recurse into level 4 through IC (1 step)
- Walk from IC to RF (10 steps)
- Recurse into level 5 through RF (1 step)
- Walk from RF to NM (8 steps)
- Recurse into level 6 through NM (1 step)
- Walk from NM to LP (12 steps)
- Recurse into level 7 through LP (1 step)
- Walk from LP to FD (24 steps)
- Recurse into level 8 through FD (1 step)
- Walk from FD to XQ (8 steps)
- Recurse into level 9 through XQ (1 step)
- Walk from XQ to WB (4 steps)
- Return to level 8 through WB (1 step)
- Walk from WB to ZH (10 steps)
- Return to level 7 through ZH (1 step)
- Walk from ZH to CK (14 steps)
- Return to level 6 through CK (1 step)
- Walk from CK to XF (10 steps)
- Return to level 5 through XF (1 step)
- Walk from XF to OA (14 steps)
- Return to level 4 through OA (1 step)
- Walk from OA to CJ (8 steps)
- Return to level 3 through CJ (1 step)
- Walk from CJ to RE (8 steps)
- Return to level 2 through RE (1 step)
- Walk from RE to XQ (14 steps)
- Return to level 1 through XQ (1 step)
- Walk from XQ to FD (8 steps)
- Return to level 0 through FD (1 step)
- Walk from FD to ZZ (18 steps)

This path takes a total of 396 steps to move from AA at the outermost layer to ZZ at the outermost layer.

In your maze, when accounting for recursion, how many steps does it take to get from the open tile marked AA to the open tile marked ZZ, both at the outermost layer?

+ 55
- 0
20-donut-maze/answer.hs View File

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

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+1),(y,x-1),(y+1,x),(y-1,x)]

findPortal :: Map.Map (Int, Int) Char -> Char -> [(Int, Int)]
findPortal m c = intercalate [] $ map (\k -> filter (\newK -> (Map.findWithDefault '#' newK m) == '.') $ surrounding k) a
where
surrounding l = cardinal l ++ (cardinal $ head $ filter (\newL -> (\i -> isUpper i || isLower i) (Map.findWithDefault '#' newL m)) $ cardinal l)
a = Map.keys $ Map.filter (==c) m

getPortals :: Map.Map (Int, Int) Char -> Map.Map Char [(Int, Int)]
getPortals m = Map.fromList $ map (\c -> (c, findPortal m c)) $ nub $ Map.elems $ Map.filter (\i -> isUpper i || isLower i) m

enterPortal :: Map.Map Char [(Int, Int)] -> Char -> (Int, Int) -> (Int, Int)
enterPortal portals c loc = if found == [] then loc else head found
where found = filter (/=loc) $ portals Map.! c

step :: Map.Map (Int, Int) Char -> Map.Map Char [(Int, Int)] -> ((Int, Bool), ((Int, Int), Set.Set (Int, Int))) -> [((Int, Bool), ((Int, Int), Set.Set (Int, Int)))]
step m portals ((steps, done), (loc, been)) =
foldl (\t (y,x) -> do
let here = Map.findWithDefault '#' (y,x) m
t ++ (if Set.member (y,x) been then []
else if here == '.' then [((steps+1,done),((y,x), Set.insert (y,x) been))]
else if (\i -> isUpper i || isLower i)here then
if here == 'Z' then [((steps,True),(loc,been))]
else do
let (newY,newX) = enterPortal portals here loc
[((steps+1,done),((newY,newX), Set.insert (newY,newX) been))]
else [])) [] (cardinal loc)

stepAll :: Map.Map (Int, Int) Char -> Map.Map Char [(Int, Int)] -> [((Int, Bool), ((Int, Int), Set.Set (Int, Int)))] -> Int
stepAll m portals (((steps, done), (loc, been)):rest) = if any (\((_, done),_) -> done) nextSteps then fst $ fst $ head $ nextSteps
else stepAll m portals (rest ++ nextSteps)
where
fullBeen = foldl (\t (_, (_, b)) -> Set.union t b) been rest
nextSteps = step m portals ((steps, done), (loc, fullBeen))

main = do
input <- readFile "input"
let parsed = parse $ lines input

let start = head $ findPortal parsed 'A'
let portals = getPortals parsed
-- print portals
print $ stepAll parsed portals [((0, False), (start, Set.singleton start))]

+ 123
- 0
20-donut-maze/input View File

@@ -0,0 +1,123 @@
a E P F Q A J H
a E P F Q A J H
#######################################.#########.###.###########.###.#####.###.#####.#######################################
#...#.....................#...........#.#...#.......#.......#.......#.#...........#.#.....#.#.......#.#...#.......#.#.#.#...#
###.#####.#############.#############.#.#.#.#.#########.#.###.###.###.###.###.#####.###.###.#.#######.###.#.#######.#.#.#.###
#.#.....#.#.#.....#...........#.....#.....#.#.........#.#...#...#...#...#.#.....#.....#...#...#.........#...........#.#...#.#
#.#####.###.###.#######.###.#####.#.###.###.###.###.#######.#.#.#######.###.#.#####.###.###.#.#####.#####.###.#######.#.###.#
#...#.....#.#.#.....#...#.....#...#.......#...#.#.#.....#...#.#.....#.#...#.#.#.......#.....#.#...#.#.#.#...#.....#.#.......#
#.#######.#.#.#.#############.#.#.#.#######.#####.#.#####.#######.###.#.###.#.#####.#.#.#.#######.#.#.#.#.#########.#.#####.#
#...#.....#.....#.#.#...#.#.....#.#.#.......#.#.......#.....#.#.#.#.#...#...#.#.....#...#...#...............#.#...#.#.....#.#
#.###.#.#######.#.#.###.#.###########.#####.#.###.#####.#.###.#.#.#.#.#.###.#.###.#.#######.###.###.#.#.#####.#.###.###.#####
#...#.#.#...#...#.....#.......#.#.#.....#...#.....#.#.#.#...#.......#.#.#.#.#.#...#.#.....#.......#.#.#...#...#...#.......#.#
#.#####.#.###.#####.###.###.#.#.#.#.###.###.###.###.#.#####.#.#.#.###.###.#.#######.#.#.#######.#.###.#####.###.#######.###.#
#...#.....#.........#.....#.#.#...#.#...#.#...#.......#.....#.#.#.#.......#.#.#.......#.#.....#.#.#.#...#...........#.#...#.#
###.#.#####.#.###.#.#########.###.#####.#.#####.###.###.#.###.#######.#####.#.#.#######.#.#####.###.#.#########.###.#.#.###.#
#...#.#...#.#.#.#.#.#.#.....#.#.#.#.#.....#...#...#.#...#.#...#.........#.....#.#...#.#.......#.#.#.#.#...#...#.#.#.#.#...#.#
###.#.#.#######.###.#.#.#####.#.#.#.#.###.#.###.###.#.###.###.#######.#####.#####.###.###.#####.#.#.###.#.###.###.#.#.#.###.#
#...#.....#.#.#.#.......#.#.......#...#.......#.#...#...#.#...#.#.....#...#.....#.............#.......#.#.#.#.#...#...#.#...#
###.###.###.#.#.###.#.#.#.#####.#.###.#.#.###.#.###.#.###.###.#.#####.###.#.#######.###########.#######.###.#.#.#.#.###.#.###
#.#.......#.#.......#.#.....#...#...#.#.#...#.#.#...#.#.#.#...#...#...#.#...#.#.#...........#.#...#...#...#...#.#...#...#...#
#.###.#####.#.#######.#.#.#####.###.#.#############.#.#.#####.###.###.#.###.#.#.#####.###.#.#.###.#.###.###.#######.###.#.###
#.#.........#.#.#.....#.#...#.#.#.#.......#...#...#.#.#.#.#.........#.#.#.#.#.#.#.....#...#.#...#.#...#...#.#.#...#.#.#.....#
#.###.###.#####.#####.###.###.###.###.#######.###.#.#.#.#.#########.#.#.#.#.#.#.#.###.#######.#####.###.###.#.#.#.#.#.#.#.#.#
#.......#.#.#.#.#...#...#.....#...........#.....#...#...#...........#...#.#.....#.#...........#.#.#.#...#...#.#.#.....#.#.#.#
#####.#####.#.#.###.#######.#########.#####.#.###.#####.#####.#######.###.#####.###.#######.###.#.#.###.#.#.#.###.#####.###.#
#.....#.....#.#.#.#.....#.............#...#.#.#.#...#...#.#.........#.....#.....#.......#.#.#.#.......#.#.#.#...#.#...#.#.#.#
#####.###.###.#.#.###.#######.#######.###.###.#.###.#.###.###.###.#######.#.#######.#####.#.#.#.###.###.###.#.#.#.###.###.#.#
#...#.#.......#.#.#.............#.#...#...#.......#.#...#.#.#.#.#...#.....#.......#.....#.........#.#.......#.#.#.....#.....#
#.###.#####.###.#.#.#.###.#######.###.#.#.###.#.###.###.#.#.#.#.###.#.#####.#####.#.###.#.###.#.#####.#.#######.#.#########.#
#.#.#...#.#.....#.#.#...#.#.#.#.....#.#.#.#.#.#.......#.....#.#.....#.#.....#...#.#...#.#.#...#...#...#...#...#.#.#.#.#.....#
#.#.#.###.#.#####.#.#######.#.###.###.#.#.#.#.#####.#.#.#.#####.#.###.###.#.#.#.#.###.#################.#.###.#.#.#.#.#.#.#.#
#...#.....#.......#.#...#...............#...#...#...#.#.#...#.#.#...#...#.#.#.#...#.......#...#.#.#...#.#...#.........#.#.#.#
###.#.#####.###########.#########.#.#######.###.#########.###.#####.###.#####.###.#.#.#.###.###.#.#.###.#########.#########.#
#.....#.......#.#...#...#...#.#...#.#.......#.....#.........#.........#...#.....#.#.#.#.....#...#...#.......#.......#.......#
#.#####.#######.###.#.###.###.#######.#########.###########.###.#########.#####.###.###########.#.#####.#########.#########.#
#.#.#...#...#...#.#.#...#.......# c I U W E B a #.#...#.#...#.#.#...#...#...#...#
#.#.###.#.###.###.#.#.#######.### c I U W E B a #.###.#.###.#.#.#.#####.#.#####.#
#.....#...#.#.#.....#.#.....#.#.# #.#.#.....#.....................#
###.###.###.#.#.#.###.###.#.#.#.# #.#.#.#.###.###.#.###.#.#.###.###
#...#.......#.#.#.......#.#......HH #...#.#.#.....#.#.#...#.#.#.....#
#.#.#.###.###.#######.#######.### #.###.#.###.#######.#.#.###.#.###
WW..#...#.......#.#.......#.#.....# FF..#...#.#.......#.#.#.#.#...#...#
#.#.#.#####.#.#.#####.###.#####.# #.#.###.#.###.#.#.###.###.#####.#
#.#.#...#...#.#.....#.#.#.....#.# #.....#...#.#.#.#...#.#.#.#...#..TT
#####.###.#####.#####.#.###.#.#.# ###########.#####.#####.#####.###
#.#.#.#.....................#...# #.................#.........#....MM
#.#.###########.###.###.###.#.### ###.#######.#.#.###.#.#######.#.#
BB........#.........#...#.#.#.#.#.# #.....#.#...#.#.....#...#.....#.#
#####.#########.#######.#.#####.# #####.#####.#.#########.#.#.#.#.#
#.........#.........#...#.......# NN....#.....#.#.....#.....#.#.#.#.#
#.#####.#.###############.####### #.#.###.###.#.#.###.#####.#####.#
#...#.#.#.#.#...#...#.......#...# PP..#.......#.#.#.#.#.........#.#..XX
###.#######.#.#####.#####.###.#.# #################.#######.###.#.#
#.............................#..OO #.........#.........#.#.#...#...#
#########.###.###.#.############# #.#####.#########.#.#.#.#########
VV....#.#.#...#.#...#.#.#...#...#.# YY..#.......#.......#.........#.#.#
###.#.#.#############.#.#.#.#.#.# ###.#############.###.#######.#.#
#.....#...#.....#...#...#...#...# #.......#.#.#...#...#...#...#....CC
#####.#.###.###.###.###.#######.# #######.#.#.#.#####.###.###.###.#
#...#.......#.#.#.#.#.........#..KK #...#.................#.........#
#.###.#.#.###.###.#.#.#.#####.#.# #.#####################.#####.###
#...#.#.#.............#...#...#.# #...#.#...............#.#.#.#.#.#
#.#########.#.###.###.#.######### #.###.#.#######.#####.###.#.###.#
#.....#.#...#.#.....#.#...#.#.#.# bb......#.#...#...#.#...#.......#.#
#.#.###.###.###############.#.#.# #####.#.#########.#.###.#####.#.#
II..#.#.....#.#.........#...#.#...# #.....#.......#.#.......#.....#..UU
###.###.#######.#######.###.###.# #.#######.#####.#.#######.#####.#
#...#...#.#.#.#.....#.....#.#.#..SS #.........#.....#.#.....#.......#
###.#.#.#.#.#.#.#####.#.###.#.#.# ###########.#########.#########.#
#.#...#...............#.........# #.#.....#.....#.....#...#.....#.#
#.#.#########.#########.#.###.### #.#.#.#.#.###.#.#.###.#.###.#.###
#.#...#.....#.#.#.#.....#...#...# #...#.#.....#...#.....#.....#...#
#.#####.#.#.###.#.############### #.#######.#.###.###.###.#######.#
#...#.#.#.#.....#.......#...#.#.# MM....#.#...#.#.#...#.#.#.#...#....OO
#.###.#.#####.###.#######.#.#.#.# #####.#######.#######.#####.#####
#.....#.#.#...#.....#.....#......JJ #.#.............#.#.............#
#.#.###.#.#.###.###.#.#######.### #.#.###.#####.#.#.#.#######.###.#
LL..#.....#.......#.....#.#...#...# DD..#.#.#...#.#.#.........#...#...#
#######################.#.####### #.#.###.###.###.#############.###
#.#.#.....#.#...................# #.#...#...#...#.#.....#.......#..GG
#.#.#.#.###.###.#####.#.#.#.#.### #.###.#####.#######.#######.###.#
cc..#...#.....#...#.#.#.#.#.#.#.#..QQ #.......#.#.......#.#...#.#.....#
#.#.#######.#.###.#.#######.#.#.# #########.#####.###.###.#.#######
#.#.....#...#...........#...#...# GG................#...............#
#.###.###.#####.#########.#.###.# #.#.###.###.###.###.#.#.#.#.#.###
ZZ......#.............#.....#.#.#.# #.#.#...#...#.......#.#.#.#.#....KK
#.###.###.#####.#.#.#######.#.### L R X V T C #.#.###.###.#######.###.#####.#.#
#.#.#.#.#.....#.#.#.....#.#.....# L R X V T C #.#.#...#.#.#...#.#...#...#...#.#
#.#.###.#.#.#.#.#.#######.#.###.#############.###.#######.###########.#######.#######.#############.#.#####.#.#.#.#.###.#.#.#
#...#.....#.#.#.#.....#.......#.#.....#.......#.......#.#.....#.#.....#.#.......#.#.....#...#.#.........#.......#.#.#...#.#.#
#.#.#.#.#.#########.#######.#######.#########.#.#.#.###.#####.#.###.#.#.###.#####.#.#####.#.#.#####.#######.###.#########.#.#
#.#.#.#.#...#.#...#...#.#.#.#.#.....#.....#...#.#.#.#.#.#.........#.#.#...#...#.#.........#...#...#.....#.#.#.........#...#.#
#.###.#.#####.#.###.###.#.#.#.#####.#####.###.#.#####.#.###.#.#######.#.###.###.###.#####.###.###.#.#####.#.#####.#.#.#.#.###
#.#...#...........#.#...........#.....#...#...#...#.#.#.....#.#...#.#.#...#...#...#...#.#...#.#.....#.#.........#.#.#.#.#...#
#####.###.###.###.###.###.#.#.#####.#####.#.###.###.#.#####.###.#.###.#.#.###.###.#.###.#########.#.#.#####.#####.#.###.###.#
#.....#.#.#.....#...#.#...#.#.#...............#.......#.#.....#.#...#.#.#...#...#.......#.#.......#...#...#.#.#.#.#...#.#.#.#
#.###.#.#####.#.#.#####.#.#.#######.#######.#####.###.#.#.###.#.###.#.#.###.#.#####.#.#.#.#######.#######.#.#.#.#######.#.#.#
#...#...#.....#.#.#.....#.#...#...#.#.#.........#.#.....#.#.#.#.#.....#...#.....#...#.#.....#.#.#.#.........#.#...#.......#.#
#.#####.#####.###.#.#.#.#######.###.#.#.#.###.#####.###.###.#.#.#########.###.#.#####.###.###.#.###.###.###.#.#.#######.#.#.#
#.#.....#.#.....#.#.#.#.#...#.......#...#...#.#...#...#.#.#.#.#.......#.....#.#.#.....#.#.#.#.#.....#.....#.........#.#.#.#.#
#.#.#.#.#.###.#.#########.#########.#.#######.#.#.###.###.#.#.#.#.###.#.#########.###.#.###.#.#####.#.#######.#.#.###.#####.#
#.#.#.#...#...#...#.#.#.#.#.#.......#.#.#.#...#.#.......#.....#.#.#.#.#...#.....#.#.....#.#.#.#.#...#.#.......#.#.#.#.#...#.#
#.#####.#.#.#.#####.#.#.#.#.###########.#.###.#.###.#######.#.#.#.#.#.#.#######.#####.###.#.#.#.#####.###.#.#.#####.#.#.#####
#.....#.#.#.#.#.#.#.#.........#...#...#.#.....#...#.#.......#.#.#.#...#...#.#.......#.............#.....#.#.#...........#.#.#
#.#####.###.#.#.#.#.#####.#.#####.###.#.###.#.#.#####.#.###.###.#.#.###.#.#.#.###.###.#######.#####.###########.#.###.###.#.#
#.#.....#.#.#.#.........#.#.#...#...#.....#.#.#.....#.#.#.#...#.#.#...#.#.#.#.#...#.....#.#.#.#...#...#.........#...#.......#
#.#####.#.#.#####.#.#######.#.#####.###.#.#.###.#.#####.#.#.#.###.###.#.###.###.#.#.#####.#.#####.#########.#.###.###.#.###.#
#.#.......#.#.#.#.#.#.#...#.#.....#...#.#.#...#.#.#.....#...#.#...#...#.#.....#.#.#.....#.....#.........#...#...#.#.#.#...#.#
#######.#####.#.###.#.###.#.#.#####.###.#####.#.#########.###.#####.###.#.#.###.#.###.###.#.###.#############.#####.#.#####.#
#.#.#...#.#.........#...........#.#.#...#.#...#.#.#...#...#.#...#.....#.#.#...#.#.........#.#.#.#.#...#...#.......#.....#.#.#
#.#.#.#.#.#########.#.#.###.#####.#.###.#.###.#.#.#.###.###.#######.###.#.###.#.#######.#####.#.#.#.#####.#.#.#####.#.###.#.#
#...#.#...#...#.......#.#.................#.#.#...#.....#.#.#.#...#...#.#...#.#.#.....#.#.#.....#.........#.#.....#.#.#.....#
#.#.#.#.###.#######.#.#.#.#.#.###.###.#.#.#.#.#.###.#.#.#.#.#.###.#.#.#.###.#.#.###.#.#.#.#####.#.#.#####.###.###.#####.#.#.#
#.#...#.#...#.......#.#.#.#.#.#.....#.#.#.#.#.#...#.#.#.......#.#.#.#.#.....#.#...#.#.#.#.........#...#.#.#.#...#...#.#.#.#.#
#####.###.###.#####.#.#######.###.#####.#.###.#.#########.#.###.#.###.#######.#.#.#.#.###.#.#.###.#.###.###.#.###.###.###.#.#
#.........#.#.#.....#.#.........#.#.....#...#.#...#...#...#.#.#...#.#...#.....#.#.#.#.#...#.#.#.#.#.........#.#.........#.#.#
#.###.#####.#####.#.###.###############.###.#.###.###.#.#####.#.#.#.#.#####.#####.#.#.#.#.#####.#.###.#####.###.###.#######.#
#.#.....#.........#.#...#.................#...#.......#.......#.#...#.#.........#...#.#.#.......#...#.#...#.#.#...#...#...#.#
#.#.#.#####.#.###.###.#.###.###.#.#.#.###.#####.###.###.#.#######.###.#######.#######.#.#.#######.#####.#####.#####.#.#.###.#
#.#.#.....#.#.#...#...#.#...#...#.#.#.#.......#...#.#...#.....#.........#.....#.........#.....#...................#.#.#.....#
#############################################.###.###########.#####.#####.#######.###########################################
b Y D R N S
b Y D R N S

+ 37
- 0
20-donut-maze/input.test View File

@@ -0,0 +1,37 @@
A
A
#################.#############
#.#...#...................#.#.#
#.#.#.###.###.###.#########.#.#
#.#.#.......#...#.....#.#.#...#
#.#########.###.#####.#.#.###.#
#.............#.#.....#.......#
###.###########.###.#####.#.#.#
#.....# B D #.#.#.#
####### B D #####.#
#.#...# #......JJ
#.#.#.# #.#####
#...#.# KK....#.#
#.###.# #####.#
EE....#.# #.....#
#####.# #.###.#
ZZ......# II....#..BB
###.### #######
FF..#.#.# #.....#
#.#.#.# ###.#.#
#...#..EE CC....#..HH
#####.# #.#####
KK......# JJ..#....II
#.###.# #.###.#
#.#...# #.....#
###.### F H G #.#.###
#.....# F H G #.#...#
#.###.#####.#.#####.#####.###.#
#...#.#.#...#.....#.....#.#...#
#.#####.###.###.#.#.#########.#
#...#.#.....#...#.#.#.#.....#.#
#.###.#####.###.###.#.#.#######
#.#.........#...#.............#
#########.###.###.#############
C G D
C G D

+ 3
- 0
20-donut-maze/run.sh View File

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

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

+ 116
- 0
21-springdroid-adventure/README.md View File

@@ -0,0 +1,116 @@
# Day 21: Springdroid Adventure

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

## Part One

You lift off from Pluto and start flying in the direction of Santa.

While experimenting further with the tractor beam, you accidentally pull an asteroid directly into your ship! It deals significant damage to your hull and causes your ship to begin tumbling violently.

You can send a droid out to investigate, but the tumbling is causing enough artificial gravity that one wrong step could send the droid through a hole in the hull and flying out into space.

The clear choice for this mission is a droid that can jump over the holes in the hull - a springdroid.

You can use an Intcode program (your puzzle input) running on an ASCII-capable computer to program the springdroid. However, springdroids don't run Intcode; instead, they run a simplified assembly language called springscript.

While a springdroid is certainly capable of navigating the artificial gravity and giant holes, it has one downside: it can only remember at most 15 springscript instructions.

The springdroid will move forward automatically, constantly thinking about whether to jump. The springscript program defines the logic for this decision.

Springscript programs only use Boolean values, not numbers or strings. Two registers are available: T, the temporary value register, and J, the jump register. If the jump register is true at the end of the springscript program, the springdroid will try to jump. Both of these registers start with the value false.

Springdroids have a sensor that can detect whether there is ground at various distances in the direction it is facing; these values are provided in read-only registers. Your springdroid can detect ground at four distances: one tile away (A), two tiles away (B), three tiles away (C), and four tiles away (D). If there is ground at the given distance, the register will be true; if there is a hole, the register will be false.

There are only three instructions available in springscript:

- AND X Y sets Y to true if both X and Y are true; otherwise, it sets Y to false.
- OR X Y sets Y to true if at least one of X or Y is true; otherwise, it sets Y to false.
- NOT X Y sets Y to true if X is false; otherwise, it sets Y to false.

In all three instructions, the second argument (Y) needs to be a writable register (either T or J). The first argument (X) can be any register (including A, B, C, or D).

For example, the one-instruction program NOT A J means "if the tile immediately in front of me is not ground, jump".

Or, here is a program that jumps if a three-tile-wide hole (with ground on the other side of the hole) is detected:

```
NOT A J
NOT B T
AND T J
NOT C T
AND T J
AND D J
```

The Intcode program expects ASCII inputs and outputs. It will begin by displaying a prompt; then, input the desired instructions one per line. End each line with a newline (ASCII code 10). When you have finished entering your program, provide the command WALK followed by a newline to instruct the springdroid to begin surveying the hull.

If the springdroid falls into space, an ASCII rendering of the last moments of its life will be produced. In these, @ is the springdroid, # is hull, and . is empty space. For example, suppose you program the springdroid like this:

```
NOT D J
WALK
```

This one-instruction program sets J to true if and only if there is no ground four tiles away. In other words, it attempts to jump into any hole it finds:

```
.................
.................
@................
#####.###########

.................
.................
.@...............
#####.###########

.................
..@..............
.................
#####.###########

...@.............
.................
.................
#####.###########

.................
....@............
.................
#####.###########

.................
.................
.....@...........
#####.###########

.................
.................
.................
#####@###########
```

However, if the springdroid successfully makes it across, it will use an output instruction to indicate the amount of damage to the hull as a single giant integer outside the normal ASCII range.

Program the springdroid with logic that allows it to survey the hull without falling into space. What amount of hull damage does it report?

Your puzzle answer was `19347868`.

## Part Two

There are many areas the springdroid can't reach. You flip through the manual and discover a way to increase its sensor range.

Instead of ending your springcode program with WALK, use RUN. Doing this will enable extended sensor mode, capable of sensing ground up to nine tiles away. This data is available in five new read-only registers:

- Register E indicates whether there is ground five tiles away.
- Register F indicates whether there is ground six tiles away.
- Register G indicates whether there is ground seven tiles away.
- Register H indicates whether there is ground eight tiles away.
- Register I indicates whether there is ground nine tiles away.

All other functions remain the same.

Successfully survey the rest of the hull by ending your program with RUN. What amount of hull damage does the springdroid now report?

Your puzzle answer was `1142479667`.

+ 39
- 0
21-springdroid-adventure/answer.hs View File

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

import Intcode

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

let directions = ["NOT A J",
"AND B J",
"NOT A T",
"AND C T",
"OR T J",
"NOT B T",
"AND D T",
"OR T J",
"NOT C T",
"AND D T",
"OR T J",
"WALK"]
putStr $ parseOutput $ computeTilHalt $ startingState parsed $ parseInput directions

let directions = ["NOT A J",
"AND B J",
"NOT A T",
"AND C T",
"OR T J",
"NOT B T",
"AND D T",
"OR T J",
"NOT C T",
"AND D T",
"AND H T",
"OR T J",
"RUN"]
putStr $ parseOutput $ computeTilHalt $ startingState parsed $ parseInput directions

+ 1
- 0
21-springdroid-adventure/input
File diff suppressed because it is too large
View File


+ 3
- 0
21-springdroid-adventure/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

@@ -34,3 +34,5 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
- [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)
- [Day 20: Donut Maze](20-donut-maze)
- [Day 21: Springdroid Adventure](21-springdroid-adventure)

Loading…
Cancel
Save