-
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
88 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,41 @@ | ||
{-| | ||
Module: Day18 | ||
Description: <https://adventofcode.com/2023/day/18 Day 18: Lavaduct Lagoon> | ||
-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day18 (part1, part2) where | ||
|
||
import Control.Monad (replicateM) | ||
import Data.Functor (($>)) | ||
import Data.List (foldl') | ||
import Data.Text (Text) | ||
import Data.Void (Void) | ||
import Numeric (readHex) | ||
import Text.Megaparsec (MonadParsec, ParseErrorBundle, Parsec, Stream(Token), choice, eof, oneOf, parse, sepEndBy, skipManyTill) | ||
import Text.Megaparsec.Char (char, digitChar, hexDigitChar, hspace1, newline, string) | ||
import Text.Megaparsec.Char.Lexer (decimal) | ||
|
||
data Direction = U | L | D | R deriving (Show) | ||
|
||
solve :: (Token s ~ Char, Stream s, Ord e, Integral a) => Parsec e s (Direction, a) -> s -> Either (ParseErrorBundle s e) a | ||
solve parser input = do | ||
moves <- parse (parser `sepEndBy` newline <* eof) "" input | ||
let (_, a, l) = foldl' f ((0, 0), 0, 2) moves | ||
pure $ abs a + l `div` 2 | ||
where | ||
f ((y, x), a, l) (d, n) | ||
| U <- d = ((y - n, x), a, l + n) | ||
| L <- d = ((y, x - n), a - y * n, l + n) | ||
| D <- d = ((y + n, x), a, l + n) | ||
| R <- d = ((y, x + n), a + y * n, l + n) | ||
|
||
part1, part2 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part1 = solve $ do | ||
d <- choice [char 'U' $> U, char 'L' $> L, char 'D' $> D, char 'R' $> R] | ||
n <- hspace1 *> decimal | ||
hspace1 *> string "(#" *> skipManyTill hexDigitChar (char ')') $> (d, n) | ||
part2 = solve $ do | ||
oneOf @[] "ULDR" *> hspace1 *> skipManyTill digitChar hspace1 *> string "(#" | ||
(n, ""):_ <- readHex <$> replicateM 5 hexDigitChar | ||
d <- choice [char '0' $> R, char '1' $> D, char '2' $> L, char '3' $> U] | ||
char ')' $> (d, n) |
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,35 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day18Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day18 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = T.unlines | ||
[ -- :r!wl-paste | sed 's/.*/ , "&"/;1s/,/ /' | ||
"R 6 (#70c710)" | ||
, "D 5 (#0dc571)" | ||
, "L 2 (#5713f0)" | ||
, "D 2 (#d2c081)" | ||
, "R 2 (#59c680)" | ||
, "D 2 (#411b91)" | ||
, "L 5 (#8ceee2)" | ||
, "U 2 (#caa173)" | ||
, "L 1 (#1b58a2)" | ||
, "U 2 (#caa171)" | ||
, "R 2 (#7807d2)" | ||
, "U 3 (#a77fa3)" | ||
, "L 2 (#015232)" | ||
, "U 2 (#7a21e3)" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` Right 62 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` Right 952408144115 |