ctrl k
  • 15-oxygen-system/README.md
    ■ ■ ■ ■ ■ ■
     1 +# Day 15: Oxygen System
     2 + 
     3 +[Day 15 Challenge](https://adventofcode.com/2019/day/15)
     4 + 
     5 +## Part One
     6 + 
     7 +Out here in deep space, many things can go wrong. Fortunately, many of those things have indicator lights. Unfortunately, one of those lights is lit: the oxygen system for part of the ship has failed!
     8 + 
     9 +According to the readouts, the oxygen system must have failed days ago after a rupture in oxygen tank two; that section of the ship was automatically sealed once oxygen levels went dangerously low. A single remotely-operated repair droid is your only option for fixing the oxygen system.
     10 + 
     11 +The Elves' care package included an Intcode program (your puzzle input) that you can use to remotely control the repair droid. By running that program, you can direct the repair droid to the oxygen system and fix the problem.
     12 + 
     13 +The remote control program executes the following steps in a loop forever:
     14 + 
     15 + - Accept a movement command via an input instruction.
     16 + - Send the movement command to the repair droid.
     17 + - Wait for the repair droid to finish the movement operation.
     18 + - Report on the status of the repair droid via an output instruction.
     19 + 
     20 +Only four movement commands are understood: north (1), south (2), west (3), and east (4). Any other command is invalid. The movements differ in direction, but not in distance: in a long enough east-west hallway, a series of commands like 4,4,4,4,3,3,3,3 would leave the repair droid back where it started.
     21 + 
     22 +The repair droid can reply with any of the following status codes:
     23 + 
     24 + - 0: The repair droid hit a wall. Its position has not changed.
     25 + - 1: The repair droid has moved one step in the requested direction.
     26 + - 2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.
     27 + 
     28 +You don't know anything about the area around the repair droid, but you can figure it out by watching the status codes.
     29 + 
     30 +For example, we can draw the area using D for the droid, # for walls, . for locations the droid can traverse, and empty space for unexplored locations. Then, the initial state looks like this:
     31 + 
     32 +```
     33 + 
     34 + 
     35 + D
     36 + 
     37 + 
     38 +```
     39 + 
     40 +To make the droid go north, send it 1. If it replies with 0, you know that location is a wall and that the droid didn't move:
     41 + 
     42 +```
     43 + 
     44 + #
     45 + D
     46 + 
     47 + 
     48 +```
     49 + 
     50 +To move east, send 4; a reply of 1 means the movement was successful:
     51 + 
     52 +```
     53 + 
     54 + #
     55 + .D
     56 + 
     57 + 
     58 +```
     59 + 
     60 +Then, perhaps attempts to move north (1), south (2), and east (4) are all met with replies of 0:
     61 + 
     62 +```
     63 + 
     64 + ##
     65 + .D#
     66 + #
     67 + 
     68 +```
     69 + 
     70 +Now, you know the repair droid is in a dead end. Backtrack with 3 (which you already know will get a reply of 1 because you already know that location is open):
     71 + 
     72 +```
     73 + 
     74 + ##
     75 + D.#
     76 + #
     77 + 
     78 +```
     79 + 
     80 +Then, perhaps west (3) gets a reply of 0, south (2) gets a reply of 1, south again (2) gets a reply of 0, and then west (3) gets a reply of 2:
     81 + 
     82 +```
     83 + 
     84 + ##
     85 + #..#
     86 + D.#
     87 + #
     88 +```
     89 + 
     90 +Now, because of the reply of 2, you know you've found the oxygen system! In this example, it was only 2 moves away from the repair droid's starting position.
     91 + 
     92 +What is the fewest number of movement commands required to move the repair droid from its starting position to the location of the oxygen system?
     93 + 
     94 +Your puzzle answer was `204`.
     95 + 
     96 +## Part Two
     97 + 
     98 +You quickly repair the oxygen system; oxygen gradually fills the area.
     99 + 
     100 +Oxygen starts in the location containing the repaired oxygen system. It takes one minute for oxygen to spread to all open locations that are adjacent to a location that already contains oxygen. Diagonal locations are not adjacent.
     101 + 
     102 +In the example above, suppose you've used the droid to explore the area fully and have the following map (where locations that currently contain oxygen are marked O):
     103 + 
     104 +```
     105 + ##
     106 +#..##
     107 +#.#..#
     108 +#.O.#
     109 + ###
     110 +```
     111 + 
     112 +Initially, the only location which contains oxygen is the location of the repaired oxygen system. However, after one minute, the oxygen spreads to all open (.) locations that are adjacent to a location containing oxygen:
     113 + 
     114 +```
     115 + ##
     116 +#..##
     117 +#.#..#
     118 +#OOO#
     119 + ###
     120 +```
     121 + 
     122 +After a total of two minutes, the map looks like this:
     123 + 
     124 +```
     125 + ##
     126 +#..##
     127 +#O#O.#
     128 +#OOO#
     129 + ###
     130 +```
     131 + 
     132 +After a total of three minutes:
     133 + 
     134 +```
     135 + ##
     136 +#O.##
     137 +#O#OO#
     138 +#OOO#
     139 + ###
     140 +```
     141 + 
     142 +And finally, the whole region is full of oxygen after a total of four minutes:
     143 + 
     144 +```
     145 + ##
     146 +#OO##
     147 +#O#OO#
     148 +#OOO#
     149 + ###
     150 +```
     151 + 
     152 +So, in this example, all locations contain oxygen after 4 minutes.
     153 + 
     154 +Use the repair droid to get a complete map of the area. How many minutes will it take to fill with oxygen?
     155 + 
     156 +Your puzzle answer was `340`.
     157 + 
  • 15-oxygen-system/answer.hs
    ■ ■ ■ ■ ■ ■
    skipped 45 lines
    46 46  startingState :: Map.Map Int Int -> [Int] -> ((Map.Map Int Int, Int, Int), ([Int], [Int]))
    47 47  startingState prog input = ((prog, 0, 0), (input, []))
    48 48   
    49  -getPoints :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> Map.Map (Int, Int) (Int, Int) -> (Int, Int) -> Int -> Map.Map (Int, Int) (Int, Int)
    50  -getPoints ((prog, y, z), (inputs, outputs)) map pos dis = if y == -1 then map else if lastObj == 0 then map else List.foldl Map.union Map.empty $ List.map (\(i, p) -> calcNext ((prog, y, z), (inputs, outputs)) map i p (dis+1)) (unmappedNeighbors map pos)
     49 +getPoints :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> Map.Map (Int, Int) Int -> (Int, Int) -> Map.Map (Int, Int) Int
     50 +getPoints ((prog, y, z), (inputs, outputs)) map pos = if y == -1 then map else if lastObj == 0 then map else List.foldl Map.union Map.empty $ List.map (\(i, p) -> calcNext ((prog, y, z), (inputs, outputs)) map i p) (unmappedNeighbors map pos)
    51 51   where
    52 52   lastObj = if length outputs > 0 then outputs!!0 else -1
    53  - unmappedNeighbors m (x, y) = filter (\(i, p) -> not . isJust $ Map.lookup p map) [(1, (x, y+1)), (2, (x, y-1)), (3, (x-1, y)), (4, (x+1, y))]
     53 + unmappedNeighbors m (x, y) = filter (\(i, p) -> not . isJust $ Map.lookup p m) [(1, (x, y+1)), (2, (x, y-1)), (3, (x-1, y)), (4, (x+1, y))]
    54 54   
    55  -calcNext :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> Map.Map (Int, Int) (Int, Int) -> Int -> (Int, Int) -> Int -> Map.Map (Int, Int) (Int, Int)
    56  -calcNext (state, (inputs, outputs)) map dir newPos newDis = getPoints (nextState, (newInput, newObj:newOutputs)) (Map.insert newPos (newObj, newDis) map) newPos newDis
     55 +calcNext :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> Map.Map (Int, Int) Int -> Int -> (Int, Int) -> Map.Map (Int, Int) Int
     56 +calcNext (state, (inputs, outputs)) map dir newPos = getPoints (nextState, (newInput, newObj:newOutputs)) (Map.insert newPos newObj map) newPos
    57 57   where (nextState, (newInput, newObj:newOutputs)) = compute state ((dir:inputs), outputs)
    58 58   
    59  -spreadOxygen :: Map.Map (Int, Int) (Int, Int) -> Map.Map (Int, Int) (Int, Int) -> (Int, Int) -> Int -> Map.Map (Int, Int) (Int, Int)
    60  -spreadOxygen map seen pos dis = if lastObj == 0 then seen else List.foldl Map.union Map.empty $ List.map (\p -> calcNextOxy map seen p (dis+1)) (unmappedNeighbors map pos)
     59 +bfs :: Map.Map (Int, Int) Int -> Map.Map (Int, Int) (Int, Int) -> (Int, Int) -> Int -> Map.Map (Int, Int) (Int, Int)
     60 +bfs map seen pos dis = if lastObj == 0 then seen else List.foldl Map.union Map.empty $ List.map (\p -> bfs map (Map.insert p ((map Map.! p), dis+1) seen) p (dis+1)) (unmappedNeighbors seen pos)
    61 61   where
    62  - lastObj = fst $ map Map.! pos
    63  - unmappedNeighbors m (x, y) = filter (\p -> not . isJust $ Map.lookup p seen) [(x, y+1), (x, y-1), (x-1, y), (x+1, y)]
     62 + lastObj = map Map.! pos
     63 + unmappedNeighbors m (x, y) = filter (\p -> not . isJust $ Map.lookup p m) [(x, y+1), (x, y-1), (x-1, y), (x+1, y)]
    64 64   
    65  -calcNextOxy :: Map.Map (Int, Int) (Int, Int) -> Map.Map (Int, Int) (Int, Int) -> (Int, Int) -> Int -> Map.Map (Int, Int) (Int, Int)
    66  -calcNextOxy map seen newPos newDis = spreadOxygen map (Map.insert newPos ((fst $ map Map.! newPos), newDis) seen) newPos newDis
    67  - 
    68  -solveA :: Map.Map (Int, Int) (Int, Int) -> ((Int, Int), (Int, Int))
    69  -solveA = flip (!!) 0 . Map.toList . Map.filter ((==) 2 . fst)
    70  - 
    71  -solveB :: Map.Map (Int, Int) (Int, Int) -> (Int, Int) -> Int
    72  -solveB map pos = snd $ maximumBy (comparing snd) $ Map.elems $ Map.filter ((== 1) . fst) $ spreadOxygen map (Map.singleton pos (2, 0)) pos 0
     65 +getMaxDis :: Map.Map (Int, Int) Int -> (Int, Int) -> Int -> Int
     66 +getMaxDis map pos item = snd $ maximumBy (comparing snd) $ Map.elems $ Map.filter ((== item) . fst) $ bfs map (Map.singleton pos (2, 0)) pos 0
    73 67   
    74 68  main = do
    75 69   input <- getArgs
    76 70   let parsed = parse $ input!!0
    77 71   
    78  - let map = getPoints (startingState parsed []) (Map.singleton (0, 0) (1, 0)) (0, 0) 0
    79  - let (pos, (item, dis)) = solveA map
    80  - print dis
    81  - print $ solveB map pos
     72 + let map = getPoints (startingState parsed []) (Map.singleton (0, 0) 1) (0, 0)
     73 + print $ getMaxDis map (0, 0) 2
     74 + print $ getMaxDis map (-16, -12) 1
    82 75   
Please wait...
Page is in error, reload to recover