This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
generated from mazharenko/aoc-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.fs
64 lines (55 loc) · 1.67 KB
/
day14.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
module impl.day14
open System
open Microsoft.FSharp.Core
let parse input =
input
|> Pattern1.read Seq.toArray
|> array2D
let rollLeft (field: char[,]) =
field |> Array2D.toJagged
|> Array.map (fun row ->
String.Join('#',
(new string(row)).Split('#')
// 'O' > '.'
|> Seq.map (Seq.sortDescending >> Seq.toArray >> String.fromChars)
)
) |> array2D
let roll4 field =
field
|> Array2D.rotateCCW
|> rollLeft
|> Array2D.rotateCW
|> rollLeft
|> Array2D.rotateCW
|> rollLeft
|> Array2D.rotateCW
|> rollLeft
|> Array2D.rotateCW
|> Array2D.rotateCW
let load m =
m |> Array2D.transpose
|> Array2D.toJagged
|> Array.map (fun col ->
col |> Seq.indexed |> Seq.where (snd >> ((=)'O'))
|> Seq.map fst
|> Seq.map (fun i -> col.Length - i)
|> Seq.sum
)
|> Array.sum
let solve1 input =
input
|> Array2D.rotateCCW
|> rollLeft
|> Array2D.rotateCW
|> load
type private State = { Map: Map<char[,], int>; Matrix: char[,]; Index: int }
let rec private findCycle state =
match Map.tryFind state.Matrix state.Map with
| None -> findCycle { Matrix = (roll4 state.Matrix); Index = state.Index + 1; Map = Map.add state.Matrix state.Index state.Map }
| Some found -> found, (state.Index - found), state.Map
let solve2 input =
let cycleStart, cycleSize, map =
findCycle { Map = Map.empty; Matrix = input; Index = 0 }
let index = ((1000000000 - cycleStart) % cycleSize) + cycleStart
map |> Map.findKey (fun _ i -> i = index)
|> load