-
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
85 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,37 @@ | ||
{-| | ||
Module: Day13 | ||
Description: <https://adventofcode.com/2023/day/13 Day 13: Point of Incidence> | ||
-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day13 (part1, part2) where | ||
|
||
import Data.List (findIndex, inits, tails) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (commonPrefixes, drop, lines, splitOn, transpose) | ||
|
||
findReflection :: ([a] -> [a] -> Bool) -> [a] -> Int | ||
findReflection _ [] = 0 | ||
findReflection eq lines = maybe 0 succ . findIndex (uncurry $ eq . reverse) . | ||
drop 1 . init $ zip (inits lines) (tails lines) | ||
|
||
part1 :: Text -> Int | ||
part1 = sum . map (part1' . T.lines) . T.splitOn "\n\n" where | ||
part1' lines = 100 * y + x where | ||
x = findReflection eq $ T.transpose lines | ||
y = findReflection eq lines | ||
xs `eq` ys = and $ zipWith (==) xs ys | ||
|
||
part2 :: Text -> Int | ||
part2 = sum . map (part2' . T.lines) . T.splitOn "\n\n" where | ||
part2' lines = 100 * y + x where | ||
x = findReflection (almostEqual False) $ T.transpose lines | ||
y = findReflection (almostEqual False) lines | ||
almostEqual k [] _ = k | ||
almostEqual k _ [] = k | ||
almostEqual k (x:xs) (y:ys) | ||
| x == y = almostEqual k xs ys | ||
| (_, x', y') <- fromMaybe ("", x, y) $ T.commonPrefixes x y | ||
, T.drop 1 x' == T.drop 1 y' | ||
= not k && almostEqual True xs ys | ||
| otherwise = False |
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,36 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day13Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day13 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = T.unlines | ||
[ -- :r!wl-paste | sed 's/.*/ , "&"/;1s/,/ /' | ||
"#.##..##." | ||
, "..#.##.#." | ||
, "##......#" | ||
, "##......#" | ||
, "..#.##.#." | ||
, "..##..##." | ||
, "#.#.##.#." | ||
, "" | ||
, "#...##..#" | ||
, "#....#..#" | ||
, "..##..###" | ||
, "#####.##." | ||
, "#####.##." | ||
, "..##..###" | ||
, "#....#..#" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 405 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` 400 |