y2016d7
This commit is contained in:
parent
93ce6d0719
commit
2299375c7c
2000
y2016/resources/7_input.txt
Normal file
2000
y2016/resources/7_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
27
y2016/src/bin/d7.rs
Normal file
27
y2016/src/bin/d7.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use std::{fs, time::Instant};
|
||||
|
||||
use utils::time::get_elapsed_string;
|
||||
use y2016::days::d7;
|
||||
|
||||
fn main() {
|
||||
let now = Instant::now();
|
||||
println!("Part 1:");
|
||||
part1();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
let now = Instant::now();
|
||||
println!("Part 2:");
|
||||
part2();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/7_input.txt")).unwrap();
|
||||
println!("{}", d7::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/7_input.txt")).unwrap();
|
||||
println!("{}", d7::process_part2(&content));
|
||||
}
|
163
y2016/src/days/d7.rs
Normal file
163
y2016/src/days/d7.rs
Normal file
@ -0,0 +1,163 @@
|
||||
pub fn process_part1(input: &str) -> u32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| if is_tls(line) { 1 } else { 0 })
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> u32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| if is_ssl(line) { 1 } else { 0 })
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn is_tls(ipv7: &str) -> bool {
|
||||
let mut abba = false;
|
||||
let mut part = String::new();
|
||||
for chara in ipv7.chars() {
|
||||
if chara == '[' {
|
||||
if contains_abba(&part) {
|
||||
abba = true;
|
||||
}
|
||||
part.clear();
|
||||
} else if chara == ']' {
|
||||
if contains_abba(&part) {
|
||||
return false;
|
||||
}
|
||||
part.clear();
|
||||
} else {
|
||||
part.push(chara);
|
||||
}
|
||||
}
|
||||
if contains_abba(&part) {
|
||||
abba = true;
|
||||
}
|
||||
abba
|
||||
}
|
||||
|
||||
fn contains_abba(part: &str) -> bool {
|
||||
if part.len() < 4 {
|
||||
return false;
|
||||
}
|
||||
for window in part.chars().collect::<Vec<char>>().windows(4) {
|
||||
if window[0] == window[3] && window[1] == window[2] && window[0] != window[1] {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn is_ssl(ipv7: &str) -> bool {
|
||||
let mut abas = Vec::new();
|
||||
let mut babs = Vec::new();
|
||||
let mut part = String::new();
|
||||
for chara in ipv7.chars() {
|
||||
if chara == '[' {
|
||||
if let Some(additional_aba) = get_aba_or_bab(&part) {
|
||||
abas.extend_from_slice(&additional_aba);
|
||||
}
|
||||
part.clear();
|
||||
} else if chara == ']' {
|
||||
if let Some(additional_bab) = get_aba_or_bab(&part) {
|
||||
babs.extend_from_slice(&additional_bab);
|
||||
}
|
||||
part.clear();
|
||||
} else {
|
||||
part.push(chara);
|
||||
}
|
||||
}
|
||||
if let Some(additional_aba) = get_aba_or_bab(&part) {
|
||||
abas.extend_from_slice(&additional_aba);
|
||||
}
|
||||
for aba in abas {
|
||||
for bab in babs.iter() {
|
||||
let aba = aba.chars().collect::<Vec<char>>();
|
||||
let bab = bab.chars().collect::<Vec<char>>();
|
||||
if aba[0] == bab[1] && aba[1] == bab[0] {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn get_aba_or_bab(part: &str) -> Option<Vec<String>> {
|
||||
if part.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let mut aba_bab: Vec<String> = Vec::new();
|
||||
for window in part.chars().collect::<Vec<char>>().windows(3) {
|
||||
if window[0] == window[2] {
|
||||
aba_bab.push(window.iter().collect());
|
||||
}
|
||||
}
|
||||
if aba_bab.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(aba_bab)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT_1: &str = "abba[mnop]qrst";
|
||||
const INPUT_2: &str = "abcd[bddb]xyyx";
|
||||
const INPUT_3: &str = "aaaa[qwer]tyui";
|
||||
const INPUT_4: &str = "ioxxoj[asdfgh]zxcvbn";
|
||||
|
||||
#[test]
|
||||
fn part1_1() {
|
||||
let result = is_tls(INPUT_1);
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_2() {
|
||||
let result = is_tls(INPUT_2);
|
||||
assert!(!result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_3() {
|
||||
let result = is_tls(INPUT_3);
|
||||
assert!(!result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_4() {
|
||||
let result = is_tls(INPUT_4);
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
const INPUT_5: &str = "aba[bab]xyz";
|
||||
const INPUT_6: &str = "xyx[xyx]xyx";
|
||||
const INPUT_7: &str = "aaa[kek]eke";
|
||||
const INPUT_8: &str = "zazbz[bzb]cdb";
|
||||
|
||||
#[test]
|
||||
fn part2_1() {
|
||||
let result = is_ssl(INPUT_5);
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_2() {
|
||||
let result = is_ssl(INPUT_6);
|
||||
assert!(!result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_3() {
|
||||
let result = is_ssl(INPUT_7);
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_4() {
|
||||
let result = is_ssl(INPUT_8);
|
||||
assert!(result);
|
||||
}
|
||||
}
|
@ -9,3 +9,5 @@ pub mod d4;
|
||||
pub mod d5;
|
||||
|
||||
pub mod d6;
|
||||
|
||||
pub mod d7;
|
||||
|
Loading…
Reference in New Issue
Block a user