Skip to content

Commit

Permalink
reuse solve1 for both parts
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 12, 2023
1 parent bcea836 commit a5df434
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions solutions/src/2023/11.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,30 @@ main :: IO ()
main =
do input <- getInputLines 2023 11
let (rows, cols) = unzip [(y, x) | (C y x, '#') <- coordLines input]
solve n = solve1 n rows + solve1 n cols
(r1, rWide) = solve1 rows
(c1, cWide) = solve1 cols
solve n = r1 + c1 + (rWide + cWide) * n
print (solve 2)
print (solve 1_000_000)

-- | Solve the travel problem along a single axis
-- | Solve the travel problem along a single axis.
-- The result is how many normal steps and how many
-- expanded steps were taken.
solve1 ::
Int {- ^ expansion factor -} ->
[Int] {- ^ galaxy positions -} ->
Int {- ^ total distance traveled -}
solve1 ex positions =
[Int] {- ^ galaxy positions -} ->
(Int,Int) {- ^ regular and expanded distances -}
solve1 positions =
case Map.assocs counted of
[] -> 0
(here, n) : xs -> go xs n here 0
[] -> (0, 0)
(here, n) : xs -> go xs n here 0 0
where
counted = counts positions
total = sum counted

go [] _ _ acc = acc
go ((there, m) : xs) n here acc = go xs (n + m) there acc'
go [] _ _ reg gap = ((,) $! reg) $! gap
go ((there, m) : xs) n here reg gap = go xs (n + m) there reg' gap'
where
crossings = n * (total - n)
distance = (there - here - 1) * ex + 1
acc' = acc + crossings * distance
width = there - here - 1
reg' = reg + crossings
gap' = gap + crossings * width

0 comments on commit a5df434

Please sign in to comment.