This commit is contained in:
Zoé Cassiopée Gauthier 2021-12-01 14:48:07 -05:00
parent 6c6d0dea61
commit 511477ed3d
2 changed files with 2036 additions and 2 deletions

2000
inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,37 @@
fn main() {
println!("Hello, world!");
use std::io::{self, BufRead};
fn increases(measurements: &Vec<u64>) -> u64 {
measurements
.windows(2)
.fold(0, |acc, x| if x[1] > x[0] { acc + 1 } else { acc })
}
fn convolve(measurements: &Vec<u64>) -> Vec<u64> {
measurements.windows(3).map(|x| x.iter().sum()).collect()
}
fn main() {
const INPUT: &'static str = include_str!("../inputs/day1.txt");
let measurements: Vec<u64> = io::Cursor::new(INPUT)
.lines()
.map(|line| line.unwrap().parse().unwrap())
.collect();
println!("increased {} times", increases(&measurements));
println!(
"increased {} times",
increases(&convolve(&measurements))
);
}
#[test]
fn part1_example() {
let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
assert_eq!(increases(&measurements), 7);
}
#[test]
fn part2_example() {
let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
assert_eq!(increases(&convolve(&measurements)), 5);
}