Skip to content

Commit 30da951

Browse files
committed
Day 2
1 parent 19307a3 commit 30da951

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sh eol=lf

2024/README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
# 2024
2-
3-
## Day 1 – Awk
4-
5-
```console
6-
$ awk -f day01-01.awk < input1
7-
$ awk -f day01-02.awk < input1
8-
```
1+
# 2024
2+
3+
## Day 1 – Awk
4+
5+
```console
6+
$ awk -f day01-01.awk < input1
7+
$ awk -f day01-02.awk < input1
8+
```
9+
10+
## Day 2 – Haskell
11+
12+
```console
13+
$ runhaskell day02-01.hs < input2
14+
$ runhaskell day02-02.hs < input2
15+
```

2024/day02-01.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
isValid :: [Int] -> Bool
2+
isValid levels = safe levels || safe (reverse levels)
3+
where
4+
safe = all (\x -> x >= 1 && x <= 3) . deltas
5+
deltas l = zipWith (-) l (tail l)
6+
7+
main = do
8+
reports <- map (map read) . map words . lines <$> getContents
9+
let validReports = filter isValid reports
10+
print (length validReports)

2024/day02-02.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
isValid :: [Int] -> Bool
2+
isValid levels = safe levels || safe (reverse levels)
3+
where
4+
safe = all (\x -> x >= 1 && x <= 3) . deltas
5+
deltas l = zipWith (-) l (tail l)
6+
7+
drop1 :: [a] -> [[a]]
8+
drop1 [] = []
9+
drop1 (x:xs) = xs : map (x:) (drop1 xs)
10+
11+
main = do
12+
reports <- map (map read) . map words . lines <$> getContents
13+
let validReports = filter (any isValid . drop1) reports
14+
print (length validReports)

0 commit comments

Comments
 (0)