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 pathday21.fs
65 lines (58 loc) · 2.37 KB
/
day21.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
module impl.day21
#nowarn "25"
open Bfs.common
open Bfs.Custom
let parse input =
Pattern1.read Seq.toArray input
|> array2D
type private State = { I: int; J:int; M: char[,]; Steps: int }
let solve1 input =
let settings = { VisitedKey = fun s -> s.I,s.J,s.Steps }
let adjacency { I = i; J = j; M = m; Steps = steps } =
if (steps >= 64) then []
else
Array2D.Adj.indexesValues4 m (i,j)
|> List.where (fun (_, c) -> c = '.' || c = 'S')
|> List.map (fun ((i, j), _) -> { I = i; J = j; M = m; Steps = steps+1 })
let initialState = { I = 65; J = 65; M = input; Steps = 0 }
let graphFolder reachedIn65Steps (reached: Path<State>) =
if reached.Length = 65 then reachedIn65Steps + 1, Continue
else reachedIn65Steps, Continue
fold settings { Adjacency = Adjacency.sameWeight adjacency } initialState graphFolder 0
let solve2 input =
let settings = { VisitedKey = fun s -> s.I, s.J, s.Steps%2 }
let adjacency { I = currentI; J = currentJ; M = m; Steps = steps } =
if steps >= 800 then []
else
Array2D.Adj.d4
|> Seq.map (fun (di, dj) -> (currentI + di, currentJ + dj))
|> Seq.where (fun (i,j) ->
let atij = Array2D.get m (i %% Array2D.length1 m) (j %% Array2D.length2 m)
atij = '.' || atij = 'S'
)
|> Seq.map (fun (i, j) -> { I = i; J = j; M = m; Steps = steps+1 })
|> Seq.toList
let initialState = { I = 65; J = 65; M = input; Steps = 0 }
let graphFolder steps (reached: Path<State>) =
reached.Head.Len :: steps, Continue
let pathSteps =
fold settings { Adjacency = Adjacency.sameWeight adjacency } initialState graphFolder []
|> List.toArray
seq { 65 .. 131 .. 1000 }
|> Seq.map (fun x ->
pathSteps |> Seq.where (fun p -> p <= x && x % 2 = p % 2) |> Seq.length
)
|> Seq.map int64
|> Seq.indexed
|> Seq.windowed 4
// try to recognize the beginning of a quadratic sequence
|> Seq.choose (fun ([|_,x1; _,x2; _,x3; i,x4|]) ->
if (x4 - x3 - (x3 - x2) = x3 - x2 - (x2 - x1))
then
let n = 26501365L / 131L - int64 i
let d = x4 - x3
let d2 = x4 - x3 - (x3 - x2)
Some (x4 + (d + d2) * n + d2 * n * (n - 1L) / 2L)
else
None
) |> Seq.head