Add day 1

Part 2 is ugly. Better solution later when it’s not 6AM anymore
This commit is contained in:
kageru 2021-12-01 06:19:33 +01:00
parent 1c3ae8ec96
commit 94bf221efd
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 2072 additions and 4 deletions

2000
2021/inputs/day01 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@ sed -i -e '$a\' inputs/day$today
echo '#![feature(test)]
extern crate test;
use aoc2020::common::*;
use aoc2021::common::*;
type Parsed = Vec<usize>;
@ -42,7 +42,7 @@ fn main() {
#[cfg(test)]
mod tests {
use super::*;
use aoc2020::*;
use aoc2021::*;
use paste::paste;
use test::black_box;

65
2021/src/bin/day01.rs Normal file
View File

@ -0,0 +1,65 @@
#![feature(test)]
extern crate test;
use aoc2021::common::{parse_nums, *};
use itertools::Itertools;
type Parsed = Vec<usize>;
fn read_input() -> String {
read_file(01)
}
fn parse_input(raw: &str) -> Parsed {
parse_nums(raw)
}
fn part1(parsed: &Parsed) -> usize {
parsed.iter().tuple_windows().filter(|(a, b)| a < b).count()
}
fn part2(parsed: &Parsed) -> usize {
let mut inc = 0;
let mut w = parsed.iter().tuple_windows();
let mut p = {
let (a, b, c) = w.next().unwrap();
a + b + c
};
for (a, b, c) in w {
if a + b + c > p {
inc += 1
}
p = a + b + c;
}
inc
}
fn main() {
let input = parse_input(&read_input());
println!("Part 1: {}", part1(&input));
println!("Part 2: {}", part2(&input));
}
#[cfg(test)]
mod tests {
use super::*;
use aoc2021::*;
use paste::paste;
use test::black_box;
const TEST_INPUT: &str = "199
200
208
210
200
207
240
269
260
263";
test!(part1() == 7);
test!(part2() == 5);
bench!(part1() == 1316);
bench!(part2() == 1344);
bench_input!(len == 2000);
}

View File

@ -12,5 +12,10 @@ 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()
}
#[inline]
pub fn parse_nums_comma(l: &str) -> Vec<usize> {
l.split(',').filter_map(|n| n.parse().ok()).collect()
}

View File

@ -1,7 +1,6 @@
#[macro_export]
macro_rules! bench {
($part: ident ($($param: expr),*) == $expected:expr) => {
use paste::paste;
paste! {
#[bench]
fn [<$part _bench>](b: &mut test::Bencher) {
@ -27,7 +26,6 @@ macro_rules! bench_input {
#[macro_export]
macro_rules! test {
($part: ident ($($param: expr),*) == $expected:expr) => {
use paste::paste;
paste! {
#[test]
fn [<$part _test>]() {