-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{-| | ||
Module: Day11 | ||
Description: <https://adventofcode.com/2023/day/11 Day 11: Cosmic Expansion> | ||
-} | ||
module Day11 (solve) where | ||
|
||
import qualified Data.IntSet as IntSet (dropWhileAntitone, fromDistinctAscList, size, takeWhileAntitone) | ||
import Data.List (tails, transpose) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (unpack) | ||
|
||
solve :: Int -> Text -> Int | ||
solve n input = sum | ||
[ y1 - y0 + abs (x1 - x0) + (n - 1) * (IntSet.size ys + IntSet.size xs) | ||
| (i, (y0, x0):points') <- zip [1..] $ tails points | ||
, (j, (y1, x1)) <- zip [i + 1..] points' | ||
, let ys = IntSet.takeWhileAntitone (< y1) $ IntSet.dropWhileAntitone (< y0) rows | ||
xs = IntSet.takeWhileAntitone (< max x0 x1) $ IntSet.dropWhileAntitone (< min x0 x1) cols | ||
] where | ||
image = lines $ T.unpack input | ||
rows = IntSet.fromDistinctAscList [y | (y, row) <- zip [0..] image, '#' `notElem` row] | ||
cols = IntSet.fromDistinctAscList [x | (x, col) <- zip [0..] $ transpose image, '#' `notElem` col] | ||
points = [(y, x) | (y, row) <- zip [0..] image, (x, '#') <- zip [0..] row] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day11Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day11 (solve) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = T.unlines | ||
[ "...#......" | ||
, ".......#.." | ||
, "#........." | ||
, ".........." | ||
, "......#..." | ||
, ".#........" | ||
, ".........#" | ||
, ".........." | ||
, ".......#.." | ||
, "#...#....." | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
solve 2 example `shouldBe` 374 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
solve 10 example `shouldBe` 1030 | ||
solve 100 example `shouldBe` 8410 |