diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
commit | 0a4fe70d367f0bf1a78602af600052f58a377c34 (patch) | |
tree | 69c2bcd2b54b48968fd52728aaf0e21ca154784c /day25.fsx | |
parent | 1e2642d8793e6a4fb6cba16cd651d5fdca3e4581 (diff) | |
download | AdventOfCode2017-0a4fe70d367f0bf1a78602af600052f58a377c34.tar.gz |
solutions to day 25 main
Diffstat (limited to 'day25.fsx')
-rw-r--r-- | day25.fsx | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/day25.fsx b/day25.fsx new file mode 100644 index 0000000..bd44736 --- /dev/null +++ b/day25.fsx @@ -0,0 +1,64 @@ +open System.Collections.Generic + +let stepCount = 12172063 +let tape = Dictionary<int, int>() +type State = A | B | C | D | E | F + +let getValue (tape: Dictionary<int, int>) index = + if not (tape.ContainsKey index) then tape[index] <- 0 + tape[index] + +let rec turingStep (tape: Dictionary<int, int>) index state stepCount= + if stepCount = 0 then tape.Values |> Seq.groupBy id |> dict |> (fun x -> Seq.length x[1]) + else + match state with + | A -> + if (getValue tape index) = 0 + then + tape[index] <- 1 + turingStep tape (index + 1) B (stepCount - 1) + else + tape[index] <- 0 + turingStep tape (index - 1) C (stepCount - 1) + | B -> + if (getValue tape index) = 0 + then + tape[index] <- 1 + turingStep tape (index - 1) A (stepCount - 1) + else + tape[index] <- 1 + turingStep tape (index - 1) D (stepCount - 1) + | C -> + if (getValue tape index) = 0 + then + tape[index] <- 1 + turingStep tape (index + 1) D (stepCount - 1) + else + tape[index] <- 0 + turingStep tape (index + 1) C (stepCount - 1) + | D -> + if (getValue tape index) = 0 + then + tape[index] <- 0 + turingStep tape (index - 1) B (stepCount - 1) + else + tape[index] <- 0 + turingStep tape (index + 1) E (stepCount - 1) + | E -> + if (getValue tape index) = 0 + then + tape[index] <- 1 + turingStep tape (index + 1) C (stepCount - 1) + else + tape[index] <- 1 + turingStep tape (index - 1) F (stepCount - 1) + | F -> + if (getValue tape index) = 0 + then + tape[index] <- 1 + turingStep tape (index - 1) E (stepCount - 1) + else + tape[index] <- 1 + turingStep tape (index + 1) A (stepCount - 1) + +turingStep tape 0 A stepCount |> printfn "%d" \ No newline at end of file |