diff options
| author | Arne Dußin | 2021-12-01 13:05:54 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-12-01 13:05:54 +0100 |
| commit | 36dc513e41c349fa69231b81433ccbad911743a6 (patch) | |
| tree | 791221a43654e77bf6c49a8231dc0e814539dc05 /src/day_1.rs | |
| download | aoc2021-36dc513e41c349fa69231b81433ccbad911743a6.tar.gz aoc2021-36dc513e41c349fa69231b81433ccbad911743a6.zip | |
Add solution to day 1
Diffstat (limited to 'src/day_1.rs')
| -rw-r--r-- | src/day_1.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/day_1.rs b/src/day_1.rs new file mode 100644 index 0000000..6632baa --- /dev/null +++ b/src/day_1.rs @@ -0,0 +1,47 @@ +pub fn run(input: Vec<String>) +{ + // Convert inputs to unsigned integer numbers + let input: Vec<u32> = input + .into_iter() + .map(|s| { + s.parse() + .expect("All lines are required to be unsigned integers") + }) + .collect(); + + part1(&input); + part2(&input); +} + +fn part1(input: &[u32]) +{ + let mut last_depth = u32::MAX; + let num_down_slopes = input + .iter() + .filter(|&depth| { + let going_deeper = *depth > last_depth; + last_depth = *depth; + + going_deeper + }) + .count(); + + println!("Solution to part 1: {}", num_down_slopes); +} + +fn part2(input: &[u32]) +{ + let mut last_aliased_depth = u32::MAX; + let num_aliased_down_slopes = input + .windows(3) + .filter(|&depths| { + let aliased_depth: u32 = depths.iter().sum(); + let going_deeper = aliased_depth > last_aliased_depth; + last_aliased_depth = aliased_depth; + + going_deeper + }) + .count(); + + println!("Solution to part 2: {}", num_aliased_down_slopes); +} |
