[TASK] Solved day 6
This commit is contained in:
2167
input/day6.txt
Normal file
2167
input/day6.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
input/day6_example.txt
Normal file
15
input/day6_example.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
||||
58
src/day6.rs
Normal file
58
src/day6.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
// use super::util;
|
||||
use std::fs;
|
||||
|
||||
pub fn solve() {
|
||||
|
||||
let raw_input = fs::read_to_string("input/day6.txt").expect("Couldn't read file!");
|
||||
let groups = raw_input
|
||||
.split("\r\n\r\n")
|
||||
.map(|g| g.lines().map(|p| String::from(p)).collect::<Vec<_>>())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let part1: usize = groups.iter()
|
||||
.map(|g| count_answers(&g))
|
||||
.sum();
|
||||
|
||||
println!("Day 6 Part 1: {}", part1);
|
||||
|
||||
let part2: u32 = groups.iter()
|
||||
.map(|g| count_equal_answers(&g))
|
||||
.sum();
|
||||
|
||||
println!("Day 6 Part 2: {}", part2);
|
||||
}
|
||||
|
||||
fn count_answers(group: &Vec<String>) -> usize {
|
||||
|
||||
let mut answers: Vec<char> = group.join("").chars().collect::<Vec<_>>();
|
||||
answers.sort();
|
||||
answers.dedup();
|
||||
|
||||
return answers.len();
|
||||
}
|
||||
|
||||
fn count_equal_answers(group: &Vec<String>) -> u32 {
|
||||
|
||||
let mut it = group.iter();
|
||||
let mut answers: u32 = to_flags(it.next().unwrap());
|
||||
|
||||
loop {
|
||||
let cur = it.next();
|
||||
if cur.is_some() {
|
||||
answers &= to_flags(cur.unwrap())
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return answers.count_ones();
|
||||
}
|
||||
|
||||
fn to_flags(answers: &String) -> u32 {
|
||||
|
||||
let mut res = 0;
|
||||
let a = 'a' as u32;
|
||||
answers.chars().for_each(|c| res |= 1 << ((c as u32) - a));
|
||||
return res;
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
mod day6;
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -22,6 +23,7 @@ fn main() {
|
||||
3 => day3::solve(),
|
||||
4 => day4::solve(),
|
||||
5 => day5::solve(),
|
||||
6 => day6::solve(),
|
||||
_ => println!("This day is not yet implemented")
|
||||
}
|
||||
} else {
|
||||
@@ -31,6 +33,7 @@ fn main() {
|
||||
day3::solve();
|
||||
day4::solve();
|
||||
day5::solve();
|
||||
day6::solve();
|
||||
}
|
||||
|
||||
println!("Execution took {} μs", now.elapsed().as_micros());
|
||||
|
||||
Reference in New Issue
Block a user