From 39d7098f362f0181debff35ef3a168f009a60d5c Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Sun, 13 Dec 2020 15:22:23 +0100 Subject: [PATCH] 2020: day13: part 1 --- aoc2020/benches/bench.rs | 2 + aoc2020/input/day13.txt | 2 + aoc2020/input/day13_provided.txt | 2 + aoc2020/src/day13.rs | 64 ++++++++++++++++++++++++++++++++ aoc2020/src/lib.rs | 1 + aoc2020/src/main.rs | 2 + 6 files changed, 73 insertions(+) create mode 100644 aoc2020/input/day13.txt create mode 100644 aoc2020/input/day13_provided.txt create mode 100644 aoc2020/src/day13.rs diff --git a/aoc2020/benches/bench.rs b/aoc2020/benches/bench.rs index ce4022b..2fe2639 100644 --- a/aoc2020/benches/bench.rs +++ b/aoc2020/benches/bench.rs @@ -12,6 +12,7 @@ use aoc2020::day09; use aoc2020::day10; use aoc2020::day11; use aoc2020::day12; +use aoc2020::day13; fn aoc2020_all(c: &mut Criterion) { c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); @@ -26,6 +27,7 @@ fn aoc2020_all(c: &mut Criterion) { c.bench_function("day10", |b| b.iter(|| day10::run().unwrap())); c.bench_function("day11", |b| b.iter(|| day11::run().unwrap())); c.bench_function("day12", |b| b.iter(|| day12::run().unwrap())); + c.bench_function("day13", |b| b.iter(|| day13::run().unwrap())); } criterion_group! { diff --git a/aoc2020/input/day13.txt b/aoc2020/input/day13.txt new file mode 100644 index 0000000..2ff076c --- /dev/null +++ b/aoc2020/input/day13.txt @@ -0,0 +1,2 @@ +1008713 +13,x,x,41,x,x,x,x,x,x,x,x,x,467,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,x,17,x,x,x,x,x,x,x,x,x,x,x,29,x,353,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,23 diff --git a/aoc2020/input/day13_provided.txt b/aoc2020/input/day13_provided.txt new file mode 100644 index 0000000..d76f619 --- /dev/null +++ b/aoc2020/input/day13_provided.txt @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 diff --git a/aoc2020/src/day13.rs b/aoc2020/src/day13.rs new file mode 100644 index 0000000..020b7c9 --- /dev/null +++ b/aoc2020/src/day13.rs @@ -0,0 +1,64 @@ +use std::fmt::Write; + +use aoc::err; + +const INPUT: &str = include_str!("../input/day13.txt"); + +pub fn run() -> aoc::Result { + let mut res = String::with_capacity(128); + + writeln!(res, "part 1: {}", part1(INPUT)?)?; + + Ok(res) +} + +fn part1(input: &str) -> aoc::Result { + let mut lines = input.lines(); + + let earliest_timestamp = lines + .next() + .ok_or_else(|| err!("input was empty"))? + .parse::() + .map_err(|e| err!("couldn't parse first line: {}", e))?; + + let bus_ids = lines + .next() + .ok_or_else(|| err!("no second line"))? + .split(',') + .filter_map(|num| { + if num == "x" { + None + } else { + Some(num.parse::().map_err(|e| err!("{}", e))) + } + }) + .collect::>>()?; + + let (bus_id, earliest_departure) = bus_ids + .iter() + .map(|id| { + let next_departure = ((earliest_timestamp / id) * id) + id; + (id, next_departure) + }) + .min_by_key(|(_, next_departure)| *next_departure) + .unwrap(); + + Ok(bus_id * (earliest_departure - earliest_timestamp)) +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED: &str = include_str!("../input/day13_provided.txt"); + + #[test] + fn part1_provided() { + assert_eq!(part1(PROVIDED).unwrap(), 295); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 3269); + } +} diff --git a/aoc2020/src/lib.rs b/aoc2020/src/lib.rs index 47112b7..ccfd34c 100644 --- a/aoc2020/src/lib.rs +++ b/aoc2020/src/lib.rs @@ -10,3 +10,4 @@ pub mod day09; pub mod day10; pub mod day11; pub mod day12; +pub mod day13; diff --git a/aoc2020/src/main.rs b/aoc2020/src/main.rs index a37c9a5..7773209 100644 --- a/aoc2020/src/main.rs +++ b/aoc2020/src/main.rs @@ -13,6 +13,7 @@ use aoc2020::day09; use aoc2020::day10; use aoc2020::day11; use aoc2020::day12; +use aoc2020::day13; fn main() -> Result<()> { let days: &[DayFunc] = &[ @@ -28,6 +29,7 @@ fn main() -> Result<()> { day10::run, day11::run, day12::run, + day13::run, ]; aoc::run(days)