1
0
Fork 0

solve: day 10

- solve: day 10, part 1
  - solve: day 10, part 2
This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 8dcd832fdc
commit 8dae9b8c45
3 changed files with 47 additions and 0 deletions

4
2020/10/day10.hs Normal file
View file

@ -0,0 +1,4 @@
module Day10 where
parse :: String -> [Int]
parse = map read . lines

15
2020/10/part1.hs Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env runghc
import Day10
import Data.List
main = interact (show . solve . parse)
solve xs = ones * threes
where
sorted = 0:(sort xs)
differences = zipWith (flip (-)) sorted (tail sorted)
ones = count 1 differences
threes = count 3 differences + 1
count x = length . filter (==x)

28
2020/10/part2.hs Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env runghc
import Day10
import Data.Array
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
main = interact (show . solve . parse)
countPaths inp = pathsFrom start
where
start = IntSet.findMax inpSet + 3
pathsFrom 0 = 1
pathsFrom x
| x < 0 = 0
| IntSet.member x allowed =
sum $ map ((!) memo) $ filter (>= 0) $ map ((-) x) [1, 2, 3]
| otherwise = 0
inpSet = IntSet.fromList inp
allowed = IntSet.insert start inpSet
memo = listArray bnds
[ pathsFrom i | i <- range bnds ]
bnds = (0, start)
solve = countPaths