tangled
alpha
login
or
join now
eldridge.cam
/
advent-of-code
2
fork
atom
Advent of Code solutions
2
fork
atom
overview
issues
pulls
pipelines
2021:7
eldridge.cam
1 year ago
15b00409
afbd5b1a
+26
2 changed files
expand all
collapse all
unified
split
2021
7
p1.hs
p2.hs
+12
2021/7/p1.hs
···
1
1
+
import Text.Parsec
2
2
+
3
3
+
crabs = (read <$> many1 digit) `sepBy` char ','
4
4
+
5
5
+
answer contents = minimum $ fmap fuel [lo..hi]
6
6
+
where
7
7
+
Right input = parse crabs "" contents
8
8
+
lo = minimum input
9
9
+
hi = maximum input
10
10
+
fuel n = sum $ fmap (abs . (n -)) input
11
11
+
12
12
+
main = getContents >>= print . answer
+14
2021/7/p2.hs
···
1
1
+
import Text.Parsec
2
2
+
3
3
+
crabs = (read <$> many1 digit) `sepBy` char ','
4
4
+
5
5
+
total n = (n * (n + 1)) `div` 2
6
6
+
7
7
+
answer contents = minimum $ fmap fuel [lo..hi]
8
8
+
where
9
9
+
Right input = parse crabs "" contents
10
10
+
lo = minimum input
11
11
+
hi = maximum input
12
12
+
fuel n = sum $ fmap (total . abs . (n -)) input
13
13
+
14
14
+
main = getContents >>= print . answer