From 20a493887911b3ac7902b29ba89f123597320b0f Mon Sep 17 00:00:00 2001 From: red Date: Mon, 15 Apr 2024 19:03:12 +0200 Subject: [PATCH] Year 2018: Day 01 --- CHANGELOG.md | 5 + Cargo.toml | 6 +- NOTES_2016.md | 2 +- NOTES_2018.md | 4 + README.md | 1 + benches/year_2018.rs | 20 + inputs/year_2018/day_01_input | 1004 +++++++++++++++++++++++++++++++++ src/lib.rs | 2 + src/year_2018.rs | 28 + src/year_2018/day_01.rs | 72 +++ 10 files changed, 1142 insertions(+), 2 deletions(-) create mode 100644 NOTES_2018.md create mode 100644 benches/year_2018.rs create mode 100644 inputs/year_2018/day_01_input create mode 100644 src/year_2018.rs create mode 100644 src/year_2018/day_01.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 774de31..0489320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ Of note: - The changelog 2015.5.2 has been rewritten from each commit content. - This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs) +## [2018.1.1] +### Added +- Solved [exercice for 2018, day 01](src/year_2018/01.rs). + + ## [2017.25.1] ### Added - Solved [exercice for 2017, day 23](src/year_2017/23.rs). diff --git a/Cargo.toml b/Cargo.toml index c24a130..0ca1439 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-rs" -version = "2017.25.1" +version = "2018.1.1" edition = "2021" authors = ["Arnaud 'red' Rouyer"] readme = "README.md" @@ -68,3 +68,7 @@ harness = false # [[bench]] # name = "year_2017_day_13" # harness = false + +[[bench]] +name = "year_2018" +harness = false diff --git a/NOTES_2016.md b/NOTES_2016.md index 4f61f80..715608d 100644 --- a/NOTES_2016.md +++ b/NOTES_2016.md @@ -1,5 +1,5 @@ # Notes for solving 2016 -## Day 1: No Time for a Taxicab +## Day 01: No Time for a Taxicab Nice to get back into it! diff --git a/NOTES_2018.md b/NOTES_2018.md new file mode 100644 index 0000000..77dc713 --- /dev/null +++ b/NOTES_2018.md @@ -0,0 +1,4 @@ +# Notes for solving 2018 +## Day 01: Chronal Calibration + +Again, first day is nothing to write home about: just iterate and sum, nothing too complicated. diff --git a/README.md b/README.md index f7a1a93..cc123a1 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ I'm also adding notes that may be useful if you're (like me) discovering Rust: - [2015, complete!](NOTES_2015.md) - [2016, complete!](NOTES_2016.md) - [2017, complete!](NOTES_2017.md) +- [2018, up to day 01!](NOTES_2018.md) # Regarding style rules I'm gonna use a mix of what `cargo fmt` does, with some stuff that feels more natural to me. diff --git a/benches/year_2018.rs b/benches/year_2018.rs new file mode 100644 index 0000000..fd888d1 --- /dev/null +++ b/benches/year_2018.rs @@ -0,0 +1,20 @@ +use advent_rs::year_2018::day_01; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +fn year_2018_day_01(c: &mut Criterion) { + let input = include_str!("../inputs/year_2018/day_01_input"); + assert_eq!(day_01::day_01_v1(input), 484); + assert_eq!(day_01::day_01_v2(input), 367); + + let mut g2018_day_01 = c.benchmark_group("year_2018::day_01"); + g2018_day_01.bench_function("year_2018::day_01_v1", |b| { + b.iter(|| day_01::day_01_v1(black_box(input))) + }); + g2018_day_01.bench_function("year_2018::day_01_v2", |b| { + b.iter(|| day_01::day_01_v2(black_box(input))) + }); + g2018_day_01.finish(); +} + +criterion_group!(benches, year_2018_day_01); +criterion_main!(benches); diff --git a/inputs/year_2018/day_01_input b/inputs/year_2018/day_01_input new file mode 100644 index 0000000..c50e8a3 --- /dev/null +++ b/inputs/year_2018/day_01_input @@ -0,0 +1,1004 @@ +-19 ++8 +-10 ++9 ++2 ++19 ++5 ++8 ++13 ++16 ++4 +-8 ++16 +-10 ++11 ++13 +-12 +-5 +-15 +-12 ++9 +-6 +-18 ++5 ++3 ++18 +-3 ++5 +-19 ++21 +-9 +-9 ++20 +-18 ++11 ++11 ++17 ++17 +-20 +-7 ++5 ++7 ++7 ++7 ++7 ++16 +-12 ++18 ++10 ++3 ++15 +-10 ++8 ++5 ++7 +-16 +-16 +-2 +-15 ++3 +-6 ++9 +-15 ++19 +-12 +-20 +-1 +-12 ++11 +-22 ++10 ++15 ++2 +-10 +-19 ++1 +-18 +-21 +-4 ++16 +-15 ++13 ++20 ++7 ++23 ++13 +-3 ++10 ++8 +-7 ++4 +-19 +-7 +-4 ++19 ++20 +-18 +-5 ++11 ++22 +-14 ++15 +-3 ++16 ++20 +-16 +-11 +-12 ++7 +-13 ++7 ++12 ++20 ++9 +-15 ++1 +-14 ++11 ++16 ++13 ++9 +-16 ++18 +-8 +-6 ++17 ++7 ++15 ++7 +-15 +-15 +-14 +-16 ++15 ++17 ++19 ++6 +-16 ++13 +-12 ++20 +-18 +-11 +-8 +-14 ++18 +-11 ++6 +-16 ++14 +-10 +-6 ++1 ++16 ++4 +-6 +-8 ++27 ++18 ++10 +-3 +-2 ++11 +-10 ++11 ++7 ++19 +-1 ++11 ++14 +-16 +-10 ++15 +-9 ++2 +-1 ++13 +-4 ++6 +-9 ++10 +-2 +-10 ++17 +-14 +-10 ++15 ++17 +-12 +-15 ++18 ++8 ++13 ++3 ++17 ++14 ++1 ++15 ++18 ++7 ++15 +-2 ++9 ++3 +-1 +-18 +-15 +-11 +-10 +-1 +-14 ++13 +-18 +-2 ++5 ++5 +-7 +-5 +-12 ++4 ++19 ++15 ++9 +-17 +-10 +-5 ++13 ++15 ++7 +-10 ++12 ++1 ++9 +-18 ++14 ++20 ++2 ++5 ++7 ++6 ++13 ++4 ++12 +-9 ++17 ++10 +-5 +-9 ++18 ++4 ++5 +-16 ++1 ++16 ++1 +-5 ++19 +-5 ++14 +-15 +-3 ++7 +-15 ++14 +-7 ++16 ++13 ++1 ++17 +-4 +-9 ++12 ++15 ++10 ++5 ++8 ++31 ++22 ++12 ++16 +-17 ++9 +-21 +-13 ++22 +-1 +-2 ++17 ++14 ++34 ++18 +-15 ++10 ++14 ++11 ++4 +-9 ++13 +-12 ++7 ++3 +-15 ++6 ++5 ++21 +-18 +-20 ++7 ++18 +-11 ++8 +-9 ++11 +-19 +-2 +-2 +-20 +-13 ++16 +-14 +-1 ++4 ++20 +-10 ++6 +-18 ++15 ++41 +-1 ++25 ++21 ++15 ++1 +-14 +-1 +-11 ++17 +-9 +-5 +-1 ++21 +-2 ++9 ++3 +-13 +-16 ++1 ++13 +-4 +-18 +-8 +-20 +-1 ++2 ++1 ++4 +-8 +-46 +-29 +-16 +-5 +-36 ++35 +-144 ++3 ++2 +-7 +-1 +-11 +-20 +-22 +-12 +-18 +-2 +-18 ++8 +-18 ++3 +-1 ++7 +-18 ++7 ++19 +-12 +-19 +-19 +-14 ++15 ++15 ++6 +-17 ++12 ++11 ++24 ++16 ++4 ++12 ++4 +-18 ++3 ++19 +-10 ++12 +-4 ++19 ++3 ++2 ++5 ++19 ++5 ++22 +-21 ++1 +-21 +-19 ++13 +-12 ++10 +-12 +-13 +-2 +-11 +-16 +-7 ++10 +-21 +-3 ++4 ++22 ++18 ++11 ++1 +-10 ++15 ++5 +-9 ++2 ++1 +-24 +-1 +-2 ++6 +-26 +-15 +-16 ++9 ++25 ++14 +-7 +-27 +-24 ++16 +-30 +-30 ++26 ++12 ++3 +-32 ++3 +-10 +-4 ++8 +-19 +-7 ++15 ++13 +-3 ++16 ++7 +-21 +-23 +-12 ++14 +-149 +-77 ++19 ++6 +-80 ++11 +-16 +-19 +-65 ++6 +-67327 +-14 ++16 +-15 +-10 ++4 ++16 +-12 +-3 +-10 ++7 ++14 +-2 ++17 ++8 +-7 ++15 +-17 +-13 +-11 +-18 ++19 +-22 +-11 +-1 +-16 +-2 ++10 +-12 +-6 ++11 ++9 ++1 ++13 ++9 +-6 ++19 +-10 +-4 +-12 ++14 ++1 ++19 ++15 ++8 +-16 ++1 ++8 ++18 +-12 +-5 ++10 ++15 +-9 +-15 ++12 ++8 +-9 ++3 ++16 ++5 ++14 ++18 +-1 +-5 ++4 ++17 ++12 +-9 +-12 +-15 ++11 +-19 ++1 +-13 ++17 +-11 +-11 +-14 ++8 ++10 +-15 ++18 +-12 ++4 +-1 ++10 ++16 ++11 ++3 +-11 +-14 +-4 ++17 ++13 ++4 ++12 +-18 +-16 +-20 ++14 +-22 +-20 +-20 ++6 ++16 +-15 +-35 +-7 +-17 +-20 ++19 +-17 +-11 +-9 +-16 +-3 ++7 +-10 +-2 ++7 ++2 ++2 ++16 ++14 ++17 +-8 +-18 ++2 +-6 ++3 +-10 +-8 ++7 ++5 ++18 +-20 +-13 +-17 +-17 +-7 ++9 ++13 +-17 ++16 ++4 ++12 +-3 ++1 +-12 +-7 ++16 ++13 ++1 ++14 +-3 +-4 ++16 +-21 +-20 +-17 ++1 +-14 +-10 +-5 +-14 +-1 +-5 ++19 ++3 ++16 +-9 +-13 ++2 ++3 +-7 +-15 ++4 ++6 ++8 +-10 ++5 ++12 ++8 ++18 +-7 ++8 +-15 ++5 +-11 +-15 ++16 ++16 ++7 +-2 ++14 ++18 ++24 ++9 ++19 ++17 ++13 +-16 +-13 +-14 +-20 +-5 ++18 +-3 ++20 ++13 ++15 ++7 ++11 +-15 +-22 +-15 +-6 ++18 +-8 ++18 +-6 +-24 +-12 ++21 ++17 ++18 ++40 ++22 ++27 +-4 +-16 ++17 +-10 +-18 ++49 ++15 +-12 ++7 ++13 +-16 ++10 ++15 +-1 +-3 ++16 ++6 +-7 ++4 +-18 ++13 +-4 +-16 ++18 +-5 ++1 ++9 +-4 +-2 ++19 ++17 +-4 +-14 +-24 +-21 ++1 ++6 +-18 ++8 ++15 +-18 +-2 +-31 +-30 ++4 ++2 +-28 +-53 +-33 ++10 +-11 +-18 +-19 +-4 +-6 +-24 +-6 +-4 ++11 +-24 +-16 ++3 +-1 +-1 ++13 +-15 +-10 +-7 +-3 ++8 +-11 ++2 ++5 ++8 +-3 +-7 ++15 ++1 +-11 +-11 +-2 +-4 ++16 ++5 ++14 +-24 +-14 +-22 +-11 +-15 +-6 ++8 ++15 ++19 +-14 +-12 ++1 ++9 ++5 ++18 ++6 +-18 ++16 +-8 +-10 +-8 +-9 ++1 ++11 +-16 ++8 ++18 ++12 ++8 ++2 ++10 ++11 +-2 ++14 ++1 ++11 +-9 +-18 +-24 ++2 +-8 ++15 +-14 +-18 ++11 ++2 +-10 +-11 +-5 +-13 +-3 +-12 +-5 +-13 ++16 ++5 +-19 +-16 +-12 +-4 ++10 +-15 ++14 +-6 ++3 +-18 ++17 ++12 +-2 ++6 ++13 ++16 +-11 ++3 ++3 +-20 +-6 ++8 +-9 +-19 +-3 ++16 +-14 +-14 ++1 +-12 +-10 ++19 +-12 +-18 ++5 +-3 ++7 +-20 +-16 +-18 ++17 +-23 ++25 ++65 ++11 ++11 ++16 +-1 ++16 +-9 ++19 ++13 +-8 +-10 ++9 +-18 +-16 +-16 +-19 ++1 +-28 ++4 ++35 ++13 ++12 ++15 +-5 +-3 ++4 ++16 ++2 ++10 ++15 ++17 ++18 ++27 ++19 ++15 ++13 ++6 +-17 ++12 ++15 ++9 ++23 ++78 +-221 +-28 ++19 ++18 +-13 +-22 +-11 +-5 +-5 +-9 +-16 +-2 ++11 ++30 ++10 ++28 +-4 +-72 +-36 ++10 +-5 ++77 ++35 ++102 ++153 +-29 +-676 +-67225 +-5 ++13 ++19 ++3 ++15 ++7 +-11 +-1 +-7 ++13 ++17 ++17 ++6 +-15 +-9 ++11 +-8 +-7 ++10 ++1 ++13 ++19 +-11 +-11 ++2 ++4 +-11 +-7 ++10 ++19 ++17 ++7 ++12 ++20 +-2 ++135638 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3f92cc6..b3cefd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ mod bfs; pub mod year_2015; pub mod year_2016; pub mod year_2017; +pub mod year_2018; /// Returns the solution for a specified exercise and input. /// @@ -46,6 +47,7 @@ pub fn solve(year: u16, day: u8, part: u8, input: impl Into) -> Option year_2015::solve(day, part, input), 2016 => year_2016::solve(day, part, input), 2017 => year_2017::solve(day, part, input), + 2018 => year_2018::solve(day, part, input), _ => None, } } diff --git a/src/year_2018.rs b/src/year_2018.rs new file mode 100644 index 0000000..3224082 --- /dev/null +++ b/src/year_2018.rs @@ -0,0 +1,28 @@ +//! Year 2017 +//! +#![doc = include_str!("../NOTES_2017.md")] + +pub mod day_01; + +pub fn solve(day: u8, part: u8, input: impl Into) -> Option { + if part > 2 { + return None; + } + + match day { + 1 => Some(day_01::day_01(part, input).to_string()), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn day_01() { + let input = include_str!("../inputs/year_2018/day_01_input"); + assert_eq!(day_01::day_01_v1(input), 484); + assert_eq!(day_01::day_01_v2(input), 367); + } +} diff --git a/src/year_2018/day_01.rs b/src/year_2018/day_01.rs new file mode 100644 index 0000000..d351162 --- /dev/null +++ b/src/year_2018/day_01.rs @@ -0,0 +1,72 @@ +use std::collections::HashSet; + +pub fn day_01_v1(input: impl Into) -> i32 { + input + .into() + .lines() + .map(|line| line.parse::().unwrap()) + .sum() +} + +pub fn day_01_v2(input: impl Into) -> i32 { + let mut numset: HashSet = HashSet::new(); + input + .into() + .lines() + .map(|line| line.parse::().unwrap()) + .cycle() + .try_fold(0, |cursor, number| match numset.insert(cursor) { + true => Ok(cursor + number), + false => Err(cursor), + }) + .unwrap_err() + + // let mut numset: HashSet = HashSet::from([0]); + // let mut cursor: i32 = 0; + // input + // .into() + // .lines() + // .map(|line| line.parse::().unwrap()) + // .cycle() + // .try_for_each(|number| { + // cursor += number; + // match numset.insert(cursor) { + // true => Some(()), + // false => None, + // } + // }); + // cursor +} + +solvable!(day_01, day_01_v1, day_01_v2, i32); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn works_with_samples_v1() { + let sample_one: [(&str, i32); 4] = [ + ("+1\n-2\n+3\n+1", 3), + ("+1\n+1\n+1", 3), + ("+1\n+1\n-2", 0), + ("-1\n-2\n-3", -6), + ]; + for (sample, result) in sample_one { + assert_eq!(day_01_v1(sample), result); + } + } + #[test] + fn works_with_samples_v2() { + let sample_two: [(&str, i32); 5] = [ + ("+1\n-2\n+3\n+1", 2), + ("+1\n-1", 0), + ("+3\n+3\n+4\n-2\n-4", 10), + ("-6\n+3\n+8\n+5\n-6", 5), + ("+7\n+7\n-2\n-7\n-4", 14), + ]; + for (sample, result) in sample_two { + assert_eq!(day_01_v2(sample), result); + } + } +}