diff --git a/CHANGELOG.md b/CHANGELOG.md index 0489320..e8633a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,14 @@ 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.2.1] +### Added +- Solved [exercice for 2018, day 02](src/year_2018/02.rs). + ## [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 0ca1439..45fda63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-rs" -version = "2018.1.1" +version = "2018.2.1" edition = "2021" authors = ["Arnaud 'red' Rouyer"] readme = "README.md" diff --git a/NOTES_2018.md b/NOTES_2018.md index 77dc713..dfc5167 100644 --- a/NOTES_2018.md +++ b/NOTES_2018.md @@ -2,3 +2,7 @@ ## Day 01: Chronal Calibration Again, first day is nothing to write home about: just iterate and sum, nothing too complicated. + +## Day 02: Inventory Management System + +Oh look, another of those "loop over EVERY-FUCKING-COMBINATION" exercises... diff --git a/benches/year_2018.rs b/benches/year_2018.rs index fd888d1..2cbaa7b 100644 --- a/benches/year_2018.rs +++ b/benches/year_2018.rs @@ -1,4 +1,5 @@ use advent_rs::year_2018::day_01; +use advent_rs::year_2018::day_02; use criterion::{black_box, criterion_group, criterion_main, Criterion}; fn year_2018_day_01(c: &mut Criterion) { @@ -16,5 +17,20 @@ fn year_2018_day_01(c: &mut Criterion) { g2018_day_01.finish(); } -criterion_group!(benches, year_2018_day_01); +fn year_2018_day_02(c: &mut Criterion) { + let input = include_str!("../inputs/year_2018/day_02_input"); + assert_eq!(day_02::day_02_v1(input), "7872"); + assert_eq!(day_02::day_02_v2(input), "tjxmoewpdkyaihvrndfluwbzc"); + + let mut g2018_day_02 = c.benchmark_group("year_2018::day_02"); + g2018_day_02.bench_function("year_2018::day_02_v1", |b| { + b.iter(|| day_02::day_02_v1(black_box(input))) + }); + g2018_day_02.bench_function("year_2018::day_02_v2", |b| { + b.iter(|| day_02::day_02_v2(black_box(input))) + }); + g2018_day_02.finish(); +} + +criterion_group!(benches, year_2018_day_01, year_2018_day_02); criterion_main!(benches); diff --git a/inputs/year_2018/day_02_input b/inputs/year_2018/day_02_input new file mode 100644 index 0000000..e7cc82f --- /dev/null +++ b/inputs/year_2018/day_02_input @@ -0,0 +1,250 @@ +tjxmoewpqkyaiqvmndgflunszc +tjxmobwpqkyaihvrndgfjubszm +tjxmzewpqkyaihvrydgflrbszc +tjxmoeypqkyvihvrndgflubsza +tjcmoewpqkytihvrndgflgbszc +tjvmoewpqkyanevrndgflubszc +tjxmoewpqkdiihirndgflubszc +tjxboewpqkyaihbrnogflubszc +ojpmoewpqkyaihvjndgflubszc +tjxyoewpqkyaiuvrndgflutszc +tjxmoewpqkyalhvrndmflebszc +tjxmoewpqzyaihhrndgflubszf +tjxmrewpqkyaihirndgfjubszc +pjxmoewpqkyaihvendgfbubszc +txxmkewpqkyjihvrndgflubszc +tjxmoewcqkyaihvrnmgflubczc +tjxmoewkqkyaghvrndgfluvszc +tjxmoewfqkhaihvrndgflubhzc +jjqmoewpqkyaihvrndzflubszc +tjxmoewmqksaihvcndgflubszc +tjrmoewpqkyaihvrvdgflubzzc +tjxxoewpqkyaiiwrndgflubszc +cjxmoawxqkyaihvrndgflubszc +tjxdoewpvkyaihvrndgflubsoc +tjxmsewpqkyaihvrndgfluzozc +tjxmoewpqkyafhvrnyeflubszc +tjxmlewpqkyawhvondgflubszc +tjxmonwpqkyaiqvrnxgflubszc +tjxmoewcqkyaihvrnjgflumszc +tjvmoewpqkyaihveadgflubszc +tjxmogfpqkyaigvrndgflubszc +tybmoewpqkyaihvrndgllubszc +tjxmoewpdkyaihvrndgfluwbzc +etxmbewpqkyaihvrndgflubszc +tjxmoeapqcynihvrndgflubszc +tbxmoewpqkyaihvrndgfdebszc +haxmoewpqyyaihvrndgflubszc +ojxmoewpqkyaihvrnegflubszr +tjxmoewpqkyaihvrndoflubarc +ljxmoewpqkykihvrndgflvbszc +tjxmovwpqkyaihvrndgfluzsyc +tvxmoewpqkyanhvrkdgflubszc +tjxmoewpqkyaihkrndgfluwwzc +zjxmoewpfkyaihvrndgfrubszc +tjxyoegpqkyaihvrndlflubszc +tjxmoewpqkyamhvrnsgflubmzc +tjmmoewpqkyaihvrndgftuwszc +tjxmoewpqbraihvrncgflubszc +tjxmeeepqkyainvrndgflubszc +tjemoegpqkyaihvredgflubszc +tjxmoewpqkyaihvdndgfzubqzc +tjxmoegrqkyaihfrndgflubszc +tjxmoewpqxyaihvrndgfluyvzc +qjxmoewpqkyaiwvrnfgflubszc +tjxwoewpqkyashkrndgflubszc +tjzmoewiqkyaihurndgflubszc +tjumuewpqkyaihvrndgflubssc +tyxooewpukyaihvrndgflubszc +tjxvoewpqkyaiivindgflubszc +ijxmoqwpqkyaihvradgflubszc +tjxmlewpqkyaihvrhdgflubwzc +tjxmkewpqkyajhqrndgflubszc +tjxmoewpqkqaiherndgflurszc +tjamoewpqkyaizvondgflubszc +tjxgogwpqkyalhvrndgflubszc +tjxmoewpqkyachvrndgflubuzq +tjxmowqpqkyaihvrnegflubszc +mjxmoewpwkyaihvrndgfkubszc +tpbmoewpqkyaihvrzdgflubszc +tjbmoewpqkyaiuvrndgflsbszc +tjxmoewpqklaghvrndgflubazc +tjxmoewpqkyrihvrndgwlpbszc +tjcmoewpqksaiyvrndgflubszc +tjxmoeapqkymihvindgflubszc +tjxmdewpqkyafhvrndgflqbszc +tjxmoewpqxyaihvrndsflubszi +tjxmoeppqkyaihvrcdgflubszd +tjxmomwpqkyainvrmdgflubszc +tjxmovwpqkyaihvrndgfdubdzc +tjxmoewwqkiaihvrjdgflubszc +tmxmoewpqkyaifvrndgflubszs +tbxmoewpqkyaihvrbdgflunszc +tjxmoewrqkyaihvxndgflubszp +ujxmoewpqkyaihvxndgflubpzc +tdxmotwpqkyaihvdndgflubszc +tjxmvewpqkyaihfrndgtlubszc +tjfmoewpqkyaihvrnyqflubszc +tjxfolwzqkyaihvrndgflubszc +ojrmoiwpqkyaihvrndgflubszc +tjsmoqwpqkyqihvrndgflubszc +tjxmohwpqkyaihvrudgflubslc +tjxtoiwpqkyaihvrnogflubszc +taxmoewpqkyaiyvrndgfwubszc +tjxwnezpqkyaihvrndgflubszc +tjxmyevpqkyaivvrndgflubszc +tjxdoeopqkyaihvgndgflubszc +tjxaoewpqkmaihvrndgflufszc +tjxmoewpqkyaxhvrndgflubncc +tjxmoewpqkyaihurndgflubbjc +tjxmjewpqgyaihvrnngflubszc +tjxmogwpqkyaihvrndgflubbcc +tjxmoewplkyaihvrnpgflibszc +tjwmoewpqkyaohvrndgfbubszc +tjwmoewpqkyaihvrndgfsubszm +tjxmogwpqkyaihvrndiflubqzc +tjxmoewpqkyaihvrndgflopshc +rjxmlewpvkyaihvrndgflubszc +tjxmogwpakyaihvrndgflzbszc +tjxmoeppqkyaihvrndgflmxszc +tjxmoewpqkyhihgrndgfzubszc +tjxqoewpqkyaihtrndgwlubszc +tjxnoespqkyaihvrndgflubsuc +tjmmoewpqkraihvrndgflfbszc +tjxmoewnqkwaihvrndgflubstc +tjxmoewpqqyaihvrndgfljbszi +tjxmoewpqkyaihkrkdgalubszc +tjxmoewpqkyaihvradgjlurszc +tvxmoewpqkybihvrndbflubszc +tjxvoewpqkyaihvradgfoubszc +tjxmoewpqfyaihvlodgflubszc +tjxmoewmnkyaiivrndgflubszc +kjxmoewpqkyaihprndgflcbszc +hjxmoewpqkcaihvrndgvlubszc +tjxmoewcqkyaihvrncgfllbszc +tuxmoewpckyaihvrndoflubszc +tjxmdewpokyaihvrndgflubszn +mjxmaewpqkyaqhvrndgflubszc +tjxmoewpmzyaihvrndgfiubszc +tjxmoewnqkyvihvrndgflubszk +tjxmoewpmnyaihvrndgftubszc +zjxmoewpqkysihvrndgfmubszc +tjxmoewpqkyaihzrntgflubbzc +tjxmoewpqkgaihwrndsflubszc +tjxjoewpqkyaihvrndgflgbizc +oqxmoewpqkyaihvrndgfldbszc +wjamoewpqkyaihvfndgflubszc +tjxmoewtmkyvihvrndgflubszc +tjlmojwpqkyaihvrndgfludszc +tjxmowwpqkyaihvrndefludszc +tjxmoewpqkbaihvrndgfluaszt +tjxmoewpqkzaahvrodgflubszc +tjxmoewpqkgaihvrndgflubtpc +tjxmoenpqkyaihcrndgfqubszc +tbxmoewpqbyaihvrndgalubszc +tjvmoewqqkyaihvrndvflubszc +tjxmoewpqkeaihvundgfaubszc +txxmoewpqkyaihvrwdgflpbszc +tzxmoewpqkijihvrndgflubszc +tjxmoewoqkytiuvrndgflubszc +tjxmrejplkyaihvrndgflubszc +tjxmoewpqkynihvrpxgflubszc +tjxmoewpqkvanhvrndgvlubszc +tjxmoewpdkyiihvrndgflubszs +tpxmyewpqkyaihvrndgfeubszc +tpxmoewpqyyaihvrndhflubszc +tjsmoewpqkyaihvrndhflubnzc +tjxmoewpukyaihvrnmgflubwzc +txxmoewpqlyaihwrndgflubszc +tjxmoewprkyaiovrndgflubxzc +tjxmouwpqkyaihzrodgflubszc +tjxmojwpqkywimvrndgflubszc +tjxsoewpqkyaihvrzdgqlubszc +tfxmoewpakyaihvrndgllubszc +tjhmoewpqiyaihvrndgflubsac +tjxmoewpqkoaihvrndoflubsxc +tjxmoewpqkyzpjvrndgflubszc +tjxmoewpqkyaiharndgzlnbszc +tjimoevpqkyaihvrndgflubbzc +tjxsoewpqkyahhvrndgfzubszc +txxmoewpqkyaimvrrdgflubszc +tjxmoewpwkyaihvrndpylubszc +tjxmoewpskyaghvrndgfbubszc +tjxmuewpqmyaihvrndgfyubszc +tjxmoewpqkyaihvdndgglubsxc +xjxmoewpqkyjiovrndgflubszc +gjxmoewpqkyaihvrndodlubszc +tjbmoewpqkyaihvridgflvbszc +tjxmozwpqkyapbvrndgflubszc +tjxeoewpqkyqihvrndgflubhzc +tjxdoewpqzyaihvrndgflubsmc +tjxmwewpqkyathvcndgflubszc +tjxmoewpszyaihvrndgflusszc +tuxmoewpqkyaihvrndgfluasxc +tjemoewpnvyaihvrndgflubszc +tjxmoewpjkyaihvrndgjlufszc +tjomoewppkyaihvzndgflubszc +tjxmvewpqkyaimvrntgflubszc +rjxmoewpqkyaihvpndgflubszq +hjxzoewpqkyaihvridgflubszc +texmoejpqkyaihvrndgflubszx +tjxcoewpqkyaihbrxdgflubszc +tjxmoewpnkyaihvrndgfltbsze +tjxmoewpdkyaihvrndwfluwbzc +tjxmoewpqryjihkrndgflubszc +tjlmoewpqkhaihvrndgflubsnc +tjxmovapqkjaihvrndgflubszc +tjxvoewpqkyaihqrndgfluyszc +tjxwoewnqkyaihvrndgfgubszc +tjdmoewpqklaihvcndgflubszc +tjxmoewpvkynihvrndgflubskc +tjxmtewpqkyaihvhndgfluaszc +tjxmoewpqkyanhvrnpgfluvszc +tjxmoewpqkyaifvbndgflubspc +tjxmoexpqknaihvrndgxlubszc +qjxmoewqqkyaihvrndgflubpzc +tjxmoewppkyaihvaxdgflubszc +myxmoewpqkyaihvrudgflubszc +tjxmwewpmkyaihvrndgflubssc +tjxmoewpqkyaihvrndgfxqbszq +tjxmoewhqkyaahvrndgflubbzc +tbxmoewmqkyaihvrndgflubszu +toxmolwpqkyaihvrndnflubszc +tjxmoewhqkyaihvrnrgflubvzc +yjxmoewcqkyaihvrndgflubfzc +tjxmoewpqkyamhvrgdgflmbszc +tjxmtewpqkyaizvrndgfluoszc +tjxmoewpqzyaihvrntsflubszc +fjxmoewpqkyaihyrmdgflubszc +tjxwoewpqcyaihbrndgflubszc +tjxmoebpqkzaihvrndcflubszc +tjxmoewpqkyaihvrndnlmubszc +tjxmoewpqkyaihvrndgeyubskc +tfxmoewpqryaihvrndgfluiszc +tjxmoewpqkjaihvynngflubszc +tjxmoewpqkqaihvonjgflubszc +tjfmokwpqkyeihvrndgflubszc +djxmoewpkkyaihvrnmgflubszc +tjxmiewpqkyaihvrndgflubhlc +tjxmmejpqkyaihvrnhgflubszc +djxmoewmqkyaihvrnggflubszc +tjxmoewpqkyaihvrkggflubsze +gjxmoewpqkyaihjrndgflvbszc +tjxmoewpqkyaidvrndgkzubszc +tjxmoewpqkyaedvrnpgflubszc +sjxmoewpqkyaihvrnngfluhszc +tjxmoewpqkuaihvrndghlubxzc +tjxmoewgqkyuihvrndgftubszc +tjxmoewpqsyaifvrkdgflubszc +tjxrrewpqkyaihvrnpgflubszc +tjxmoezpqkyaihvrwdgflabszc +tjfmoewpqknaihvrndgflubkzc +tjxmoewnqkxaihvrndgflubtzc +tjxmoewpkkyaihvrndgfrnbszc +tjxmorwpnkqaihvrndgflubszc +tsxmoewwqkyathvrndgflubszc +tjxmoeupqkyaihvrndyflubszp +bjxmoewdqkyaihvrndgflurszc +tjxmoewpvkyaihvrndoflubszq +sjxmoewpqkyaihvrndgflubyec +tjxmoewpqkyaizcrndgfnubszc \ No newline at end of file diff --git a/src/year_2018.rs b/src/year_2018.rs index 3224082..e50bcfc 100644 --- a/src/year_2018.rs +++ b/src/year_2018.rs @@ -1,8 +1,9 @@ -//! Year 2017 +//! Year 2018 //! -#![doc = include_str!("../NOTES_2017.md")] +#![doc = include_str!("../NOTES_2018.md")] pub mod day_01; +pub mod day_02; pub fn solve(day: u8, part: u8, input: impl Into) -> Option { if part > 2 { @@ -11,6 +12,7 @@ pub fn solve(day: u8, part: u8, input: impl Into) -> Option { match day { 1 => Some(day_01::day_01(part, input).to_string()), + 2 => Some(day_02::day_02(part, input).to_string()), _ => None, } } @@ -25,4 +27,11 @@ mod tests { assert_eq!(day_01::day_01_v1(input), 484); assert_eq!(day_01::day_01_v2(input), 367); } + + #[test] + fn day_02() { + let input = include_str!("../inputs/year_2018/day_02_input"); + assert_eq!(day_02::day_02_v1(input), "7872"); + assert_eq!(day_02::day_02_v2(input), "tjxmoewpdkyaihvrndfluwbzc"); + } } diff --git a/src/year_2018/day_02.rs b/src/year_2018/day_02.rs new file mode 100644 index 0000000..7975760 --- /dev/null +++ b/src/year_2018/day_02.rs @@ -0,0 +1,90 @@ +use itertools::Itertools; + +pub fn day_02_v1(input: impl Into) -> String { + let chk = input + .into() + .lines() + .map(|input| { + let mut two = false; + let mut tre = false; + for (_chr, cnt) in &input.bytes().sorted().group_by(|chr| *chr) { + match cnt.count() { + 2 => two = true, + 3 => tre = true, + _ => (), + } + } + (two, tre) + }) + .fold((0, 0), |mut acc, elt| { + if elt.0 { + acc.0 += 1; + } + if elt.1 { + acc.1 += 1; + } + acc + }); + format!("{}", chk.0 * chk.1) +} + +fn common_characters(lhs: &str, rhs: &str) -> Option { + let mut one_diff = false; + lhs + .chars() + .zip(rhs.chars()) + .try_fold("".to_string(), |mut acc, (lhc, rhc)| { + if lhc != rhc && one_diff { + None + } else if lhc != rhc { + one_diff = true; + Some(acc) + } else { + acc.push(lhc); + Some(acc) + } + }) +} + +pub fn day_02_v2(input: impl Into) -> String { + let input = input.into(); + let input: Vec<_> = input.lines().collect(); + for i in 0..input.len() { + for j in i + 1..input.len() { + if let Some(common) = common_characters(input[i], input[j]) { + return common; + } + } + } + "".to_string() +} +solvable!(day_02, day_02_v1, day_02_v2, String); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn works_with_samples_v1() { + let sample_one = "abcdef\n\ + bababc\n\ + abbcde\n\ + abcccd\n\ + aabcdd\n\ + abcdee\n\ + ababab"; + assert_eq!(day_02_v1(sample_one), "12"); + } + + #[test] + fn works_with_samples_v2() { + let sample_two = "abcde\n\ + fghij\n\ + klmno\n\ + pqrst\n\ + fguij\n\ + axcye\n\ + wvxyz"; + assert_eq!(day_02_v2(sample_two), "fgij"); + } +}