@@ -21,29 +21,40 @@ let getGalaxyIndexes input =
21
21
row |> Seq.indexed |> Seq.filter ( snd >> (=) Galaxy) |> Seq.map ( fun ( x , _ ) -> ( x, y)))
22
22
|> List.ofSeq
23
23
24
- let measureDistance expRow expCol expandIndex input ( x1 , y1 ) ( x2 , y2 ) =
25
- let dy =
26
- [ ( min y1 y2) .. ( max y1 y2) ] |> List.sumBy ( fun y -> if expRow y then expandIndex else 1 L)
24
+ let getExpansions input =
25
+ let rows =
26
+ input
27
+ |> Seq.indexed
28
+ |> Seq.filter ( fun ( y , row ) -> not <| Array.contains Galaxy row)
29
+ |> Seq.map fst
30
+ |> List.ofSeq
27
31
28
- let dx =
29
- [ ( min x1 x2) .. ( max x1 x2) ] |> List.sumBy ( fun x -> if expCol x then expandIndex else 1 L)
32
+ let columns =
33
+ [ 0 .. (( Array.item 0 input |> Array.length) - 1 ) ]
34
+ |> List.filter ( fun col -> not <| Array.exists ( Array.item col >> (=) Galaxy) input)
35
+ |> List.ofSeq
30
36
31
- dx + dy - 2 L
37
+ rows , columns
32
38
33
- let shouldExpRow input =
34
- Utils.memoize ( fun row -> input |> Array.item row |> Array.contains Galaxy |> not )
39
+ let measureDistance expRow expCol expandIndex ( x1 , y1 ) ( x2 , y2 ) =
40
+ let ( minx , miny ) = ( min x1 x2, min y1 y2)
41
+ let ( maxx , maxy ) = ( max x1 x2, max y1 y2)
35
42
36
- let shouldExpCol input =
37
- Utils.memoize ( fun col -> input |> Array.exists ( Array.item col >> (=) Galaxy) |> not )
43
+ let dx =
44
+ int64 ( maxx - minx) + ( expandIndex - 1 L) * ( List.count ( fun i -> minx < i && i < maxx) expCol |> int64)
45
+
46
+ let dy =
47
+ int64 ( maxy - miny) + ( expandIndex - 1 L) * ( List.count ( fun i -> miny < i && i < maxy) expRow |> int64)
48
+
49
+ dx + dy
38
50
39
51
let solve dist input =
40
52
let galaxies = getGalaxyIndexes input
41
- let shouldExpRow = shouldExpRow input
42
- let shouldExpCol = shouldExpCol input
53
+ let expRow , expCol = getExpansions input
43
54
44
55
List.allPairs galaxies galaxies
45
56
|> List.filter ( uncurry (<))
46
- |> List.sumBy ( fun ( l , r ) -> measureDistance shouldExpRow shouldExpCol dist input l r)
57
+ |> List.sumBy ( fun ( l , r ) -> measureDistance expRow expCol dist l r)
47
58
48
59
let solution = makeSolution () parser ( solve 2 L) ( solve 1000000 L)
49
60
@@ -73,10 +84,10 @@ module Tests =
73
84
[<InlineData( 0 , 9 , 4 , 9 , 5 ) >]
74
85
let ``Example part 1 - concrete pair`` x1 y1 x2 y2 dist =
75
86
let input = parseTestInput parser input
76
- let shouldExpRow = shouldExpRow input
77
- let shouldExpCol = shouldExpCol input
78
- measureDistance shouldExpRow shouldExpCol 2 input ( x1, y1) ( x2, y2) |> should equal ( int64 dist)
79
- measureDistance shouldExpRow shouldExpCol 2 input ( x2, y2) ( x1, y1) |> should equal ( int64 dist)
87
+ let expRow , expCol = getExpansions input
88
+
89
+ measureDistance expRow expCol 2 ( x1, y1) ( x2, y2) |> should equal ( int64 dist)
90
+ measureDistance expRow expCol 2 ( x2, y2) ( x1, y1) |> should equal ( int64 dist)
80
91
81
92
[<Fact>]
82
93
let ``Example part 2`` () =
0 commit comments