Skip to content

Commit

Permalink
Day 1: Minor simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 1, 2023
1 parent 6d0213a commit 9b01df7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions hs/src/Day1.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{-|
Module: Day1
Description: <https://adventofcode.com/2023/day/1 Day 1: Trebuchet?! >
Description: <https://adventofcode.com/2023/day/1 Day 1: Trebuchet?!>
-}
{-# LANGUAGE OverloadedStrings #-}
module Day1 (part1, part2) where

import Data.Maybe (fromMaybe, listToMaybe)
import Data.Maybe (listToMaybe, mapMaybe)
import Data.Text (Text)
import qualified Data.Text as T (inits, isPrefixOf, isSuffixOf, lines, pack, tails)

Expand All @@ -14,10 +14,10 @@ part1 = solve digits
part2 = solve extendedDigits

solve :: [(Int, Text)] -> Text -> Int
solve values = sum . map solve' . T.lines where
solve' line = fromMaybe 0 . listToMaybe $ do
x <- [i | s <- T.tails line, (i, t) <- values, t `T.isPrefixOf` s]
y <- [j | s <- reverse $ T.inits line, (j, t) <- values, t `T.isSuffixOf` s]
solve values = sum . mapMaybe solve' . T.lines where
solve' line = do
x <- listToMaybe [i | s <- T.tails line, (i, t) <- values, t `T.isPrefixOf` s]
y <- listToMaybe [j | s <- reverse $ T.inits line, (j, t) <- values, t `T.isSuffixOf` s]
pure $ 10 * x + y

digits, extendedDigits :: [(Int, Text)]
Expand Down

0 comments on commit 9b01df7

Please sign in to comment.