diff options
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 |