This commit is contained in:
Fabian Schmidt 2024-10-28 13:29:30 +01:00
parent 8370d27bea
commit 6c6b50dee0
4 changed files with 1126 additions and 0 deletions

1000
y2015/resources/5_input.txt Normal file

File diff suppressed because it is too large Load Diff

20
y2015/src/bin/d5.rs Normal file
View File

@ -0,0 +1,20 @@
use std::fs;
use y2015::days::d5;
fn main() {
part1();
part2();
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/5_input.txt")).unwrap();
println!("{}", d5::process_part1(&content));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/5_input.txt")).unwrap();
println!("{}", d5::process_part2(&content));
}

105
y2015/src/days/d5.rs Normal file
View File

@ -0,0 +1,105 @@
use std::collections::HashMap;
const DISALLOWED: [(u8, u8); 4] = [(b'a', b'b'), (b'c', b'd'), (b'p', b'q'), (b'x', b'y')];
const VOWELS: [u8; 5] = [b'a', b'e', b'i', b'o', b'u'];
pub fn process_part1(input: &str) -> i32 {
input
.lines()
.map(|line| line.as_bytes())
.map(|line| {
let mut num_vowels = 0;
let mut has_double = false;
if VOWELS.contains(&line[0]) {
num_vowels += 1;
}
for window in line.windows(2) {
if DISALLOWED.contains(&(window[0], window[1])) {
return 0;
}
if window[0] == window[1] {
has_double = true;
}
if VOWELS.contains(&window[1]) {
num_vowels += 1;
}
}
if num_vowels >= 3 && has_double {
return 1;
}
0
})
.sum()
}
// used the following solution to debug
// cat input | grep "\(..\).*\1" | grep "\(.\).\1" | wc -l
pub fn process_part2(input: &str) -> i32 {
input
.lines()
.map(|line| line.as_bytes())
.map(|line| {
let mut pairs = HashMap::new();
let mut has_sandwich = false;
for window in line.windows(3) {
if window[0] == window[2] {
has_sandwich = true;
break;
}
}
if !has_sandwich {
return 0;
}
let mut last_pair = "".to_string();
let mut is_valid = true;
for window in line.windows(2) {
let pair = String::from_utf8(window.to_vec()).unwrap();
if pair == last_pair && is_valid {
is_valid = false;
continue;
}
is_valid = true;
pairs
.entry(pair.clone())
.and_modify(|counter| *counter += 1)
.or_insert(1);
last_pair = pair;
}
for (_, count) in pairs.into_iter() {
if count >= 2 {
println!("{}", String::from_utf8(line.to_vec()).unwrap());
return 1;
}
}
0
})
.sum()
}
#[cfg(test)]
mod tests_5 {
use super::*;
const INPUT1: &str = "ugknbfddgicrmopn
aaa
jchzalrnumimnmhp
haegwjzuvuyypxyu
dvszwmarrgswjxmb";
#[test]
fn it_works() {
let result = process_part1(INPUT1);
assert_eq!(result, 2);
}
const INPUT2: &str = "qjhvhtzxzqqjkmpb
xxyxx
uurcxstgmygtbstg
ieodomkazucvgmuy";
#[test]
fn part2() {
let result = process_part2(INPUT2);
assert_eq!(result, 2);
}
}

View File

@ -2,3 +2,4 @@ pub mod d1;
pub mod d2;
pub mod d3;
pub mod d4;
pub mod d5;