-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.idr
40 lines (32 loc) · 1.12 KB
/
Day1.idr
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
module Advent.Day1
import Advent.Util
import Data.IORef
import Data.List
import Data.SortedSet
import Data.Stream
export
Part1 : IO ()
Part1 = do
calibration <- newIORef 0
Util.processStdin \input_line => modifyIORef calibration (+ (cast input_line))
printLn !(readIORef calibration)
export
Part2: IO ()
Part2 = do
changeListLIFO <- newIORef []
Util.processStdin \input_line => modifyIORef changeListLIFO (cast input_line ::)
putStrLn$ frequencyScanner$ reverse$ !(readIORef changeListLIFO)
where
frequencyScanner : List Int -> String
frequencyScanner [] = "error: no input"
frequencyScanner changeList@(_ :: _) = show firstRepeat
where
startingFrequency : Int
startingFrequency = 0
accumulatedWithHistory : Stream (Int, SortedSet Int)
accumulatedWithHistory = scanl
(\(current,reached), change => (current+change, insert current reached))
(startingFrequency, SortedSet.empty)
(Stream.cycle changeList)
firstRepeat : Int
firstRepeat = fst$ Util.first (uncurry SortedSet.contains) accumulatedWithHistory