5 read :: *File -> [Instr]
8 | l.[size l - 1] <> '\n' = []
9 = [toInstr [c\\c<-:l | c <> '\n']:read f]
11 toInstr ['nop ':rs] = Nop (toInt (toString rs))
12 toInstr ['acc ':rs] = Acc (toInt (toString rs))
13 toInstr ['jmp ':rs] = Jmp (toInt (toString rs))
15 :: Instr = Nop Int | Acc Int | Jmp Int
20 # instr = {c\\c<-instr}
21 = (one instr, two instr)
24 two = hd o filter snd o map interpret o nopjump 0
26 nopjump :: Int {Instr} -> [{Instr}]
30 Nop i = [update {i\\i<-:is} pc (Jmp i):nopjump (pc+1) is]
31 Jmp i = [update {i\\i<-:is} pc (Nop i):nopjump (pc+1) is]
34 interpret :: {Instr} -> (Int, Bool)
35 interpret instr = int [] 0 0
38 | pc >= size instr = (acc, True)
39 | isMember pc vis = (acc, False)
41 Nop _ = int [pc:vis] (1 + pc) acc
42 Acc i = int [pc:vis] (1 + pc) (i + acc)
43 Jmp i = int [pc:vis] (i + pc) acc