-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.hs
More file actions
68 lines (53 loc) · 1.67 KB
/
run.hs
File metadata and controls
68 lines (53 loc) · 1.67 KB
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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import AoC.Draw.Chars
import Data.Bifunctor
import Data.Foldable
import Data.List
type Paper = [(Int, Int)]
parsePoint :: String -> (Int, Int)
parsePoint l = read @(Int, Int) $ "(" ++ l ++ ")"
parseFold :: String -> (Int, Int)
parseFold f = case stripPrefix "fold along " f of
Just ('x':'=':v) -> (read @Int v, 0)
Just ('y':'=':v) -> (0, read @Int v)
parseAll :: String -> (Paper, [(Int, Int)])
parseAll input =
let (points, folds) = break null (lines input)
in (map parsePoint points, map parseFold (drop 1 folds))
paper :: Paper -> [[Bool]]
paper points =
let (cs, rs) = unzip points
cmax = maximum cs
rmax = maximum rs
cmin = minimum cs
rmin = minimum rs
in [ [ (ci, ri) `elem` points | ci <- [cmin..cmax] ] | ri <- [rmin..rmax] ]
foldLine :: Int -> Paper -> Paper
foldLine x = map (first f)
where f = \case i | i > x -> 2 * x - i
| otherwise -> i
swap :: (Int, Int) -> (Int, Int)
swap (x, y) = (y, x)
paperFold :: (Int, Int) -> Paper -> Paper
paperFold =
\case (x, 0) -> nub . foldLine x
(0, y) -> nub . map swap . foldLine y . map swap
pp :: [(Int, Int)] -> String
pp = ppGrid (\case True -> 'X'
False -> ' ') . paper
part1 (points, f:_) = length $ paperFold f points
part2 (points, folds) =
(\case Right x -> x)
. readLetters
. pp
. foldl' (flip paperFold) points $ folds
main = main' "input.txt"
exampleMain = main' "example.txt"
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
putStrLn (part2 input)