{-# Language NumericUnderscores, ImportQualifiedPost #-}
module Main (main) where
import Advent (getInputLines, counts)
import Advent.Coord (Coord(C), coordLines)
import Data.List (tails)
import Data.Map qualified as Map
main :: IO ()
IO ()
main =
do input <- Int -> Int -> IO [String]
getInputLines Int
2023 Int
11
let (rows, cols) = unzip [(y, x) | (C y x, '#') <- coordLines input]
(r1, rWide) = solve1 rows
(c1, cWide) = solve1 cols
solve Int
n = Int
r1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
rWide Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
cWide) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n
print (solve 2)
print (solve 1_000_000)
solve1 ::
[Int] ->
(Int,Int)
solve1 :: [Int] -> (Int, Int)
solve1 [Int]
galaxies = ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
crossings, [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Int -> Int -> Int) -> [Int] -> [Int] -> [Int]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(*) [Int]
crossings [Int]
gaps))
where
counted :: Map Int Int
counted = [Int] -> Map Int Int
forall (f :: * -> *) a. (Foldable f, Ord a) => f a -> Map a Int
counts [Int]
galaxies
total :: Int
total = Map Int Int -> Int
forall a. Num a => Map Int a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Map Int Int
counted
gaps :: [Int]
gaps = [Int
x2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
x1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 | Int
x1 : Int
x2 : [Int]
_ <- [Int] -> [[Int]]
forall a. [a] -> [[a]]
tails (Map Int Int -> [Int]
forall k a. Map k a -> [k]
Map.keys Map Int Int
counted)]
crossings :: [Int]
crossings = [Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
total Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) | Int
n <- (Int -> Int -> Int) -> [Int] -> [Int]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (Map Int Int -> [Int]
forall k a. Map k a -> [a]
Map.elems Map Int Int
counted)]