From d002880b830389144868e875dd42f948de8cdd83 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 29 Oct 2024 14:36:36 +0100 Subject: [PATCH] y2015d11 --- y2015/src/bin/d11.rs | 14 +++++++++ y2015/src/days/d11.rs | 70 +++++++++++++++++++++++++++++++++++++++++++ y2015/src/days/mod.rs | 1 + 3 files changed, 85 insertions(+) create mode 100644 y2015/src/bin/d11.rs create mode 100644 y2015/src/days/d11.rs diff --git a/y2015/src/bin/d11.rs b/y2015/src/bin/d11.rs new file mode 100644 index 0000000..e431d6b --- /dev/null +++ b/y2015/src/bin/d11.rs @@ -0,0 +1,14 @@ +use y2015::days::d11; + +fn main() { + part1(); + part2(); +} + +fn part1() { + println!("{}", d11::process_part1("hepxcrrq")); +} + +fn part2() { + println!("{}", d11::process_part1(&d11::process_part1("hepxcrrq"))); +} diff --git a/y2015/src/days/d11.rs b/y2015/src/days/d11.rs new file mode 100644 index 0000000..1d563c8 --- /dev/null +++ b/y2015/src/days/d11.rs @@ -0,0 +1,70 @@ +pub fn process_part1(input: &str) -> String { + let mut password = input.to_string(); + loop { + password = increment(&password); + if password.contains("i") || password.contains("o") || password.contains("l") { + continue; + } + if !has_straight(&password) { + continue; + } + if num_pairs(&password) < 2 { + continue; + } + return password; + } +} + +fn has_straight(password: &str) -> bool { + for window in password.as_bytes().windows(3) { + let (a, b, c) = (window[0], window[1], window[2]); + let (inc_b, inc_c) = (increment_letter(a), increment_letter(b)); + if b == inc_b.0 && c == inc_c.0 && !(inc_b.1 || inc_c.1) { + return true; + } + } + false +} + +fn num_pairs(password: &str) -> u32 { + let mut num_pairs = 0; + let mut was_last_pair = false; + password.as_bytes().windows(2).for_each(|window| { + if window[0] == window[1] && !was_last_pair { + num_pairs += 1; + was_last_pair = true; + } else { + was_last_pair = false; + } + }); + num_pairs +} + +fn increment(password: &str) -> String { + let mut password_bytes = password.as_bytes().to_vec(); + password_bytes.reverse(); + let mut new_password_bytes = vec![]; + let mut increment_next = true; + for letter in password_bytes { + if increment_next { + let (new_letter, overflow) = increment_letter(letter); + new_password_bytes.push(new_letter); + increment_next = overflow; + } else { + new_password_bytes.push(letter); + } + } + if increment_next { + new_password_bytes.push(b'a'); + } + new_password_bytes.reverse(); + String::from_utf8(new_password_bytes).unwrap() +} + +fn increment_letter(letter: u8) -> (u8, bool) { + if letter == b'z' { + (b'a', true) + } else { + (letter + 1, false) + } +} diff --git a/y2015/src/days/mod.rs b/y2015/src/days/mod.rs index ab0515c..45f3d0b 100644 --- a/y2015/src/days/mod.rs +++ b/y2015/src/days/mod.rs @@ -1,5 +1,6 @@ pub mod d1; pub mod d10; +pub mod d11; pub mod d2; pub mod d3; pub mod d4;