Skip to content

Commit 970190d

Browse files
committed
Refactor
1 parent 1125743 commit 970190d

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

2023/11.fs

+28-17
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,40 @@ let getGalaxyIndexes input =
2121
row |> Seq.indexed |> Seq.filter (snd >> (=) Galaxy) |> Seq.map (fun (x, _) -> (x, y)))
2222
|> List.ofSeq
2323

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 1L)
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
2731

28-
let dx =
29-
[ (min x1 x2) .. (max x1 x2) ] |> List.sumBy (fun x -> if expCol x then expandIndex else 1L)
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
3036

31-
dx + dy - 2L
37+
rows, columns
3238

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)
3542

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 - 1L) * (List.count (fun i -> minx < i && i < maxx) expCol |> int64)
45+
46+
let dy =
47+
int64 (maxy - miny) + (expandIndex - 1L) * (List.count (fun i -> miny < i && i < maxy) expRow |> int64)
48+
49+
dx + dy
3850

3951
let solve dist input =
4052
let galaxies = getGalaxyIndexes input
41-
let shouldExpRow = shouldExpRow input
42-
let shouldExpCol = shouldExpCol input
53+
let expRow, expCol = getExpansions input
4354

4455
List.allPairs galaxies galaxies
4556
|> 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)
4758

4859
let solution = makeSolution () parser (solve 2L) (solve 1000000L)
4960

@@ -73,10 +84,10 @@ module Tests =
7384
[<InlineData(0, 9, 4, 9, 5)>]
7485
let ``Example part 1 - concrete pair`` x1 y1 x2 y2 dist =
7586
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)
8091

8192
[<Fact>]
8293
let ``Example part 2`` () =

0 commit comments

Comments
 (0)