From 1b2652a70d2e4a1bb6f50b69c2b6cf63827af823 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 15 Dec 2020 10:21:03 +0100 Subject: [PATCH] Add 2020/15/1 --- 2020/inputs/day15 | 1 + 2020/src/bin/day15.rs | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 2020/inputs/day15 create mode 100644 2020/src/bin/day15.rs diff --git a/2020/inputs/day15 b/2020/inputs/day15 new file mode 100644 index 0000000..8285f9b --- /dev/null +++ b/2020/inputs/day15 @@ -0,0 +1 @@ +15,12,0,14,3,1 diff --git a/2020/src/bin/day15.rs b/2020/src/bin/day15.rs new file mode 100644 index 0000000..9034bee --- /dev/null +++ b/2020/src/bin/day15.rs @@ -0,0 +1,66 @@ +#![feature(test)] +extern crate test; +use std::collections::HashMap; + +type Parsed = Vec; + +fn read_input() -> String { + String::from("15,12,0,14,3,1") +} + +fn parse_input(raw: &str) -> Parsed { + raw.split(',').filter_map(|x| x.parse().ok()).collect() +} + +fn part1(parsed: &Parsed, limit: usize) -> usize { + let mut iter = parsed.iter(); + let mut previous = HashMap::new(); + let mut current = 0; + for i in 0..limit - 1 { + if let Some(&n) = iter.next() { + current = n; + previous.insert(current, i); + current = 0; + continue; + } + println!("Adding {} to {:?}", current, previous); + match previous.get(¤t) { + Some(&position) => { + previous.insert(current, i); + current = i.saturating_sub(position); + } + None => { + previous.insert(current, i); + current = 0; + } + } + } + current +} + +// only here so the test/bench macro works +fn part2(parsed: &Parsed, limit: usize) -> usize { + part1(parsed, limit) +} + +fn main() { + let input = parse_input(&read_input()); + println!("Part 1: {}", part1(&input, 2020)); + println!("Part 2: {}", part1(&input, 30000000)); +} + +#[cfg(test)] +mod tests { + use super::*; + use aoc2020::*; + use paste::paste; + use test::black_box; + + const TEST_INPUT: &str = "0,3,6"; + + test!(part1(2020) == 436); + test!(part2(30000000) == 175594); + //bench!(part1() == 0); + //bench!(part2() == 0); + bench_input!(len == 6); +}