day02 both

This commit is contained in:
Johannes
2018-12-02 09:13:46 +01:00
parent a0509342ae
commit b89e4f4929
4 changed files with 303 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
fn main() {
aoc_2018::tasks::day01::task1();
aoc_2018::tasks::day01::task2();
aoc_2018::tasks::day02::task1();
aoc_2018::tasks::day02::task2();
}

50
src/tasks/day02.rs Normal file
View File

@@ -0,0 +1,50 @@
use utils;
pub fn task1() {
let input = utils::read_file("input/day02.txt");
let mut count_two = 0;
let mut count_three = 0;
for line in input.lines() {
let mut counts = [0u8; 26];
for c in line.chars() {
counts[(c as usize - 'a' as usize)] += 1;
}
if counts.iter().any(|count| *count == 2) {
count_two += 1;
}
if counts.iter().any(|count| *count == 3) {
count_three += 1;
}
}
println!("Part 1: {}", count_two * count_three);
}
pub fn task2() {
let input = utils::read_file("input/day02.txt");
for x in input.lines() {
for y in input.lines() {
let mut diff_index = 0;
let mut diff_count = 0;
for (i, (a, b)) in x.chars().zip(y.chars()).enumerate() {
if a != b {
diff_index = i;
diff_count += 1;
}
}
if diff_count == 1 {
println!(
"Part 2: {}{}",
&x[0..diff_index],
&x[diff_index + 1..x.len()]
);
return;
}
}
}
println!("Part 2: None found!");
}

View File

@@ -1 +1,2 @@
pub mod day01;
pub mod day02;