From: Neil Smith Date: Mon, 11 Dec 2023 14:29:06 +0000 (+0000) Subject: More tinkering X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=47ab90651f43f37766dddc251a942142e2940653;p=advent-of-code-23.git More tinkering --- diff --git a/advent11/Main.hs b/advent11/Main.hs index 2a94423..41b28f1 100644 --- a/advent11/Main.hs +++ b/advent11/Main.hs @@ -2,8 +2,8 @@ import AoC -import Data.List -import Data.Maybe +-- import Data.List +-- import Data.Maybe import Linear (V2(..), (^+^), (^-^)) import qualified Data.Set as S @@ -18,18 +18,22 @@ main = let galaxies = mkGalaxies text -- print galaxies -- print $ expandGalaxies galaxies 10 + + -- print $ allDist $ expandGalaxies 2 galaxies + -- print $ allDist $ expandGalaxies (10^6) galaxies + print $ part1 galaxies print $ part2 galaxies part1, part2 :: Galaxies -> Int part1 = allDistances . expandGalaxies 2 -part2 = allDistances . expandGalaxies (10^6) +part2 = allDistances . expandGalaxies 10e6 allDistances :: Galaxies -> Int -allDistances gs = case S.minView gs of - Nothing -> 0 - Just (g, gs') -> (S.foldl' (addDist g) 0 gs) + allDistances gs' - where addDist g1 acc g2 = acc + distance g1 g2 +allDistances gs = snd $ S.foldl' addGalaxy (S.empty, 0) gs + where addGalaxy (seen, d) new = + (S.insert new seen, S.foldl' (addDist new) d seen) + addDist g1 d g2 = d + distance g1 g2 distance :: Position -> Position -> Int distance g1 g2 = abs dr + abs dc