# Advent of Code 2021, Day 1

Hohoho, Advent of Code is upon us! Solve two programming problems every day, because that’s fun!

As a scheming Haskeller, and a haskelling Schemer, that’s what I’m going to use. The solutions will be on my Github.

Without further ado, here we go.

## Scheme

All Advent of Code challenges come in two parts.

### Part 1

Here’s the problem we need to solve: Our input is a list of depths measured by sonar. We need to figure out how often depth increases compared to the prior measurement.

I like to keep my functions composable and pure, so we’ll start with the impure part, lets first isolate the input. We’ll read it from a text file named `Input.txt`

and turn it into a list:

```
(define input
(letrec ((input-file (open-input-file "Input.txt"))
(R (lambda (ls)
(let ((next (read input-file)))
(if (eof-object? next)
ls
(R (cons next ls)))))))
(reverse (R '()))))
```

Looking at it, the `let`

statement is a bit overkill, but it’s not really problematic here. We basically collect all the values read line by line from `Input.txt`

and append it to a list. Since we go line by line, we need to reverse the list with `reverse`

.

Next, a little helper to remove the last element of a given list:

```
(define all-but-last
(lambda (ls)
(reverse (cdr (reverse ls)))))
```

Nothing special there. To calculate the difference between the *nth* and the *(n+1)th*, we simply copy the original list of depths and remove the last element from the original, and the first from the copy. This way, both lists have the same length and we can simply `map`

the subtraction between those two lists:

```
(define depth-diffs
(lambda (depths)
(let ((minuends (all-but-last depths))
(subtrahends (cdr depths)))
(map - minuends subtrahends))))
```

Now we have the differences between the measured depths. As the last step, we simple “count” the number of negative changes (aka, depth increases) with a simple fold:

```
(define depth-increases
(lambda (diffs)
(fold-right (lambda (d ds)
(if (< d 0)
(+ 1 ds)
ds))
0
diffs)))
```

That’s it. Now we only need to compose our functions into a solution:

```
(define solution-part1 (depth-increases (depth-diffs input)))
```

Running this program with Chez Scheme with my input yields the correct result of 1624.

### Part 2

For part 2, we need to calculate the same depth differences, but this time we use the “average” of three measurements by adding them up:

```
(define depth-triple-sums
(let ((summands1 (all-but-last (all-but-last input)))
(summands2 (all-but-last (cdr input)))
(summands3 (cddr input)))
(map + summands1 summands2 summands3)))
```

The list `depth-triple-sums`

now holds the sum of three consecutive measurements each. Since we made our functions easy to reuse, we can simply write:

```
(define solution-part2 (depth-increases (depth-diffs depth-triple-sums)))
```

This yields the correct answer of 1654 for my input.

Awesome! Let’s do it again in Haskell.

## Haskell

I’ll show you the whole code for both parts since it’s almost an exact mirror of the Scheme version:

```
import System.IO
main :: IO ()
main = do
handle <- openFile "Input.txt" ReadMode
contents <- hGetContents handle
let depths = map read . lines $ contents :: [Integer]
print $ "Part 1: " ++ show (part1 depths)
print $ "Part 2: " ++ show (part2 depths)
hClose handle
depthDiffs :: [Integer] -> [Integer]
depthDiffs xs = zipWith (-) xs (tail xs)
depthIncreases :: [Integer] -> Integer
depthIncreases = foldr (\x xs -> if x < 0 then 1 + xs else xs) 0
part1 :: [Integer] -> Integer
part1 = depthIncreases . depthDiffs
depthTripleSums :: [Integer] -> [Integer]
depthTripleSums xs = zipWith3 (\a b c -> a + b + c) xs (tail xs) (tail . tail $ xs)
part2 :: [Integer] -> Integer
part2 = part1 . depthTripleSums
```

Both programs are structured similarly. There are only two real differences. One, I hid the whole input reading in the `IO ()`

monad `main`

, and two, `depthTripleSums`

is a function here. For the simple reason that I can use Haskell’s composition operator to create the functions that calculate the final results.

That’s it for today. You can find all my solutions on my Github.

See you tomorrow.