-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.hs
92 lines (75 loc) · 2.37 KB
/
Day8.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module Day8
( part1
, part2
) where
import Data.Maybe (Maybe (Just, Nothing), fromJust, isNothing)
import Data.Set (Set, empty, insert, member)
import Data.Vector (Vector, fromList, (!), (!?))
import Helpers.Parsers
data BootCode =
BootCode
{ instructions :: Instructions
, vals :: Vals
, position :: Position
, accumulator :: Int
, seen :: Set Position
}
deriving (Show)
type Instructions = Vector String
type Vals = Vector Int
type Position = Int
makeBootCode :: [(String, Int)] -> BootCode
makeBootCode inst =
BootCode (fromList . map fst $ inst) (fromList . map snd $ inst) 0 0 empty
stripPlus :: String -> Int
stripPlus n@(a:as)
| a == '+' = read as
| otherwise = read n
advance :: Int -> BootCode -> BootCode
advance d bc =
bc {position = position bc + d, seen = insert (position bc) $ seen bc}
accumulate :: BootCode -> BootCode
accumulate bc =
advance 1 $ bc {accumulator = accumulator bc + vals bc ! position bc}
jump :: BootCode -> BootCode
jump bc = advance (vals bc ! position bc) bc
nop :: BootCode -> BootCode
nop = advance 1
curInst :: BootCode -> String
curInst bc = instructions bc ! position bc
loop :: BootCode -> Int
loop bc
| position bc `elem` seen bc = accumulator bc
| ci == "acc" = loop . accumulate $ bc
| ci == "jmp" = loop . jump $ bc
| ci == "nop" = loop . nop $ bc
where
ci = curInst bc
terminate :: BootCode -> Int
terminate bc
| ci == "acc" = terminate . accumulate $ bc
| ci == "jmp" && isNothing changedJump = terminate . jump $ bc
| ci == "jmp" = fromJust changedJump
| ci == "nop" && isNothing changedNop = terminate . nop $ bc
| ci == "nop" = fromJust changedNop
where
ci = curInst bc
changedJump = changed . nop $ bc
changedNop = changed . jump $ bc
changed :: BootCode -> Maybe Int
changed bc
| position bc `elem` seen bc = Nothing
| isNothing (instructions bc !? position bc) = Just $ accumulator bc
| ci == "acc" = changed . accumulate $ bc
| ci == "jmp" = changed . jump $ bc
| ci == "nop" = changed . nop $ bc
where
ci = curInst bc
part1 :: Bool -> String -> String
part1 _ =
show .
loop . makeBootCode . map ((\[a, b] -> (a, stripPlus b)) . words) . lines
part2 :: Bool -> String -> String
part2 _ =
show .
terminate . makeBootCode . map ((\[a, b] -> (a, stripPlus b)) . words) . lines