Skip to content

Commit 092c792

Browse files
committedDec 1, 2021
2021 (and 2019): Day 1
1 parent 3d2afdd commit 092c792

File tree

6 files changed

+2154
-1
lines changed

6 files changed

+2154
-1
lines changed
 

‎2019/01/input.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
74767
2+
108567
3+
135114
4+
103725
5+
55085
6+
144135
7+
88766
8+
94314
9+
109095
10+
114013
11+
91594
12+
97858
13+
122165
14+
80803
15+
94873
16+
98280
17+
116305
18+
66960
19+
85105
20+
97510
21+
51829
22+
50460
23+
86361
24+
71217
25+
77310
26+
68460
27+
60591
28+
109303
29+
66381
30+
139184
31+
93497
32+
116217
33+
93193
34+
92289
35+
104371
36+
74040
37+
124924
38+
125877
39+
144950
40+
139877
41+
104798
42+
148258
43+
98386
44+
145120
45+
75609
46+
80208
47+
68458
48+
138641
49+
147555
50+
81179
51+
70443
52+
108683
53+
148921
54+
64459
55+
127861
56+
83336
57+
50123
58+
102155
59+
118397
60+
139916
61+
115265
62+
112932
63+
142676
64+
106577
65+
87480
66+
122386
67+
51573
68+
61156
69+
140013
70+
87671
71+
122005
72+
82909
73+
141790
74+
61341
75+
123625
76+
91724
77+
69630
78+
112495
79+
145851
80+
79977
81+
107629
82+
130937
83+
127680
84+
56887
85+
73639
86+
68652
87+
143813
88+
50498
89+
102140
90+
55277
91+
86773
92+
53889
93+
148907
94+
94901
95+
53640
96+
129436
97+
105184
98+
71527
99+
100433
100+
56709

‎2019/day01.hs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
main :: IO ()
2+
main = do
3+
input <- readFile "./01/input.txt"
4+
let list = read <$> lines input
5+
print $ "Part 1: " <> show (solve1 list)
6+
print $ "Part 2: " <> show (solve2 list)
7+
8+
solve1 = sum . map fuelForMass
9+
solve2 = sum . map fuelForMassAndFuel
10+
11+
fuelForMass :: (Integral a) => a -> a
12+
fuelForMass n = (n `div` 3) - 2
13+
14+
fuelForMassAndFuel :: (Integral a) => a -> a
15+
fuelForMassAndFuel = sum . takeWhile (> 0) . drop 1 . iterate fuelForMass

‎2021/01/input.txt

+2,000
Large diffs are not rendered by default.

‎2021/day01.hs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Data.List (tails)
2+
3+
main :: IO ()
4+
main = do
5+
input <- readFile "./01/input.txt"
6+
let list = read <$> lines input
7+
print $ "Part 1: " <> show (solve1 list)
8+
print $ "Part 2: " <> show (solve2 list)
9+
10+
solve1 :: [Int] -> Int
11+
solve1 = length . filter (uncurry (<)) . pairs
12+
13+
solve2 :: [Int] -> Int
14+
solve2 input = length . filter (uncurry (<)) $ pairs (map sum . windows 3 $ input)
15+
16+
pairs :: [a] -> [(a,a)]
17+
pairs xs@(_:xs') = zip xs xs'
18+
19+
windows :: Int -> [a] -> [[a]]
20+
windows m = foldr (zipWith (:)) (repeat []) . take m . tails

‎hie.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cradle:
2+
direct:
3+
arguments: []

‎readme.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Advent of Code 2021
2+
3+
## Day 1:
4+
5+
The `windows` function from last year (day 9) ended up being useful already!
6+
7+
18
# Advent of Code 2020
29

310
## Days 1 - 7:
@@ -102,7 +109,7 @@ against the full input was successful as well, and instantaneous.
102109
Sort of a game of life simulation but with seating charts. As with all of these
103110
I'm sure my solution is naive and slow, and I'm working on a rewrite before I
104111
complete the second part...
105-
112+
https://stackoverflow.com/questions/7442892/repeatedly-applying-a-function-until-the-result-is-stable
106113

107114
## Day 12:
108115

@@ -113,3 +120,11 @@ applying ship instructions in reverse -- this actually worked for part one, but
113120
since the instructions in part two are order dependant, it was completely wrong
114121
even on the first instruction. I finally realized my mistake after stepping
115122
through the solver function with `Debug.Trace`.
123+
124+
125+
# Advent of Code 2019
126+
127+
## Day 1:
128+
129+
Did this as a warm-up right before the 2021 AoC opened, initially in the REPL.
130+
I haven't written any Haskell in 9 months besides messing with [xmonad](https://github.com/xmonad/xmonad).

0 commit comments

Comments
 (0)
Please sign in to comment.