day 1 solution
This commit is contained in:
1 parent
74b4a8d279
commit
3fae3a8963
5 files changed
+2067
-2
No files matched your search
Generated
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2021"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
]
|
||||||
@@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
199
|
||||||
|
200
|
||||||
|
208
|
||||||
|
210
|
||||||
|
200
|
||||||
|
207
|
||||||
|
240
|
||||||
|
269
|
||||||
|
260
|
||||||
|
263
|
||||||
+2000
File diff suppressed because it is too large.
Load diff
+40
-2
@@ -1,3 +1,41 @@
|
|||||||
fn main() {
|
use anyhow::Result;
|
||||||
println!("Hello, world!");
|
use std::fs;
|
||||||
|
|
||||||
|
fn read_i64s(filename: &str) -> Result<Vec<i64>> {
|
||||||
|
Ok(read_file(filename)?
|
||||||
|
.lines()
|
||||||
|
.map(|s| s.parse::<i64>().expect("i64 parse error"))
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_file(filename: &str) -> Result<String> {
|
||||||
|
Ok(fs::read_to_string(filename)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_incrementing_windows(window: usize, v: &[i64]) -> usize {
|
||||||
|
// create an array of the window sums and then window(2) that array to count increments
|
||||||
|
v.windows(window)
|
||||||
|
.map(|w| w.iter().sum())
|
||||||
|
.collect::<Vec<i64>>()
|
||||||
|
.windows(2)
|
||||||
|
.filter(|s| s[1] > s[0])
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn day01() -> Result<(usize, usize)> {
|
||||||
|
let data = read_i64s("data/day01-test.txt")?;
|
||||||
|
assert_eq!(count_incrementing_windows(1, &data), 7);
|
||||||
|
assert_eq!(count_incrementing_windows(3, &data), 5);
|
||||||
|
|
||||||
|
let data = read_i64s("data/day01.txt")?;
|
||||||
|
let part1 = count_incrementing_windows(1, &data);
|
||||||
|
let part2 = count_incrementing_windows(3, &data);
|
||||||
|
assert_eq!(part1, 1298);
|
||||||
|
assert_eq!(part2, 1248);
|
||||||
|
Ok((part1, part2))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
println!("day01: {:?}", day01()?);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user