-
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.
Merge pull request #137 from ephemient/hs/day21
- Loading branch information
Showing
7 changed files
with
89 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
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,52 @@ | ||
-- | | ||
-- Module: Day21 | ||
-- Description: <https://adventofcode.com/2024/day/21 Day 21: Keypad Conundrum> | ||
module Day21 (solve) where | ||
|
||
import Data.Array.Unboxed (UArray, listArray, range, (!)) | ||
import Data.Char (digitToInt, isDigit) | ||
import Data.Function (on) | ||
import Data.List (permutations) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (foldl', lines, unpack) | ||
|
||
luts :: [UArray ((Int, Int), (Int, Int)) Int] | ||
luts = | ||
iterate buildLut . listArray ((lo, lo), (hi, hi)) $ | ||
[abs (x2 - x1) + abs (y2 - y1) + 1 | ((x1, y1), (x2, y2)) <- range ((lo, lo), (hi, hi))] | ||
where | ||
(lo, hi) = ((0, -1), (2, 3)) | ||
buildLut lut = | ||
listArray (((0, -1), (0, -1)), ((2, 3), (2, 3))) $ | ||
[ foldl' min maxBound $ map cost $ filter check $ permutations moves | ||
| ((x1, y1), (x2, y2)) <- range ((lo, lo), (hi, hi)), | ||
let moves = | ||
replicate (x2 - x1) (2, -1) | ||
++ replicate (y2 - y1) (1, 0) | ||
++ replicate (y1 - y2) (1, -1) | ||
++ replicate (x1 - x2) (0, -1) | ||
check = notElem (0, 0) . scanl move (x1, y1) | ||
cost string = sum $ zipWith (curry (lut !)) ((2, 0) : string) (string ++ [(2, 0)]) | ||
] | ||
move (x, y) (1, 0) = (x, y + 1) | ||
move (x, y) (0, -1) = (x - 1, y) | ||
move (x, y) (1, -1) = (x, y - 1) | ||
move (x, y) (2, -1) = (x + 1, y) | ||
|
||
solve :: Int -> Text -> Int | ||
solve depth input = sum [cost (T.unpack line) * T.foldl' accumDigits 0 line | line <- T.lines input] | ||
where | ||
cost string = sum $ zipWith (curry (luts !! depth !) `on` pos) ('A' : string) string | ||
accumDigits n d | isDigit d = 10 * n + digitToInt d | ||
accumDigits n _ = n | ||
pos '0' = (1, 0) | ||
pos '1' = (0, 1) | ||
pos '2' = (1, 1) | ||
pos '3' = (2, 1) | ||
pos '4' = (0, 2) | ||
pos '5' = (1, 2) | ||
pos '6' = (2, 2) | ||
pos '7' = (0, 3) | ||
pos '8' = (1, 3) | ||
pos '9' = (2, 3) | ||
pos _ = (2, 0) |
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,24 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day21Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day21 (solve) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "029A", | ||
"980A", | ||
"179A", | ||
"456A", | ||
"379A" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
solve 2 example `shouldBe` 126384 |