solved day 5

This commit is contained in:
timeshifter
2026-06-06 13:48:16 +02:00
parent 2676c0679e
commit 07ac33c05a
2 changed files with 60 additions and 13 deletions
+13 -9
View File
@@ -1,9 +1,13 @@
ugknbfddgicrmopn #ugknbfddgicrmopn
aaa #aaa
jchzalrnumimnmhp #jchzalrnumimnmhp
haegwjzuvuyypxyu #haegwjzuvuyypxyu
dvszwmarrgswjxmb #dvszwmarrgswjxmb
qjhvhtzxzqqjkmpb
xxyxx #qjhvhtzxzqqjkmpb
uurcxstgmygtbstg #xxyxx
ieodomkazucvgmuy #uurcxstgmygtbstg
#ieodomkazucvgmuy
#aaa
xyxy
#aabcdefgaa
+47 -4
View File
@@ -14,14 +14,18 @@ fn main() {
} }
fn part_1_and_2<T: Fn(&&str) -> bool>(data: &str, f: T) { fn part_1_and_2<T: Fn(&&str) -> bool>(data: &str, f: T) {
let sum = data.lines().filter(f).count(); let sum = data
.lines()
.filter(|line| !line.starts_with('#'))
.filter(f)
.count();
println!("{sum}"); println!("{sum}");
} }
fn is_nice_1(s: &&str) -> bool { fn is_nice_1(s: &&str) -> bool {
let vowel_count = s.chars().filter(|c| VOWELS.contains(*c)).count(); let vowel_count = s.chars().filter(|c| VOWELS.contains(*c)).count();
let double_letter = s.chars().zip(s.chars().skip(1)).any(|(a, b)| a == b); let has_double_letter = s.chars().zip(s.chars().skip(1)).any(|(a, b)| a == b);
let has_forbidden_tuple = s let has_forbidden_tuple = s
.chars() .chars()
@@ -29,14 +33,53 @@ fn is_nice_1(s: &&str) -> bool {
.map(|(a, b)| format!("{a}{b}")) .map(|(a, b)| format!("{a}{b}"))
.any(|tuple| FORBIDDEN.contains(&tuple.as_str())); .any(|tuple| FORBIDDEN.contains(&tuple.as_str()));
vowel_count >= 3 && double_letter && !has_forbidden_tuple vowel_count >= 3 && has_double_letter && !has_forbidden_tuple
} }
fn is_nice_2(s: &&str) -> bool { fn is_nice_2(s: &&str) -> bool {
let mut subrule_1 = false; let mut subrule_1 = false;
let mut subrule_2 = false;
let mut digraphs = extract_digraphs(s);
digraphs.sort_unstable();
let double_digraphs = digraphs
.iter()
.zip(digraphs.iter().skip(1))
.filter(|(a, b)| a == b)
.map(|(a, _)| a.clone());
for dd in double_digraphs {
// can't use the other one again because we
// need it unsorted here
let digraphs = extract_digraphs(s);
let mut iter = digraphs.into_iter();
let idx1 = iter.position(|x| x == *dd).unwrap();
let idx2 = iter.position(|x| x == *dd).unwrap() + idx1 + 1;
if idx2.abs_diff(idx1) >= 2 {
subrule_1 = true;
break;
}
}
let repeated_letter = s
.chars()
.zip(s.chars().skip(2))
.find(|(a, b)| a == b)
.map(|(a, _)| a);
let subrule_2 = repeated_letter.is_some();
subrule_1 && subrule_2 subrule_1 && subrule_2
} }
fn extract_digraphs(s: &&str) -> Vec<String> {
s.chars()
.zip(s.chars().skip(1))
.map(|(a, b)| format!("{a}{b}"))
.collect()
}
// https://adventofcode.com/2015/day/5 // https://adventofcode.com/2015/day/5