Day 1
This commit is contained in:
parent
6c6d0dea61
commit
511477ed3d
2000
inputs/day1.txt
Normal file
2000
inputs/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
38
src/day1.rs
38
src/day1.rs
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user