-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.hs
More file actions
25 lines (19 loc) · 687 Bytes
/
Copy pathday03.hs
File metadata and controls
25 lines (19 loc) · 687 Bytes
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
#!/usr/bin/env runghc
module Main where
import Control.Arrow ((&&&))
import Data.Char (digitToInt)
import Data.List (maximumBy, tails)
import Data.Ord (comparing)
main = interact (unlines . sequence [part1, part2] . banks)
part1, part2 :: [[Int]] -> String
part1 = ("Part 1: " ++) . show . sum . map (joltage 2)
part2 = ("Part 2: " ++) . show . sum . map (joltage 12)
joltage = joltage' 0
joltage' :: Int -> Int -> [Int] -> Int
joltage' acc n bank
| n <= 0 = acc
| otherwise = joltage' (acc * 10 + j) (n - 1) bank'
where
i = max 1 (length bank - n + 1)
(j : bank') = maximumBy (comparing (take 1 &&& length)) (take i (tails bank))
banks = map (map digitToInt) . lines