diff options
| author | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
| commit | baf4910870a6e8999802b9a4a22eabd4142a34e3 (patch) | |
| tree | 2d11443dc21e53bd0d99d015cf789937d6d95862 /2021/src/day01.rs | |
| parent | 49d0c908f24b2c193c9deed1716fe36061ba26a1 (diff) | |
Move all Advent of Codes into one repo
Diffstat (limited to '2021/src/day01.rs')
| -rw-r--r-- | 2021/src/day01.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/2021/src/day01.rs b/2021/src/day01.rs new file mode 100644 index 0000000..7b1e6a9 --- /dev/null +++ b/2021/src/day01.rs @@ -0,0 +1,39 @@ +use std::error::Error; +use std::fs; +use std::path::Path; + +fn get_input(path: &Path) -> Result<Vec<u16>, Box<dyn Error>> { + let data = fs::read_to_string(path)?; + Ok(data.lines().filter_map(|x| x.parse::<u16>().ok()).collect()) +} + +#[cfg(part1)] +pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> { + let values = get_input(path)?; + let mut last = u16::MAX; + let mut larger = 0; + for v in values { + if v > last { + larger += 1; + } + + last = v; + } + + println!("{}", larger); + Ok(()) +} + +#[cfg(part2)] +pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> { + let values = get_input(path)?; + let mut increase = 0; + for i in 0..(values.len() - 3) { + if values[i] < values[i + 3] { + increase += 1; + } + } + + println!("{}", increase); + Ok(()) +} |
