mirror of
https://gitlab.com/MisterBiggs/aoc_2015-rust.git
synced 2025-08-01 11:11:24 +00:00
Day 5 complete
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@@ -6,9 +6,25 @@ version = 3
|
|||||||
name = "AoC2015"
|
name = "AoC2015"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
"md5",
|
"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]]
|
[[package]]
|
||||||
name = "md5"
|
name = "md5"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
|
@@ -6,4 +6,5 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
md5 = "0.7.0"
|
md5 = "0.7.0"
|
||||||
|
itertools = "0.10.5"
|
1000
inputs/day5.txt
Normal file
1000
inputs/day5.txt
Normal file
File diff suppressed because it is too large
Load Diff
144
src/day5.rs
Normal file
144
src/day5.rs
Normal 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,6 +2,7 @@ mod day1;
|
|||||||
mod day2;
|
mod day2;
|
||||||
mod day3;
|
mod day3;
|
||||||
mod day4;
|
mod day4;
|
||||||
|
mod day5;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Running Advent of Code 2021");
|
println!("Running Advent of Code 2021");
|
||||||
@@ -9,4 +10,5 @@ fn main() {
|
|||||||
day2::run();
|
day2::run();
|
||||||
day3::run();
|
day3::run();
|
||||||
day4::run();
|
day4::run();
|
||||||
|
day5::run();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user