-
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
84 additions
and
0 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,38 @@ | ||
-- | | ||
-- Module: Day8 | ||
-- Description: <https://adventofcode.com/2024/day/8 Day 8: Resonant Collinearity> | ||
module Day8 (part1, part2) where | ||
|
||
import Control.Monad (guard) | ||
import Data.Containers.ListUtils (nubOrd) | ||
import Data.Ix (Ix (inRange)) | ||
import Data.Map qualified as Map (elems, fromListWith) | ||
import Data.Set qualified as Set (singleton, toList) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (length, lines, unpack) | ||
|
||
solve :: ((Int, Int) -> (Int, Int) -> [(Int, Int)]) -> Text -> Int | ||
solve extend input = | ||
length . nubOrd $ do | ||
values <- Set.toList <$> Map.elems points | ||
p0 <- values | ||
p1 <- values | ||
guard $ p0 /= p1 | ||
takeWhile (inRange ((0, 0), (height - 1, width - 1))) $ extend p0 p1 | ||
where | ||
input' = T.lines input | ||
height = length input' | ||
width = maximum $ T.length <$> input' | ||
points = | ||
Map.fromListWith (<>) $ | ||
[ (c, Set.singleton (y, x)) | ||
| (y, line) <- zip [0 ..] $ T.lines input, | ||
(x, c) <- zip [0 ..] $ T.unpack line, | ||
c /= '.' | ||
] | ||
|
||
part1 :: Text -> Int | ||
part1 = solve $ \(y0, x0) (y1, x1) -> [(2 * y1 - y0, 2 * x1 - x0)] | ||
|
||
part2 :: Text -> Int | ||
part2 = solve $ \(y0, x0) (y1, x1) -> zip [y1, 2 * y1 - y0 ..] [x1, 2 * x1 - x0 ..] |
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,34 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day8Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day8 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "............", | ||
"........0...", | ||
".....0......", | ||
".......0....", | ||
"....0.......", | ||
"......A.....", | ||
"............", | ||
"............", | ||
"........A...", | ||
".........A..", | ||
"............", | ||
"............" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 14 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` 34 |