From 8647f68ff73192d6e13314afbf4ed3b498c4d284 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 12 Dec 2019 09:55:34 +0000 Subject: [PATCH] Day 11 done --- advent11/package.yaml | 60 ++++++++++++++ advent11/src/advent11.hs | 140 +++++++++++++++++++++++++++++++ data/advent11.txt | 1 + problems/day11.html | 173 +++++++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 5 files changed, 375 insertions(+) create mode 100644 advent11/package.yaml create mode 100644 advent11/src/advent11.hs create mode 100644 data/advent11.txt create mode 100644 problems/day11.html diff --git a/advent11/package.yaml b/advent11/package.yaml new file mode 100644 index 0000000..eda3fb6 --- /dev/null +++ b/advent11/package.yaml @@ -0,0 +1,60 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent11 +synopsis: Advent of Code +version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NegativeLiterals +- NumDecimals +- OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeInType +- TypeOperators +- ViewPatterns + + +executables: + advent11: + main: advent11.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - text + - containers + - intcode diff --git a/advent11/src/advent11.hs b/advent11/src/advent11.hs new file mode 100644 index 0000000..264ce31 --- /dev/null +++ b/advent11/src/advent11.hs @@ -0,0 +1,140 @@ +import Debug.Trace + +import Intcode + +import qualified Data.Text.IO as TIO + +import qualified Data.Map.Strict as M +-- import Data.Map.Strict ((!)) +-- import Data.List +-- import Data.Function (on) + +type Position = (Int, Int) -- x, y +data Colour = Black | White deriving (Show, Eq, Ord) +data Direction = North | East | South | West deriving (Show, Eq, Ord, Enum, Bounded) + +data Ant = Ant + { _machine :: Machine + , _executionState :: ExecutionState + , _currentInput :: [Integer] + , _machineOutput :: [Integer] + , _currentPosition :: Position + , _currentDirection :: Direction + } deriving (Show, Eq) + +type Hull = M.Map Position Colour + + +main :: IO () +main = do + text <- TIO.readFile "data/advent11.txt" + let mem = parseMachineMemory text + -- print mem + print $ part1 mem + putStrLn $ part2 mem + + +part1 mem = M.size hull + where ant = encapsulate mem [] + hull = runAnt ant M.empty + +part2 mem = showHull hull + where ant = encapsulate mem [] + hull = runAnt ant (M.singleton (0, 0) White) + +encapsulate :: [Integer] -> [Integer] -> Ant +encapsulate mem input = Ant + { _machine = makeMachine mem + , _executionState = Runnable + , _machineOutput = [] + , _currentInput = input + , _currentPosition = (0, 0) + , _currentDirection = North + } + + +runAnt :: Ant -> Hull -> Hull +-- runAnt ant hull | trace (show ant ++ " -> " ++ (show (runAntStep ant)) ++ " -- " ++ show hull) False = undefined +runAnt ant hull = hull'' + where ant' = runAntStep ant + output = _machineOutput ant' + hull' = if (null output) then hull else paint hull ant' (output!!0) + ant'' = if (null output) then ant' else move ant' (output!!1) + ant''' = camera ant'' hull + hull'' = if (_executionState ant' == Terminated) + then hull' + else runAnt ant''' hull' + + +paint :: Hull -> Ant -> Integer -> Hull +paint hull ant 0 = M.insert (_currentPosition ant) Black hull +paint hull ant 1 = M.insert (_currentPosition ant) White hull + +move :: Ant -> Integer -> Ant +move ant angle = ant { _currentDirection = direction', _currentPosition = position' } + where direction' = turn (_currentDirection ant) angle + delta = directionDelta direction' + position' = sumPos delta $ _currentPosition ant + +camera :: Ant -> Hull -> Ant +camera ant hull = ant { _currentInput = input' } + where colour = M.findWithDefault Black (_currentPosition ant) hull + input = _currentInput ant + input' = input ++ [colourNum colour] + +colourNum :: Colour -> Integer +colourNum Black = 0 +colourNum White = 1 + +turn :: Direction -> Integer -> Direction +turn direction 0 = predW direction +turn direction 1 = succW direction + +directionDelta :: Direction -> Position +directionDelta North = ( 0 , 1) +directionDelta East = ( 1 , 0) +directionDelta South = ( 0 , -1) +directionDelta West = (-1 , 0) + +sumPos :: Position -> Position -> Position +sumPos (a, b) (c, d) = (a + c, b + d) + + +runAntStep :: Ant -> Ant +runAntStep a = a { _machine = machine' + , _executionState = halted + , _machineOutput = output + } + where machine = _machine a + input = _currentInput a + (halted, machine', output) = runMachine input machine + + +showHull :: Hull -> String +showHull hull = unlines rows + where minX = minimum $ map fst $ M.keys hull + minY = minimum $ map snd $ M.keys hull + maxX = maximum $ map fst $ M.keys hull + maxY = maximum $ map snd $ M.keys hull + rows = [showHullRow hull minX maxX y | y <- reverse [minY..maxY]] + +showHullRow :: Hull -> Int -> Int -> Int -> String +showHullRow hull minX maxX y = [showHullCell hull x y | x <- [minX..maxX]] + +showHullCell :: Hull -> Int -> Int -> Char +showHullCell hull x y + | colour == White = '\x2588' + | colour == Black = ' ' + where colour = M.findWithDefault Black (x, y) hull + + +-- | a `succ` that wraps +succW :: (Bounded a, Enum a, Eq a) => a -> a +succW dir | dir == maxBound = minBound + | otherwise = succ dir + +-- | a `pred` that wraps +predW :: (Bounded a, Enum a, Eq a) => a -> a +predW dir | dir == minBound = maxBound + | otherwise = pred dir + diff --git a/data/advent11.txt b/data/advent11.txt new file mode 100644 index 0000000..1db3e3c --- /dev/null +++ b/data/advent11.txt @@ -0,0 +1 @@ +3,8,1005,8,314,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,28,2,2,16,10,1,1108,7,10,1006,0,10,1,5,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,65,1006,0,59,2,109,1,10,1006,0,51,2,1003,12,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,101,1006,0,34,1,1106,0,10,1,1101,17,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,135,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,156,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,178,1,108,19,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,204,1,1006,17,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,230,1006,0,67,1,103,11,10,1,1009,19,10,1,109,10,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,101,0,8,268,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,290,2,108,13,10,101,1,9,9,1007,9,989,10,1005,10,15,99,109,636,104,0,104,1,21101,48210224024,0,1,21101,0,331,0,1105,1,435,21101,0,937264165644,1,21101,0,342,0,1105,1,435,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,235354025051,0,1,21101,389,0,0,1105,1,435,21102,29166169280,1,1,21102,400,1,0,1105,1,435,3,10,104,0,104,0,3,10,104,0,104,0,21102,709475849060,1,1,21102,1,423,0,1106,0,435,21102,868498428684,1,1,21101,434,0,0,1105,1,435,99,109,2,21201,-1,0,1,21101,0,40,2,21102,1,466,3,21101,456,0,0,1105,1,499,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,461,462,477,4,0,1001,461,1,461,108,4,461,10,1006,10,493,1101,0,0,461,109,-2,2106,0,0,0,109,4,2102,1,-1,498,1207,-3,0,10,1006,10,516,21102,1,0,-3,21201,-3,0,1,21201,-2,0,2,21102,1,1,3,21102,535,1,0,1106,0,540,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,563,2207,-4,-2,10,1006,10,563,21202,-4,1,-4,1106,0,631,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21101,582,0,0,1105,1,540,22102,1,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,601,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,623,22102,1,-1,1,21101,623,0,0,105,1,498,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 diff --git a/problems/day11.html b/problems/day11.html new file mode 100644 index 0000000..9e50c29 --- /dev/null +++ b/problems/day11.html @@ -0,0 +1,173 @@ + + + + +Day 11 - Advent of Code 2019 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 22*

   0x0000|2019

+ + + +
+ +

--- Day 11: Space Police ---

On the way to Jupiter, you're pulled over by the Space Police.

+

"Attention, unmarked spacecraft! You are in violation of Space Law! All spacecraft must have a clearly visible registration identifier! You have 24 hours to comply or be sent to Space Jail!"

+

Not wanting to be sent to Space Jail, you radio back to the Elves on Earth for help. Although it takes almost three hours for their reply signal to reach you, they send instructions for how to power up the emergency hull painting robot and even provide a small Intcode program (your puzzle input) that will cause it to paint your ship appropriately.

+

There's just one problem: you don't have an emergency hull painting robot.

+

You'll need to build a new emergency hull painting robot. The robot needs to be able to move around on the grid of square panels on the side of your ship, detect the color of its current panel, and paint its current panel black or white. (All of the panels are currently black.)

+

The Intcode program will serve as the brain of the robot. The program uses input instructions to access the robot's camera: provide 0 if the robot is over a black panel or 1 if the robot is over a white panel. Then, the program will output two values:

+
    +
  • First, it will output a value indicating the color to paint the panel the robot is over: 0 means to paint the panel black, and 1 means to paint the panel white.
  • +
  • Second, it will output a value indicating the direction the robot should turn: 0 means it should turn left 90 degrees, and 1 means it should turn right 90 degrees.
  • +
+

After the robot turns, it should always move forward exactly one panel. The robot starts facing up.

+

The robot will continue running for a while like this and halt when it is finished drawing. Do not restart the Intcode computer inside the robot during this process.

+

For example, suppose the robot is about to start running. Drawing black panels as ., white panels as #, and the robot pointing the direction it is facing (< ^ > v), the initial state and region near the robot looks like this:

+
.....
+.....
+..^..
+.....
+.....
+
+

The panel under the robot (not visible here because a ^ is shown instead) is also black, and so any input instructions at this point should be provided 0. Suppose the robot eventually outputs 1 (paint white) and then 0 (turn left). After taking these actions and moving forward one panel, the region now looks like this:

+
.....
+.....
+.<#..
+.....
+.....
+
+

Input instructions should still be provided 0. Next, the robot might output 0 (paint black) and then 0 (turn left):

+
.....
+.....
+..#..
+.v...
+.....
+
+

After more outputs (1,0, 1,0):

+
.....
+.....
+..^..
+.##..
+.....
+
+

The robot is now back where it started, but because it is now on a white panel, input instructions should be provided 1. After several more outputs (0,1, 1,0, 1,0), the area looks like this:

+
.....
+..<#.
+...#.
+.##..
+.....
+
+

Before you deploy the robot, you should probably have an estimate of the area it will cover: specifically, you need to know the number of panels it paints at least once, regardless of color. In the example above, the robot painted 6 panels at least once. (It painted its starting panel twice, but that panel is still only counted once; it also never painted the panel it ended on.)

+

Build a new emergency hull painting robot and run the Intcode program on it. How many panels does it paint at least once?

+
+

Your puzzle answer was 2343.

--- Part Two ---

You're not sure what it's trying to paint, but it's definitely not a registration identifier. The Space Police are getting impatient.

+

Checking your external ship cameras again, you notice a white panel marked "emergency hull painting robot starting panel". The rest of the panels are still black, but it looks like the robot was expecting to start on a white panel, not a black one.

+

Based on the Space Law Space Brochure that the Space Police attached to one of your windows, a valid registration identifier is always eight capital letters. After starting the robot on a single white panel instead, what registration identifier does it paint on your hull?

+
+

Your puzzle answer was JFBERBUH.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 1e2f684..70ad0dd 100644 --- a/stack.yaml +++ b/stack.yaml @@ -48,6 +48,7 @@ packages: - advent08 - advent09 - advent10 +- advent11 # Dependency packages to be pulled from upstream that are not in the resolver. -- 2.34.1