-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.hs
108 lines (87 loc) · 2.47 KB
/
Day13.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
module Day13 where
import Control.Monad
import Data.Bifunctor
import Data.Functor
import Text.ParserCombinators.ReadP
import Harness
import ParseHelper
import qualified Data.Array as A
import qualified Data.List as L
main :: IO ()
main = getInputAndSolve (parseInputRaw parsePuzzle) foldOne foldAll
-- SOLVE
foldOne :: Puzzle -> Int
foldOne Puzzle{..} =
length . L.nub . foldPaper pDots $ head pFolds
foldAll :: Puzzle -> PrettyOutput
foldAll Puzzle{..} =
renderDots $ L.foldl' foldPaper pDots pFolds
-- HELPERS
foldPaper :: [(Int, Int)] -> Fold -> [(Int, Int)]
foldPaper dots = \case
FoldX i ->
foldOn first i dots
FoldY i ->
foldOn second i dots
foldOn
:: ((Int -> Int) -> (Int, Int) -> (Int, Int))
-> Int
-> [(Int, Int)] -> [(Int, Int)]
foldOn updater line =
map . updater $ \v ->
if v > line then
2 * line - v
else
v
-- | Helper type to prevent quotes/escape-sequences from rendering when
-- a solver returns a String.
--
-- TODO: if useful for more exercises, move to Harness module.
newtype PrettyOutput = PrettyOutput String
-- | Render with leading newline + indentation.
instance Show PrettyOutput where
show (PrettyOutput s) = ("\n" <>) $ unlines $ map ("\t" <>) $ lines s
-- | Render the dots as a grid of Chars
renderDots :: [(Int, Int)] -> PrettyOutput
renderDots dots =
PrettyOutput
. A.showGridWith (: [])
. A.sparseGrid ' '
$ map (, '#') dots
-- PARSE
data Puzzle
= Puzzle
{ pDots :: ![(Int, Int)]
, pFolds :: ![Fold]
} deriving (Show, Read, Eq, Ord)
data Fold
= FoldX !Int
| FoldY !Int
deriving (Show, Read, Eq, Ord)
parsePuzzle :: ReadP Puzzle
parsePuzzle = do
pDots <- sepBy parseDot newline
replicateM_ 2 newline
pFolds <- sepBy parseFold newline
newline $> Puzzle{..}
where
parseDot :: ReadP (Int, Int)
parseDot = do
x <- parseInt
void $ char ','
y <- parseInt
return (x, y)
parseFold :: ReadP Fold
parseFold =
choice
[ fmap FoldY $ string "fold along y=" *> parseInt
, fmap FoldX $ string "fold along x=" *> parseInt
]