-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
127 lines (111 loc) · 4.51 KB
/
Main.hs
File metadata and controls
127 lines (111 loc) · 4.51 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module Main where
import Data.Attoparsec.ByteString.Lazy (eitherResult, parse)
import Data.Foldable (find)
import qualified Data.IntSet as IntSet
import qualified Data.List as List
import Data.Maybe (fromJust)
import Data.Monoid (Sum (Sum))
import qualified Data.Sequence as Seq (sort)
import qualified Data.Set as Set
import DayEight (dayEightInput, fixInstructions)
import DayEleven (countOccupiedSeats, dayElevenInput, updateUntilStable)
import DayFive (dayFiveInput, determineSeatId)
import DayFour (dayFourInput, passportList, validPassport)
import DayNine (dayNineInput, findContiguousSeqAddingTo, findInvalidInt)
import DayOne
( dayOneInput,
find2NumbersAddingTo,
find3NumbersAddingTo,
)
import DaySeven (countContaining, countNestedBags, daySevenInput, rules)
import DaySix (daySixInput, groups)
import DayTen (countJolts, dayTenInput, possibleAdapterConfigs, steps)
import DayThirteen (dayThirteenInput, findTimestamp, waitTime)
import DayThree (checkSlopes, dayThreeInput)
import DayTwelve (dayTwelveInput, runInstructions)
import DayTwo
( dayTwoInput,
parseLine,
testPassword,
testPassword',
)
sumWhen :: (Foldable t, Functor t) => (a -> Bool) -> t a -> Int
sumWhen f = sum . fmap (inc . f)
where
inc True = 1
inc False = 0
main :: IO ()
main = do
inputDayOne <- Seq.sort <$> dayOneInput
let (a, b) = fromJust $ find2NumbersAddingTo 2020 inputDayOne
print "Day One"
print ("Part One: " ++ show (a * b))
let (c, d, e) = fromJust $ find3NumbersAddingTo 2020 inputDayOne
print ("Part Two: " ++ show (c * d * e))
inputDayTwo <- dayTwoInput
print "Day Two"
case traverse parseLine inputDayTwo of
Right passwords -> do
print ("Part One: " ++ show (sumWhen (uncurry testPassword) passwords))
print ("Part Two: " ++ show (sumWhen (uncurry testPassword') passwords))
Left err -> print $ "Could not parse input for Day Two: " ++ err
inputDayThree <- dayThreeInput
print "Day Three"
print ("Part One: " ++ show (checkSlopes inputDayThree (3, 1)))
print ("Part Two: " ++ show (product $ checkSlopes inputDayThree <$> [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]))
inputDayFour <- dayFourInput
print "Day Four"
print "Part One: Invalidated by changes"
case eitherResult $ parse passportList inputDayFour of
Right passports -> print ("Part Two:" ++ show (sumWhen validPassport passports))
Left err -> print $ "Could not parse input for Day Four: " ++ err
inputDayFive <- dayFiveInput
print "Day Five"
let seatIds = determineSeatId <$> inputDayFive
let maxSeatId = maximum seatIds
print ("Part One: " ++ show maxSeatId)
let seatIds' = IntSet.fromList seatIds
print ("Part Two: " ++ show (find (not . (`IntSet.member` seatIds')) [(minimum seatIds) .. maxSeatId]))
inputDaySix <- daySixInput
print "Day Six"
case eitherResult $ parse groups inputDaySix of
Right gs -> do
print "Part One: Invalidated by changes"
print ("Part Two: " ++ show (sum (Set.size <$> gs)))
Left err -> print ("Issue with Day Six input: " ++ err)
inputDaySeven <- daySevenInput
print "Day Seven"
case eitherResult $ parse rules inputDaySeven of
Right bags -> do
print ("Part One: " ++ show (countContaining bags "shinygold"))
print ("Part Two: " ++ show (countNestedBags "shinygold" bags))
Left err -> print ("Issue with Day Seven Input: " ++ err)
inputDayEight <- dayEightInput
print "Day Eight"
print "Part One: Invalidated by changes"
print ("Part Two: " ++ show (fixInstructions inputDayEight))
inputDayNine <- dayNineInput
print "Day Nine"
print ("Part One: " ++ show (findInvalidInt 25 inputDayNine))
print ("Part Two: " ++ show (findContiguousSeqAddingTo 177777905 inputDayNine))
inputDayTen <- dayTenInput
print "Day Ten"
let diffs = steps (List.sort inputDayTen)
let (Sum jolts3, Sum jolts1) = countJolts diffs
print ("Part One: " ++ show (jolts1 * jolts3))
print ("Part Two: " ++ show (possibleAdapterConfigs inputDayTen))
inputDayEleven <- dayElevenInput
print "Day Eleven"
print "Part One: Invalidated by changes "
print ("Part Two: " ++ show (countOccupiedSeats (updateUntilStable inputDayEleven)))
inputDayTwelve <- dayTwelveInput
print "Day Twelve"
let (x, y) = runInstructions inputDayTwelve
print "Part One: Invalidated by changes"
print ("Part Two: " ++ show (abs x + abs y))
inputDayThirteen <- dayThirteenInput
print "Day Thirteen"
let Just result1 = waitTime inputDayThirteen
print ("Part One: " ++ show result1)
let result2 = findTimestamp inputDayThirteen
print ("Part One: " ++ show result2)