Day 01 - Sonar Sweep
Language: Rust
Problem https://adventofcode.com/2021/day/1
Part 1:
A list of depth measurements. Count how many times consecutive values increase. windows(2)
makes this clean:
let ans = numbers.windows(2)
.filter(|pair| pair[0] < pair[1])
.count();
Part 2: Now compare sums of consecutive 3-element windows. The naive approach is to sum each window and compare adjacent sums, but two neighboring windows of size 3 share two elements. That means the comparison reduces to just checking whether the new element is larger than the one that fell off.
So instead of summing anything, windows(4) and comparing the first and last element
directly does the same thing:
let ans = numbers.windows(4)
.filter(|w| w[0] < w[3])
.count();
Clean and fast.
Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2021/day01.rs
No C writeup yet.