-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
35 lines (26 loc) · 943 Bytes
/
Program.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
module Day06
open System.IO
let redistribute (memory: byref<int[]>) =
let mutable (position, value) = memory |> Array.indexed |> Array.maxBy (fun (_, v) -> v)
memory.[position] <- 0
while value > 0 do
position <- (position + 1) % Array.length memory
memory.[position] <- memory.[position] + 1
value <- value - 1
let findLoop input =
let mutable memory = List.toArray input
let mutable steps = 0
let mutable visited = []
while List.contains memory visited |> not do
visited <- visited @ [Array.copy memory]
steps <- steps + 1
redistribute &memory
(steps, Array.toList memory)
[<EntryPoint>]
let main argv =
let input = File.ReadAllText("input.txt").Split("\t") |> Seq.map int |> Seq.toList
let (steps1, sequence) = findLoop input
printfn "Part one: %d" steps1
let (steps2, _) = findLoop sequence
printfn "Part two: %d" steps2
0