Add day 6 part 1

This commit is contained in:
kageru 2021-12-06 11:01:23 +01:00
parent 78de4f1d46
commit 0256625608
3 changed files with 61 additions and 2 deletions

1
2021/inputs/day06 Normal file
View File

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

54
2021/src/bin/day06.rs Normal file
View File

@ -0,0 +1,54 @@
#![feature(test)]
extern crate test;
use aoc2021::common::*;
use std::iter;
const DAY: usize = 6;
const DAYS_PER_CHILD: usize = 6;
const DAYS_UNTIL_FERTILE: usize = 2;
type Parsed = Vec<usize>;
fn parse_input(raw: &str) -> Parsed {
parse_nums_comma(raw)
}
fn part1(parsed: &Parsed, generations: usize) -> usize {
iter::successors(Some(parsed.to_owned()), |fish| {
let fish = fish
.into_iter()
.flat_map(|n| match n {
0 => vec![DAYS_PER_CHILD, DAYS_PER_CHILD + DAYS_UNTIL_FERTILE],
&n => vec![n - 1],
})
.collect();
Some(fish)
})
.take(generations + 1)
.last()
.unwrap()
.len()
}
fn part2(parsed: &Parsed) -> usize {
unimplemented!()
}
fn main() {
let input = parse_input(&read_file(DAY));
println!("Part 1: {}", part1(&input, 80));
println!("Part 2: {}", part2(&input));
}
#[cfg(test)]
mod tests {
use super::*;
use aoc2021::*;
const TEST_INPUT: &str = "3,4,3,1,2";
test!(part1(80) == 5934);
test!(part2() == 0);
bench!(part1(80) == 364461);
bench!(part2() == 0);
bench_input!(Vec::len => 0);
}

View File

@ -6,10 +6,14 @@ pub fn read_file(day: usize) -> String {
#[inline]
pub fn parse_nums(l: &str) -> Vec<usize> {
l.lines().filter_map(|n| n.parse().ok()).collect()
l.lines().map(parse_num).collect()
}
#[inline]
pub fn parse_nums_comma(l: &str) -> Vec<usize> {
l.split(',').filter_map(|n| n.parse().ok()).collect()
l.split(',').map(parse_num).collect()
}
fn parse_num(s: &str) -> usize {
s.trim().parse().unwrap_or_else(|e| panic!("Invalid number {s}: {e:?}"))
}