-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8.fs
90 lines (72 loc) · 2.05 KB
/
Day8.fs
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
module AoC2020.Day8
open System.Text.RegularExpressions
open AoC2020.Utils
type ExecutionState =
| Running
| Halted
| Terminated
type State =
{ Pos: int
Acc: int
Visited: int Set
State: ExecutionState }
let jmp n state () =
let newPos = state.Pos + n
match Set.contains newPos state.Visited with
| true -> { state with State = Halted }
| false ->
{ state with
Pos = newPos
Visited = state.Visited.Add newPos }
let acc n state = jmp 1 { state with Acc = state.Acc + n }
let nop state = jmp 1 state
let getCmd s =
let re =
Regex("^(?<cmd>acc|jmp|nop) (?<arg>[+-]\d+)")
let m = re.Match(s)
let cmd = m.Groups.["cmd"].Value
let arg = int m.Groups.["arg"].Value
cmd, arg
let parseCommand s =
match getCmd s with
| ("acc", arg) -> acc arg
| ("jmp", arg) -> jmp arg
| ("nop", _) -> nop
let initialState =
{ Pos = 0
Acc = 0
Visited = Set.singleton 0
State = Running }
let execute (program: string array) =
let mutable state = initialState
while state.State = Running do
try
let op = parseCommand program.[state.Pos]
state <- op state ()
with IndexOutOfRangeException -> state <- { state with State = Terminated }
state
let replaceJump (program: string array) pos =
let p = Array.copy program
p.[pos] <- "nop +0"
p
let findJumpCommands program =
program
|> Seq.mapi (fun i e -> i, e)
|> Seq.filter (fun (_, s) -> fst (getCmd s) = "jmp")
|> Seq.map fst
let mutate originalProgram =
let program = originalProgram |> Array.ofSeq
findJumpCommands originalProgram
|> Seq.map (fun pos -> replaceJump program pos)
let day8 fn () =
let program = readInput fn |> Array.ofSeq
let state = execute program
state.Acc |> int64
let day8part2 fn () =
let programs = readInput fn |> mutate
let state =
programs
|> Seq.map execute
|> Seq.filter (fun s -> s.State = Terminated)
|> Seq.head
state.Acc |> int64