-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.hs
181 lines (148 loc) · 5.2 KB
/
8.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Lib
import Data.Char
import Control.DeepSeq
import Data.Maybe
import GHC.Generics
import Data.Function
import Control.Monad.Fix
import Debug.Trace
dummyInput = [ "nop +0"
, "acc +1"
, "jmp +4"
, "acc +3"
, "jmp -3"
, "acc -99"
, "acc +1"
, "jmp -4"
, "acc +6" ]
dummyBand = fromJust $ toBand dummyInput
where toBand = sequence . map ((fmap (flip (,) False)) . parseInstruction)
main = do
let toBand = sequence . map ((fmap (flip (,) False)) . parseInstruction)
dummyBand = fromJust $ toBand dummyInput
--print $ toBand dummyInput
--print $ runState (fixState eval dummyBand) $ EvalState 0 0
contents <- readFile "8.input"
let band = fromJust $ toBand $ lines contents
--print $ runState (fixState eval band) $ EvalState 0 0
putStrLn "fixing bug:"
let res = untilLastExecuted band (-1)
print res
print $ (!!627) . fst $ res
untilLastExecuted :: Band (ArithFunc Int) -> Int -> (Band (ArithFunc Int), EvalState)
untilLastExecuted band i =
let result = runState (fixState eval (changeIth i band)) $ EvalState 0 0
in if (snd . last . fst $ result)
then result
else untilLastExecuted band (i+1)
changeIth :: Int -> Band (ArithFunc Int) -> Band (ArithFunc Int)
changeIth _ [] = []
changeIth 0 ((Jmp x,b):t) = (Nop x,b) : t
changeIth 0 ((Nop x,b):t) = (Jmp x,b) : t
changeIth i (instr@(Jmp _,_):t) = instr : changeIth (i-1) t
changeIth i (instr@(Nop _,_):t) = instr : changeIth (i-1) t
changeIth i (h:t) = h : changeIth i t
parseArithFunc :: forall a. Read a => String -> Maybe (ArithFunc a)
parseArithFunc (a:s)
| a == '+' = Just $ Plus rest
| a == '-' = Just $ Minus rest
| a == '*' = Just $ Times rest
| otherwise = Nothing
where
rest = read @a s
parseInstruction :: String -> Maybe (Instruction (ArithFunc Int))
parseInstruction s = case inst of
"jmp" -> Jmp <$> f
"acc" -> Acc <$> f
"nop" -> Nop <$> f
_ -> Nothing
where inst = take 3 s
f = parseArithFunc $ drop 4 s
data Instruction a = Nop a | Acc a | Jmp a
deriving (Show, Generic, Read)
instance NFData a => NFData (Instruction a)
type Band a = [(Instruction a, Bool)]
data EvalState = EvalState { instrPointer :: Int
, accumulator :: Int }
deriving (Show, Generic, Eq)
incr :: EvalState -> EvalState
incr (EvalState i a) = EvalState (i+1) a
modAcc :: (Int -> Int) -> EvalState -> EvalState
modAcc f (EvalState i a) = EvalState i $ f a
modInstr :: (Int -> Int) -> EvalState -> EvalState
modInstr f (EvalState i a) = EvalState (f i) a
newtype State s a = State { runState :: s -> (a, s) }
contramap :: (a -> b) -> (a, c) -> (b, c)
contramap f (a, c) = (f a, c)
instance Functor (State s) where
f `fmap` (State s) = State $ contramap f . s
instance Applicative (State s) where
pure a = State $ \s -> (a, s)
(State act) <*> s2 = State $ \s ->
let (f, s') = act s
in runState (f <$> s2) s'
instance Monad (State s) where
(State act) >>= f = State $ \s ->
let (a, s') = act s
in runState (f a) s'
{-
instance MonadFix (State s) where
mfix f = mfix $ \st -> f st
-}
get :: State s s
get = State $ \s -> (s,s)
put :: s -> State s ()
put x = State $ \sth -> ((), x)
modify :: (s -> s) -> State s ()
modify f = do
x <- get
put (f x)
(!!!) :: NFData a => [a] -> Int -> Maybe a
(!!!) l a = deepTry (l!!) a
data ArithFunc a = Plus a | Minus a | Times a
deriving (Show, Generic)
instance NFData a => NFData (ArithFunc a)
toFunc :: Num a => ArithFunc a -> (a -> a)
toFunc (Plus a) = ((+) a)
toFunc (Minus a) = (flip (-) a)
toFunc (Times a) = (*a)
firstBy :: Eq a => (a -> a -> Bool) -> [a] -> a
firstBy f (a:as) = fa as a
where
fa (a:as) b
| f a b = b
| otherwise = fa as a
modList :: Int -> (a -> a) -> [a] -> [a]
modList i f l
| length l > i = take (i) l ++ [f $ l!!i] ++ (drop (i+1) l )
| otherwise = l
fixState :: (Eq a, Show a) => (b -> State a b) -> b -> State a b
fixState action lastOutput = do
last <- get
newOutput <- action lastOutput
new <- get
if last == new --compare states
then return lastOutput
else fixState action newOutput
{-
let now = f last
valNow = fst $ runState (now >> get) $ last
in if valNow == last
then now
else fixState f valNow
-}
eval :: Band (ArithFunc Int) -> State EvalState (Band (ArithFunc Int))
eval instructions = do
s@(EvalState p a) <- get
let newEval =
(case instructions !!! p of
Nothing -> s
(Just (_, True)) -> s
(Just (Nop f, _)) -> EvalState (p+1) a
(Just (Acc f, _)) -> EvalState (p+1) (toFunc f $ a)
(Just (Jmp f, _)) -> EvalState (toFunc f $ p) a )
put $ newEval
return $ modList p (\(a, _) -> (a, True)) instructions