Skip to content

Commit 4718e3b

Browse files
committed
refactor(2020.17-haskell): adopt advent-of-code-api
1 parent 0a22418 commit 4718e3b

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2024.7.2.84
1+
2024.7.2.85

input/2020/day17.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ executables:
443443
<<: *executable
444444
main: AdventOfCode.Year2020.Day17
445445
dependencies:
446+
- infinite-list
446447
- linear
447448
aoc-2020-day18:
448449
<<: *executable

src/AdventOfCode/Year2020/Day17.hs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
13
module AdventOfCode.Year2020.Day17
24
( main,
35
getInput,
@@ -6,37 +8,46 @@ module AdventOfCode.Year2020.Day17
68
)
79
where
810

9-
import AdventOfCode.Input (parseInput)
10-
import AdventOfCode.TH (inputFilePath)
11+
import AdventOfCode.Input (parseInputAoC)
12+
import AdventOfCode.Puzzle
13+
import AdventOfCode.TH (defaultMainPuzzle)
1114
import AdventOfCode.Util (neighborsOf)
12-
import Control.Applicative ((<|>))
1315
import Control.Lens (ifoldl', set)
14-
import Data.Functor (($>))
16+
import Data.List.Infinite ((!!))
17+
import Data.List.Infinite qualified as Infinite
1518
import Data.Map qualified as Map
16-
import Data.Set (Set)
1719
import Data.Set qualified as Set
1820
import Linear (R2 (..), V2 (..), V3 (..), V4 (..))
21+
import Relude
1922
import Text.Trifecta
2023

24+
type Domain f =
25+
( Applicative f,
26+
Traversable f,
27+
R2 f,
28+
Num (f Int),
29+
Ord (f Int)
30+
) ::
31+
Constraint
32+
33+
type Codomain f a = Set (f Int) -> a
34+
2135
main :: IO ()
22-
main =
23-
do
24-
input <- getInput
25-
putStr "Part One: "
26-
print $ partOne (mkPocketDimension input)
27-
putStr "Part Two: "
28-
print $ partTwo (mkPocketDimension input)
36+
main = $(defaultMainPuzzle)
2937

3038
getInput :: IO [[Bool]]
31-
getInput = parseInput region $(inputFilePath)
39+
getInput = parseInputAoC 2020 17 cubeRegion
40+
41+
partOne :: SimplePuzzle [[Bool]] Int
42+
partOne = asks (solve . mkPocketDimension @V3)
3243

33-
partOne :: Set (V3 Int) -> Int
34-
partOne = Set.size . (!! 6) . iterate stepCycle
44+
partTwo :: SimplePuzzle [[Bool]] Int
45+
partTwo = asks (solve . mkPocketDimension @V4)
3546

36-
partTwo :: Set (V4 Int) -> Int
37-
partTwo = Set.size . (!! 6) . iterate stepCycle
47+
solve :: (Domain f) => Codomain f Int
48+
solve = Set.size . (!! 6) . Infinite.iterate stepCycle
3849

39-
stepCycle :: (Applicative f, Traversable f, Num (f Int), Ord (f Int)) => Set (f Int) -> Set (f Int)
50+
stepCycle :: (Domain f) => Codomain f (Set (f Int))
4051
stepCycle activeCubes = stillActive <> activated
4152
where
4253
stillActive = filterNeighborCounts (\n -> n == 2 || n == 3) activeNeighborCounts
@@ -45,19 +56,22 @@ stepCycle activeCubes = stillActive <> activated
4556
inactiveNeighborCounts = neighborCounts `Map.withoutKeys` activeCubes
4657
filterNeighborCounts p = Map.keysSet . Map.filter p
4758
neighborCounts =
48-
Map.unionsWith ((+) :: Int -> Int -> Int) $
49-
Map.fromSet (const 1) . neighborsOf
50-
<$> Set.toList activeCubes
59+
Map.unionsWith ((+) :: Int -> Int -> Int)
60+
$ Map.fromSet (const 1)
61+
. neighborsOf
62+
<$> Set.toList activeCubes
5163

52-
mkPocketDimension :: (Applicative f, R2 f, Ord (f Int)) => [[Bool]] -> Set (f Int)
64+
mkPocketDimension :: (Domain f) => [[Bool]] -> Set (f Int)
5365
mkPocketDimension = ifoldl' (ifoldl' . go) Set.empty
5466
where
55-
go y x activeCubes True = Set.insert (set _xy (V2 x y) (pure 0)) activeCubes
67+
go y x activeCubes True = Set.insert (set _xy (V2 x y) 0) activeCubes
5668
go _ _ activeCubes False = activeCubes
5769

58-
region :: Parser [[Bool]]
59-
region = some cube `sepEndBy` newline
70+
cubeRegion :: Parser [[Bool]]
71+
cubeRegion = some cube `sepEndBy` newline
6072
where
6173
cube =
62-
char '#' $> True
63-
<|> char '.' $> False
74+
char '#'
75+
$> True
76+
<|> char '.'
77+
$> False

0 commit comments

Comments
 (0)