From: Neil Smith Date: Mon, 14 Dec 2020 17:24:54 +0000 (+0000) Subject: Done day 8 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=90918214710d0fc62c1f70114b15aa677223494b;p=advent-of-code-20.git Done day 8 --- diff --git a/advent08/package.yaml b/advent08/package.yaml new file mode 100644 index 0000000..f0fd36c --- /dev/null +++ b/advent08/package.yaml @@ -0,0 +1,63 @@ +# 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: advent08 +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 +- NamedFieldPuns +- NegativeLiterals +- NumDecimals +# - OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeFamilies +- TypeInType +- TypeOperators +- ViewPatterns + +executables: + advent08: + main: advent08.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - text + - attoparsec + - containers + - vector + \ No newline at end of file diff --git a/advent08/src/advent08.hs b/advent08/src/advent08.hs new file mode 100644 index 0000000..dab483e --- /dev/null +++ b/advent08/src/advent08.hs @@ -0,0 +1,98 @@ +-- import Debug.Trace + +import Data.Text (Text) +import qualified Data.Text as T +import qualified Data.Text.IO as TIO + +import Data.Attoparsec.Text +-- import Data.Attoparsec.Combinator +import Control.Applicative + +import qualified Data.Set as S +import qualified Data.Vector as V +import Data.Vector ((!), (//)) + +data Instruction = Acc Int | Jmp Int | Nop Int + deriving (Show, Eq) + +type Program = V.Vector Instruction + +data Machine = Machine + { machineProgram :: Program + , machineIP :: Int + , machineAcc :: Int + } + +data MachineResult = Looped Int | Terminated Int | OutOfBounds Int Int + deriving (Show, Eq) + + +main :: IO () +main = + do text <- TIO.readFile "data/advent08.txt" + let program = successfulParse text + -- print program + let machine = Machine program 0 0 + print $ part1 machine + print $ part2 program + +part1 machine = executeMany S.empty machine + +part2 program = filter terminates $ map runProgram programs + where programs = altPrograms program + runProgram p = executeMany S.empty (Machine p 0 0) + terminates (Terminated _) = True + terminates (OutOfBounds _ _) = True + terminates _ = False + + +executeMany visited machine + -- if currentIP `S.member` visited + -- then Looped (machineAcc machine) + -- else if currentIP == programSize + -- then Terminated (machineAcc machine) + -- else if currentIP > programSize + -- then OutOfBounds (machineAcc machine) currentIP + -- else executeMany visited' machine' + | currentIP `S.member` visited = Looped (machineAcc machine) + | currentIP == programSize = Terminated (machineAcc machine) + | currentIP > programSize = OutOfBounds (machineAcc machine) currentIP + | otherwise = executeMany visited' machine' + where machine' = executeStep machine + currentIP = machineIP machine + visited' = S.insert currentIP visited + programSize = V.length $ machineProgram machine + + +executeStep m = execute ((machineProgram m)!(machineIP m)) m + +execute (Acc n) m = m { machineIP = machineIP m + 1 + , machineAcc = machineAcc m + n + } +execute (Jmp n) m = m { machineIP = machineIP m + n } +execute (Nop n) m = m { machineIP = machineIP m + 1 } + + +altPrograms program = map (mutateProgram program) [0..(V.length program - 1)] + +mutateProgram program i = go (program!i) + where go (Nop n) = program // [(i, Jmp n)] + go (Jmp n) = program // [(i, Nop n)] + go _ = program + +-- -- Parse the input file + + +instructionP = accP <|> jmpP <|> nopP + +accP = Acc <$> ("acc " *> signed decimal) +jmpP = Jmp <$> ("jmp " *> signed decimal) +nopP = Nop <$> ("nop " *> signed decimal) + +programP = V.fromList <$> sepBy instructionP endOfLine + +successfulParse :: Text -> Program +successfulParse input = + case parseOnly programP input of + Left _err -> V.empty -- TIO.putStr $ T.pack $ parseErrorPretty err + Right program -> program diff --git a/data/advent08.txt b/data/advent08.txt new file mode 100644 index 0000000..1aac5a0 --- /dev/null +++ b/data/advent08.txt @@ -0,0 +1,644 @@ +acc +17 +jmp +1 +acc +16 +acc +15 +jmp +161 +acc +37 +acc +5 +acc -13 +nop +134 +jmp +426 +acc +26 +acc +0 +jmp +262 +acc -1 +nop +266 +jmp +486 +jmp +149 +acc +20 +acc +27 +jmp +564 +acc +35 +acc +9 +acc +31 +acc +0 +jmp +81 +acc -7 +nop +142 +acc +11 +jmp +125 +jmp +610 +jmp +534 +nop +42 +jmp +469 +acc +48 +acc +47 +acc -4 +jmp +114 +jmp +150 +nop +311 +acc +25 +acc +19 +jmp -4 +acc +48 +acc +38 +acc -7 +jmp +292 +acc +19 +jmp +386 +acc +15 +acc -7 +acc -8 +acc +32 +jmp +98 +acc +33 +jmp +460 +acc +7 +acc +30 +jmp +428 +acc -5 +acc +30 +acc +1 +jmp +305 +jmp +91 +nop -47 +acc +23 +acc +33 +acc +49 +jmp +538 +acc +19 +nop +560 +acc +17 +jmp +362 +jmp +530 +jmp +182 +acc +50 +acc +46 +acc -1 +nop +60 +jmp +39 +acc +36 +acc +28 +acc +49 +acc -13 +jmp +93 +acc -17 +acc +20 +acc -8 +jmp -71 +nop +454 +acc -1 +jmp +365 +acc -13 +acc -3 +acc +5 +jmp +479 +acc +39 +jmp +531 +jmp +68 +acc -15 +acc +10 +acc +19 +jmp +508 +acc +6 +nop +301 +jmp -9 +acc +28 +acc +29 +nop -65 +jmp +116 +acc -4 +acc -3 +jmp -63 +acc -5 +jmp -71 +acc +9 +acc +19 +jmp +34 +jmp +346 +nop +484 +nop +278 +nop +284 +jmp +120 +acc -15 +acc -4 +acc +32 +acc -3 +jmp +461 +acc +24 +acc -7 +jmp -7 +acc +15 +jmp +349 +acc +7 +acc +6 +acc -15 +acc +8 +jmp +402 +jmp +274 +acc +15 +acc +28 +acc +47 +jmp +491 +acc +39 +acc +37 +jmp -65 +nop +31 +nop +437 +nop +183 +acc +33 +jmp +469 +nop +26 +jmp +260 +jmp +20 +acc -19 +acc +11 +acc +8 +jmp +337 +acc +41 +nop +55 +jmp -64 +acc +33 +acc -5 +jmp +79 +acc +43 +jmp +28 +acc +39 +jmp +231 +jmp +268 +acc -10 +jmp +360 +acc +50 +jmp +91 +acc +37 +acc +16 +jmp +170 +jmp +162 +jmp +426 +nop +396 +acc +5 +jmp +276 +acc -14 +acc +1 +acc -7 +acc -17 +jmp +399 +acc +4 +jmp +443 +acc +24 +acc +44 +acc +29 +jmp -161 +jmp -4 +acc -2 +acc -9 +acc +29 +jmp -164 +acc +43 +acc +17 +nop +395 +jmp +68 +acc -10 +jmp +1 +jmp -114 +acc +6 +nop +70 +acc +19 +jmp +191 +jmp +156 +acc -11 +nop +128 +acc +49 +acc +6 +jmp +390 +acc -2 +acc -4 +acc +11 +jmp +257 +jmp +71 +nop +350 +nop +87 +jmp +47 +acc -10 +acc +3 +jmp +345 +acc +21 +acc +6 +jmp -89 +acc +26 +acc +16 +acc -18 +acc +21 +jmp +43 +acc +41 +acc -4 +acc +39 +acc +27 +jmp +227 +acc +21 +nop +183 +acc +5 +jmp +164 +acc +36 +jmp +6 +acc +35 +acc -4 +acc +30 +acc +15 +jmp -82 +acc +29 +acc +8 +nop -138 +jmp +1 +jmp +128 +acc +41 +jmp +288 +acc +12 +acc +36 +jmp +172 +acc +42 +acc +14 +acc +8 +jmp +160 +acc -2 +nop +200 +acc +15 +jmp +105 +jmp -224 +acc +44 +nop +161 +acc +0 +acc +48 +jmp +252 +acc +35 +jmp +354 +nop +114 +acc +10 +acc -18 +jmp +268 +acc +28 +jmp +70 +nop -80 +acc +44 +jmp -220 +acc -4 +acc +1 +acc +43 +acc +39 +jmp -232 +nop -257 +acc -3 +jmp -195 +acc -10 +acc +42 +acc +47 +nop -70 +jmp -145 +acc +21 +jmp +1 +acc +19 +acc +8 +jmp +140 +acc +10 +acc +20 +acc +22 +acc +18 +jmp -129 +acc +40 +jmp +244 +jmp +1 +nop +112 +acc +26 +acc +4 +jmp -162 +acc +2 +acc +29 +jmp +1 +jmp -98 +acc +11 +nop +117 +acc +33 +acc +36 +jmp +208 +nop -172 +acc +13 +acc -4 +acc +48 +jmp -264 +acc +33 +acc +37 +jmp -256 +acc +8 +jmp -51 +acc +43 +acc +19 +acc +48 +acc +41 +jmp +119 +acc +46 +acc +39 +acc +37 +jmp +289 +acc +48 +acc -16 +jmp -136 +jmp +1 +acc +40 +jmp -16 +nop +246 +jmp -300 +acc -6 +jmp +110 +acc +34 +nop -315 +jmp -316 +acc -9 +acc +10 +acc +19 +jmp -347 +acc +37 +acc +49 +acc +45 +acc +35 +jmp -74 +jmp +52 +nop -222 +jmp -141 +acc -8 +jmp +153 +acc +6 +jmp +258 +acc +13 +acc +13 +acc +25 +acc +45 +jmp +140 +acc +12 +acc +4 +acc +6 +acc -13 +jmp +122 +acc +17 +acc -19 +jmp -314 +acc -9 +acc +10 +acc -14 +acc +40 +jmp -345 +acc +42 +acc -6 +acc +10 +jmp -231 +acc -14 +acc +0 +acc -4 +nop +110 +jmp +177 +acc +29 +acc +38 +jmp +121 +acc +28 +acc +6 +acc +12 +jmp +98 +acc -3 +jmp +153 +acc +49 +acc -1 +acc +44 +acc -10 +jmp +119 +jmp -198 +acc +40 +acc -2 +jmp -17 +acc +13 +acc +41 +acc -2 +jmp +28 +nop -351 +jmp -66 +acc +4 +acc +0 +acc +20 +nop -393 +jmp -137 +acc -10 +acc -14 +jmp +101 +jmp -409 +jmp +1 +acc +17 +jmp +173 +jmp +105 +jmp -46 +acc +12 +jmp -319 +nop -284 +nop -340 +jmp +16 +acc +25 +acc -13 +acc +48 +acc -8 +jmp -90 +jmp +113 +acc +28 +acc +20 +acc +3 +nop -289 +jmp +168 +acc +27 +nop -17 +acc -12 +jmp -50 +jmp -248 +acc +38 +acc +21 +jmp +1 +jmp -364 +nop +104 +jmp +103 +jmp -425 +jmp +1 +acc -1 +acc +20 +acc +42 +jmp +38 +acc +20 +acc +5 +acc +0 +acc +20 +jmp -118 +nop +154 +acc +39 +acc -1 +acc -4 +jmp -155 +jmp +1 +jmp +35 +acc +10 +acc +7 +acc +18 +jmp -473 +jmp +84 +acc +14 +acc +20 +jmp -295 +nop -8 +nop -271 +acc -19 +acc -6 +jmp +50 +acc +24 +acc -5 +acc +41 +nop -229 +jmp -329 +acc +40 +acc -10 +jmp -112 +jmp +1 +nop -372 +jmp -137 +jmp -242 +acc +39 +acc +30 +acc +30 +jmp -396 +acc +4 +acc +2 +nop -114 +acc +30 +jmp +114 +acc +48 +acc -7 +acc +28 +jmp -462 +acc +24 +jmp -359 +jmp -272 +acc -17 +jmp +46 +jmp -227 +acc -3 +acc -11 +acc +10 +acc +16 +jmp -5 +jmp -387 +acc +46 +jmp -269 +acc -7 +acc +30 +jmp +1 +jmp -519 +acc +50 +jmp -23 +nop -105 +jmp -359 +acc +19 +jmp -106 +jmp +26 +nop -128 +jmp -123 +acc -7 +acc +12 +jmp -474 +acc +11 +acc +30 +jmp -530 +acc -17 +jmp -504 +acc +20 +acc +15 +acc +27 +acc +34 +jmp -111 +acc +37 +acc -6 +nop -2 +jmp -493 +acc +9 +jmp -49 +jmp -442 +acc +0 +acc -6 +jmp -107 +acc +34 +acc +39 +jmp -166 +acc -7 +jmp +21 +acc +29 +jmp -139 +acc +22 +acc -8 +acc +39 +jmp -337 +jmp -191 +acc +0 +acc +34 +jmp -102 +acc -17 +acc -10 +jmp +8 +jmp +7 +acc +32 +acc -19 +acc -6 +jmp -553 +acc +30 +jmp -470 +acc +43 +acc +2 +acc +11 +acc -7 +jmp +8 +acc +39 +acc -9 +acc +16 +jmp -213 +nop -70 +jmp -341 +jmp +5 +acc +3 +jmp -406 +acc +45 +jmp -478 +nop -409 +acc -11 +acc +40 +jmp -482 +acc +0 +acc -13 +nop -341 +acc +30 +jmp -79 +acc +4 +acc +31 +acc +10 +jmp -389 +acc +24 +acc +15 +acc +36 +acc +6 +jmp -103 +jmp -25 +jmp -436 +acc +4 +jmp -317 +jmp -583 +acc +12 +acc +44 +jmp -61 +acc -12 +acc +21 +acc +19 +jmp -628 +acc +33 +acc +37 +acc +23 +nop -182 +jmp +1 \ No newline at end of file diff --git a/data/advent08a.txt b/data/advent08a.txt new file mode 100644 index 0000000..6fee349 --- /dev/null +++ b/data/advent08a.txt @@ -0,0 +1,9 @@ +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 \ No newline at end of file diff --git a/problems/day08.html b/problems/day08.html new file mode 100644 index 0000000..e2974f4 --- /dev/null +++ b/problems/day08.html @@ -0,0 +1,184 @@ + + + + +Day 8 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 16*

  0.0.0.0:2020

+ + + +
+ +

--- Day 8: Handheld Halting ---

Your flight to the major airline hub reaches cruising altitude without incident. While you consider checking the in-flight menu for one of those drinks that come with a little umbrella, you are interrupted by the kid sitting next to you.

+

Their handheld game console won't turn on! They ask if you can take a look.

+

You narrow the problem down to a strange infinite loop in the boot code (your puzzle input) of the device. You should be able to fix it, but first you need to be able to run the code in isolation.

+

The boot code is represented as a text file with one instruction per line of text. Each instruction consists of an operation (acc, jmp, or nop) and an argument (a signed number like +4 or -20).

+
    +
  • acc increases or decreases a single global value called the accumulator by the value given in the argument. For example, acc +7 would increase the accumulator by 7. The accumulator starts at 0. After an acc instruction, the instruction immediately below it is executed next.
  • +
  • jmp jumps to a new instruction relative to itself. The next instruction to execute is found using the argument as an offset from the jmp instruction; for example, jmp +2 would skip the next instruction, jmp +1 would continue to the instruction immediately below it, and jmp -20 would cause the instruction 20 lines above to be executed next.
  • +
  • nop stands for No OPeration - it does nothing. The instruction immediately below it is executed next.
  • +
+

For example, consider the following program:

+
nop +0
+acc +1
+jmp +4
+acc +3
+jmp -3
+acc -99
+acc +1
+jmp -4
+acc +6
+
+

These instructions are visited in this order:

+
nop +0  | 1
+acc +1  | 2, 8(!)
+jmp +4  | 3
+acc +3  | 6
+jmp -3  | 7
+acc -99 |
+acc +1  | 4
+jmp -4  | 5
+acc +6  |
+
+

First, the nop +0 does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) and jmp +4 sets the next instruction to the other acc +1 near the bottom. After it increases the accumulator from 1 to 2, jmp -4 executes, setting the next instruction to the only acc +3. It sets the accumulator to 5, and jmp -3 causes the program to continue back at the first acc +1.

+

This is an infinite loop: with this sequence of jumps, the program will run forever. The moment the program tries to run any instruction a second time, you know it will never terminate.

+

Immediately before the program would run an instruction a second time, the value in the accumulator is 5.

+

Run your copy of the boot code. Immediately before any instruction is executed a second time, what value is in the accumulator?

+
+

Your puzzle answer was 1867.

--- Part Two ---

After some careful analysis, you believe that exactly one instruction is corrupted.

+

Somewhere in the program, either a jmp is supposed to be a nop, or a nop is supposed to be a jmp. (No acc instructions were harmed in the corruption of this boot code.)

+

The program is supposed to terminate by attempting to execute an instruction immediately after the last instruction in the file. By changing exactly one jmp or nop, you can repair the boot code and make it terminate correctly.

+

For example, consider the same program from above:

+
nop +0
+acc +1
+jmp +4
+acc +3
+jmp -3
+acc -99
+acc +1
+jmp -4
+acc +6
+
+

If you change the first instruction from nop +0 to jmp +0, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of the jmp instructions, the program will still eventually find another jmp instruction and loop forever.

+

However, if you change the second-to-last instruction (from jmp -4 to nop -4), the program terminates! The instructions are visited in this order:

+
nop +0  | 1
+acc +1  | 2
+jmp +4  | 3
+acc +3  |
+jmp -3  |
+acc -99 |
+acc +1  | 4
+nop -4  | 5
+acc +6  | 6
+
+

After the last instruction (acc +6), the program terminates by attempting to run the instruction below the last instruction in the file. With this change, after the program terminates, the accumulator contains the value 8 (acc +1, acc +1, acc +6).

+

Fix the program so that it terminates normally by changing exactly one jmp (to nop) or nop (to jmp). What is the value of the accumulator after the program terminates?

+
+

Your puzzle answer was 1303.

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 e7e925d..07d779b 100644 --- a/stack.yaml +++ b/stack.yaml @@ -42,6 +42,7 @@ packages: - advent05 - advent06 - advent07 +- advent08 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as