From 471a3064adfa47d8d6c9161e3312f1a043179595 Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 7 Nov 2021 20:43:59 +0100 Subject: [PATCH] 2015 day 11 --- 2015/src/bin/day11.rs | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 2015/src/bin/day11.rs diff --git a/2015/src/bin/day11.rs b/2015/src/bin/day11.rs new file mode 100644 index 0000000..99b1433 --- /dev/null +++ b/2015/src/bin/day11.rs @@ -0,0 +1,75 @@ +use aoc::input; + +fn main() { + let mut pass = input::read_chars(); + + // Part 1 + get_next_pass(&mut pass); + println!("Next pass: {}", pass); + + // Part 2 + get_next_pass(&mut pass); + println!("Next pass: {}", pass); +} + +fn get_next_pass(pass: &mut String) { + loop { + unsafe { inc(pass.as_bytes_mut()) } + let pb = pass.as_bytes(); + if has_seq(pb) && !has_banned_chars(pb) && has_pairs(&pass) { + break; + } + } +} + +fn has_seq(pass: &[u8]) -> bool { + for i in 0..(pass.len() - 2) { + if pass[i] == pass[i + 1] - 1 && pass[i + 1] == pass[i + 2] - 1 { + return true; + } + } + false +} + +fn has_banned_chars(pass: &[u8]) -> bool { + for b in b"iol".iter() { + if pass.contains(b) { + return true; + } + } + false +} + +fn has_pairs(pass: &str) -> bool { + let mut num_pairs = 0; + let mut pc = ' '; + let mut skip_next = false; + for c in pass.chars() { + if skip_next { + skip_next = false; + pc = c; + continue + } + if c == pc { + num_pairs += 1; + skip_next = true; + } + pc = c; + } + num_pairs >= 2 +} + +fn inc(pass: &mut [u8]) { + let mut i = pass.len() - 1; + loop { + let lc = pass[i]; + if lc < b'z' { + let offset = if lc == b'i' || lc == b'o' || lc == b'l' { 2 } else { 1 }; + pass[i] = pass[i] + offset; + break; + } else { + pass[i] = b'a'; + i -= 1; + } + } +}