Add Day 6
continuous-integration/drone/push Build is passing Details

main
Julius 2021-12-06 09:12:49 +01:00
parent 4e62401f62
commit d7d1a1d840
Signed by: j00lz
GPG Key ID: AF241B0AA237BBA2
4 changed files with 88 additions and 2 deletions

1
input/day06 Normal file
View File

@ -0,0 +1 @@
3,3,5,1,1,3,4,2,3,4,3,1,1,3,3,1,5,4,4,1,4,1,1,1,3,3,2,3,3,4,2,5,1,4,1,2,2,4,2,5,1,2,2,1,1,1,1,4,5,4,3,1,4,4,4,5,1,1,4,3,4,2,1,1,1,1,5,2,1,4,2,4,2,5,5,5,3,3,5,4,5,1,1,5,5,5,2,1,3,1,1,2,2,2,2,1,1,2,1,5,1,2,1,2,5,5,2,1,1,4,2,1,4,2,1,1,1,4,2,5,1,5,1,1,3,1,4,3,1,3,2,1,3,1,4,1,2,1,5,1,2,1,4,4,1,3,1,1,1,1,1,5,2,1,5,5,5,3,3,1,2,4,3,2,2,2,2,2,4,3,4,4,4,1,2,2,3,1,1,4,1,1,1,2,1,4,2,1,2,1,1,2,1,5,1,1,3,1,4,3,2,1,1,1,5,4,1,2,5,2,2,1,1,1,1,2,3,3,2,5,1,2,1,2,3,4,3,2,1,1,2,4,3,3,1,1,2,5,1,3,3,4,2,3,1,2,1,4,3,2,2,1,1,2,1,4,2,4,1,4,1,4,4,1,4,4,5,4,1,1,1,3,1,1,1,4,3,5,1,1,1,3,4,1,1,4,3,1,4,1,1,5,1,2,2,5,5,2,1,5

View File

@ -37,7 +37,7 @@ impl Day for Day05 {
}
}
fn covered_twice(vents: &Vec<Path>) -> usize {
fn covered_twice(vents: &[Path]) -> usize {
let mut m = HashMap::new();
for vent in vents {
for cover in &vent.covers {

82
src/day06.rs Normal file
View File

@ -0,0 +1,82 @@
use std::collections::HashMap;
use crate::Day;
pub struct Day06(Vec<u8>);
impl Day for Day06 {
fn init(content: String) -> Self {
Self(content.split(',').map(|x| x.parse().expect("This is not an int!")).collect::<Vec<_>>())
}
fn part1(&self) -> String {
format!("{}", self.do_hashmap_magic(80))
}
fn part2(&self) -> String {
format!("{}", self.do_hashmap_magic(256))
}
}
impl Day06 {
fn do_hashmap_magic(&self, days: u64) -> u64 {
let x = self.0.clone();
let mut hm = HashMap::<u8, u64>::new();
for z in x {
let e = hm.entry(z).or_insert(0);
*e += 1;
}
for _ in 0..days {
let mut to_add = 0;
for num in 0..=8 {
if num == 0 {
to_add = *hm.get(&0).unwrap_or(&0);
} else {
let entry = { *hm.get(&num).unwrap_or(&0) };
let prev = hm.entry(num - 1).or_insert(0);
*prev = entry;
}
}
{
let en = hm.entry(8).or_insert(0);
*en = to_add;
}
{
let en = hm.entry(6).or_insert(0);
*en += to_add;
}
}
hm.values().sum::<u64>()
}
}
#[cfg(test)]
mod tests {
use crate::day06::Day06;
use crate::day::Day;
const INPUT: &str = r"3,4,3,1,2";
#[test]
fn part1_test() {
let d = Day06::init(INPUT.to_string());
assert_eq!("5934", d.part1());
}
#[test]
fn part2_test() {
let d = Day06::init(INPUT.to_string());
assert_eq!("26984457539", d.part2());
}
#[test]
fn part1_real() {
let d = Day06::init(crate::load_input("06"));
assert_eq!("362740", d.part1());
}
#[test]
fn part2_real() {
let d = Day06::init(crate::load_input("06"));
assert_eq!("1644874076764", d.part2());
}
}

View File

@ -6,6 +6,7 @@ use crate::day02::Day02;
use crate::day03::Day03;
use crate::day04::Day04;
use crate::day05::Day05;
use crate::day06::Day06;
use crate::day::Day;
mod day;
@ -14,6 +15,7 @@ mod day02;
mod day03;
mod day04;
mod day05;
mod day06;
fn load_input(day: &str) -> String {
read_to_string(format!("./input/day{}", day)).expect("Could not load input")
@ -52,7 +54,8 @@ fn main() {
Box::new(Day02::init(load_input("02"))),
Box::new(Day03::init(load_input("03"))),
Box::new(Day04::init(load_input("04"))),
Box::new(Day05::init(load_input("05")))];
Box::new(Day05::init(load_input("05"))),
Box::new(Day06::init(load_input("06")))];
let _verbosity = matches.occurrences_of("v");
if matches.is_present("all") {