-
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
71 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,40 @@ | ||
{-| | ||
Module: Day15 | ||
Description: <https://adventofcode.com/2023/day/15 Day 15: Lens Library> | ||
-} | ||
{-# LANGUAGE OverloadedStrings, TransformListComp #-} | ||
module Day15 (part1, part2) where | ||
|
||
import Data.Bits ((.&.)) | ||
import Data.Char (isAlphaNum, isSpace, ord) | ||
import Data.Function (on) | ||
import Data.List (foldl', groupBy, sortOn) | ||
import qualified Data.Map as Map (delete, empty, insertWith, toList) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (foldl', null, span, split, stripPrefix, stripSuffix) | ||
import qualified Data.Text.Read as T (decimal) | ||
import GHC.Exts (groupWith, the) | ||
|
||
hash :: Text -> Int | ||
hash = (.&. 255) . T.foldl' hash' 0 where hash' k c = 17 * (k + ord c) | ||
|
||
isSep :: Char -> Bool | ||
isSep ',' = True | ||
isSep c = isSpace c | ||
|
||
part1, part2 :: Text -> Int | ||
part1 = sum . map hash . T.split isSep | ||
part2 = power . foldl' go Map.empty . zip [0..] . filter (not . T.null) . T.split isSep | ||
where | ||
go lens (c, ins) | ||
| "-" <- rest = Map.delete key lens | ||
| Just (Right (n, "")) <- T.decimal <$> T.stripPrefix "=" rest | ||
= Map.insertWith (\(n, _) (_, c) -> (n, c)) key (n, c) lens | ||
where (key, rest) = T.span isAlphaNum ins | ||
power lens = sum | ||
[ (the (hash <$> key) + 1) * sum (zipWith (*) [1..] n) | ||
| (key, (n, c)) <- Map.toList lens | ||
, then sortOn by (hash key, c) | ||
, then group by hash key using groupBy . on (==) | ||
] |
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,19 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day15Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day15 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7\n" | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 1320 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` 145 |