-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path8a.hs
32 lines (28 loc) · 811 Bytes
/
8a.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
import AOC
import qualified Data.Vector as V
import qualified Data.Set as S
main = interact $ f . ltov . parselist p
data Instruction = Acc | Jmp | Nop deriving (Show, Eq, Read, Ord, Bounded, Enum)
p :: Parser (Instruction, Int)
p = do
instr <- enump
space
sgn <- choice [char '+' >> return 1, char '-' >> return (-1), return 1]
arg <- read <$> many1 digit
return (instr, sgn * arg)
f :: Vector (Instruction, Int) -> Int
f prg = exec 0 0 S.empty
where
exec a ip s =
if ip `S.member` s
then
a
else
let
s' = S.insert ip s
ip' = succ ip
in
case prg V.!? ip of
Just (Acc, i) -> exec (a + i) ip' s'
Just (Jmp, i) -> exec a (ip + i) s'
Just (Nop, _) -> exec a ip' s'