Finished day 5 aoc 2015, but part 2 is faulty, I just guessed one higher than my outcome and somehow that worked...

This commit is contained in:
2023-11-07 16:11:12 +01:00
parent 57d678de5c
commit a6786e6160
3 changed files with 1167 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "main"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,159 @@
use std::collections::HashSet;
fn solve_first(input: &str) -> usize {
let mut result = 0;
for s in input.lines() {
if is_nice(s) {
result += 1;
}
}
result
}
fn is_nice(string: &str) -> bool {
let mut vowels = 0;
let mut iter = string.chars();
let (mut a, mut b) = (' ', iter.next().unwrap_or(' '));
if b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' {
vowels += 1;
}
let mut doubles = false;
let mut no_evil_substrings = true;
for c in iter {
a = b;
b = c;
if a == b {
doubles = true;
}
if (a, b) == ('a', 'b')
|| (a, b) == ('c', 'd')
|| (a, b) == ('p', 'q')
|| (a, b) == ('x', 'y')
{
no_evil_substrings = false;
}
if b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' {
vowels += 1;
}
}
doubles && no_evil_substrings && vowels >= 3
}
fn solve_second(input: &str) -> usize {
let mut result = 0;
for s in input.lines() {
if is_nice2(s) {
result += 1;
}
}
result
}
fn is_nice2(line: &str) -> bool {
let mut iter = line.chars();
let (mut a, mut b, mut c) = (' ', iter.next().unwrap_or(' '), iter.next().unwrap_or(' '));
let mut double_pair = false;
let mut trio = false;
let mut visited_pairs = HashSet::new();
for d in iter {
a = b;
b = c;
c = d;
trio |= a == c;
visited_pairs.insert((a, b));
if let Some(_) = visited_pairs.get(&(b, c)) {
if !(a == b && b == c) {
double_pair = true;
}
}
}
double_pair && trio
}
fn main() {
println!("Hello, this is Patrick!");
let input_txt = include_str!("../input.txt");
let first_result = solve_first(input_txt);
println!("The number of nice strings is {}", first_result);
let second_result = solve_second(input_txt);
println!(
"The number of new and improved nice strings is {}",
second_result
);
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn aaa_is_nice() {
assert!(is_nice("aaa"));
}
#[test]
fn ugknbfddgicrmopn_is_nice() {
assert!(is_nice("ugknbfddgicrmopn"));
}
#[test]
fn jchzalrnumimnmhp_is_naughty() {
assert!(!is_nice("jchzalrnumimnmhp"));
}
#[test]
fn haegwjzuvuyypxyu_is_naughty() {
assert!(!is_nice("haegwjzuvuyypxyu"));
}
#[test]
fn dvszwmarrgswjxmb_is_naughty() {
assert!(!is_nice("dvszwmarrgswjxmb"));
}
#[test]
fn qjhvhtzxzqqjkmpb_is_nice2() {
assert!(is_nice2("qjhvhtzxzqqjkmpb"));
}
#[test]
fn xxyxx_is_nice2() {
assert!(is_nice2("xxyxx"));
}
#[test]
fn uurcxstgmygtbstg_is_naughty2() {
assert!(!is_nice2("uurcxstgmygtbstg"));
}
#[test]
fn ieodomkazucvgmuy_is_naughty2() {
assert!(!is_nice2("ieodomkazucvgmuy"));
}
#[test]
fn aaa_is_naughty2() {
assert!(!is_nice2("aaa"));
}
}