-
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
83 additions
and
3 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,39 @@ | ||
{-| | ||
Module: Day3 | ||
Description: <https://adventofcode.com/2023/day/3 Day 3: Gear Ratios> | ||
-} | ||
module Day3 (part1, part2) where | ||
|
||
import Control.Arrow (second) | ||
import Data.Char (isDigit, isSpace) | ||
import qualified Data.Map as Map (elems, fromListWith) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (break, index, length, lines, span) | ||
import qualified Data.Text.Read as T (decimal) | ||
import qualified Data.Vector as V ((!), fromList, length) | ||
|
||
parts :: Text -> [((Int, Int), Int)] | ||
parts input = | ||
[ ((x, y'), number) | ||
| y <- [0..V.length lines - 1] | ||
, ((x0, x1), number) <- numbers 0 $ lines V.! y | ||
, y' <- [max 0 $ y - 1..min (V.length lines - 1) $ y + 1] | ||
, let line = lines V.! y' | ||
, x <- [max 0 $ x0 - 1..min (T.length line - 1) x1] | ||
, let c = T.index line x | ||
, not $ c == '.' || isSpace c || isDigit c | ||
] where | ||
lines = V.fromList $ T.lines input | ||
numbers acc t | ||
| (leading, t') <- T.break isDigit t | ||
, Right (number, trailing) <- T.decimal t' | ||
= let acc' = acc + T.length t - T.length trailing | ||
in ((acc + T.length leading, acc'), number) : numbers acc' trailing | ||
| otherwise = [] | ||
|
||
part1 :: Text -> Int | ||
part1 = sum . map snd . parts | ||
|
||
part2 :: Text -> Int | ||
part2 = sum . map product . filter ((== 2) . length) . | ||
Map.elems . Map.fromListWith (++) . map (second (:[])) . parts |
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,30 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day3Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day3 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = T.unlines | ||
[ "467..114.." | ||
, "...*......" | ||
, "..35..633." | ||
, "......#..." | ||
, "617*......" | ||
, ".....+.58." | ||
, "..592....." | ||
, "......755." | ||
, "...$.*...." | ||
, ".664.598.." | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 4361 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` 467835 |