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
|
#r "nuget: astar-search, 1.0.2"
open AStar
let isWall (x, y) =
let bits = System.Convert.ToString(x*x + 3*x + 2*x*y + y + y*y + 1350, 2)
bits |> Seq.filter ((=) '1') |> Seq.length |> fun x -> x % 2 = 1
let neighbours (x, y) =
let found = seq [ (x+1, y); (x, y+1); (x-1, y); (x, y-1) ]
found |> Seq.filter (fun (nx, ny) ->
nx >= 0 && ny >= 0 &&
not <| isWall(nx, ny))
let gScore _ _ = 1.
let fScore (x, y) (gx, gy) =
sqrt ((float gx - float x)**2. + (float gy - float y)**2.)
// part 1
match search (1, 1) (31, 39) {neighbours=neighbours; gCost=gScore; fCost=fScore; maxIterations=None} with
| Some path ->
printfn "%d" <| Seq.length path - 1
| None -> printfn "0"
// part 2
let mutable count = 0
for i = 0 to 51 do
for j= 0 to 51 do
if i+j <= 52 && not (isWall(i, j)) && not (Set.contains (i,j) (Set.ofList [ (0, 51); (0, 52); (51, 0); (52, 0) ]))
then
match search (1, 1) (i, j) {neighbours=neighbours; gCost=gScore; fCost=fScore; maxIterations=None} with
| Some path ->
if Seq.length path - 1 <= 50 then
count <- count + 1
| None -> ()
printfn "%d" count
|