Commit 90918214 authored by Neil Smith's avatar Neil Smith
Browse files

Done day 8

parent b3f38625
# 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: <https://github.com/sol/hpack>.
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
-- 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
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