From baf4910870a6e8999802b9a4a22eabd4142a34e3 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Thu, 1 Dec 2022 22:30:22 +0100 Subject: Move all Advent of Codes into one repo --- 2021/src/day01.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2021/src/day01.rs (limited to '2021/src/day01.rs') 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, Box> { + let data = fs::read_to_string(path)?; + Ok(data.lines().filter_map(|x| x.parse::().ok()).collect()) +} + +#[cfg(part1)] +pub fn solve(path: &Path) -> Result<(), Box> { + 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> { + 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(()) +} -- cgit v1.2.3