1
0
mirror of https://gitlab.com/MisterBiggs/aoc_2015-rust.git synced 2025-06-15 22:46:43 +00:00

Day 5 complete

This commit is contained in:
Anson 2022-11-05 20:26:42 -06:00
parent 15763843fe
commit 78c999f90b
5 changed files with 1164 additions and 1 deletions

16
Cargo.lock generated
View File

@ -6,9 +6,25 @@ version = 3
name = "AoC2015"
version = "0.1.0"
dependencies = [
"itertools",
"md5",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "md5"
version = "0.7.0"

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
md5 = "0.7.0"
md5 = "0.7.0"
itertools = "0.10.5"

1000
inputs/day5.txt Normal file

File diff suppressed because it is too large Load Diff

144
src/day5.rs Normal file
View File

@ -0,0 +1,144 @@
use std::fs;
use itertools::Itertools;
pub fn run() {
println!("Day 5:");
let input = fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string();
// dbg!(input.)
println!("\tPart 1: {}", part1(&input));
println!("\tPart 2: {}", part2(&input));
}
fn part1(input: &String) -> usize {
let mut nice = 0;
for txt in input.split_whitespace() {
let mut blacklist_count = 0;
let blacklist = ["ab", "cd", "pq", "xy"];
for black in blacklist {
if txt.contains(black) {
blacklist_count += 1;
}
}
if blacklist_count != 0 {
continue;
}
let mut repeat_count = 0;
for (l, r) in txt.chars().tuple_windows() {
if l == r {
repeat_count += 1;
}
}
if repeat_count == 0 {
continue;
}
let vowels = "aeiou";
let mut vowel_count = 0;
for c in txt.chars() {
if vowels.contains(c) {
vowel_count += 1;
}
}
if vowel_count < 3 {
continue;
}
nice += 1;
}
return nice;
}
fn part2(input: &String) -> usize {
let mut nice = 0;
for txt in input.split_whitespace() {
let mut double = false;
for (l, r) in txt
.chars()
.tuple_windows::<(_, _)>()
.enumerate()
.tuple_combinations()
{
if l.1 == r.1 {
if l.0.abs_diff(r.0) > 1 {
double = true;
}
}
}
if !double {
continue;
}
let mut double_split = false;
for (l, _, r) in txt.chars().tuple_windows::<(_, _, _)>() {
if l == r {
double_split = true;
}
}
if double_split {
nice += 1;
}
}
return nice;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1_examples() {
assert_eq!(part1(&"ugknbfddgicrmopn".to_string()), 1);
assert_eq!(
part1(
&"ugknbfddgicrmopn\naaa\njchzalrnumimnmhp\nhaegwjzuvuyypxyu\ndvszwmarrgswjxmb"
.to_string()
),
2
);
}
#[test]
fn test_1_input() {
assert_eq!(
part1(
&fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string()
),
238
);
}
#[test]
fn test_2_examples() {
assert_eq!(part2(&"qjhvhtzxzqqjkmpb".to_string()), 1);
assert_eq!(part2(&"xxyxx".to_string()), 1);
assert_eq!(part2(&"aaa".to_string()), 0);
assert_eq!(part2(&"aaaaa".to_string()), 1);
assert_eq!(
part2(&"qjhvhtzxzqqjkmpb\nxxyxx\nuurcxstgmygtbstg\nieodomkazucvgmuy".to_string()),
2
);
}
#[test]
fn test_2_input() {
assert_eq!(
part2(
&fs::read_to_string("./inputs/day5.txt")
.expect("Could not read file")
.to_string()
),
69
);
}
}

View File

@ -2,6 +2,7 @@ mod day1;
mod day2;
mod day3;
mod day4;
mod day5;
fn main() {
println!("Running Advent of Code 2021");
@ -9,4 +10,5 @@ fn main() {
day2::run();
day3::run();
day4::run();
day5::run();
}