2020: day13: refacto / more tests

This commit is contained in:
Antoine Martin 2020-12-13 17:47:58 +01:00
parent 4c928e06ac
commit d457461d2f

View file

@ -3,7 +3,6 @@ use std::fmt::Write;
use aoc::err; use aoc::err;
const INPUT: &str = include_str!("../input/day13.txt"); const INPUT: &str = include_str!("../input/day13.txt");
const PROVIDED: &str = include_str!("../input/day13_provided.txt");
pub fn run() -> aoc::Result<String> { pub fn run() -> aoc::Result<String> {
let mut res = String::with_capacity(128); let mut res = String::with_capacity(128);
@ -54,9 +53,11 @@ fn part2(input: &str) -> aoc::Result<u64> {
// we don't need the first line anymore, skip it // we don't need the first line anymore, skip it
lines.next().ok_or_else(|| err!("input was empty"))?; lines.next().ok_or_else(|| err!("input was empty"))?;
let bus_ids: Vec<(u64, u64)> = lines find_timestamp(lines.next().ok_or_else(|| err!("no second line"))?)
.next() }
.ok_or_else(|| err!("no second line"))?
fn find_timestamp(input: &str) -> aoc::Result<u64> {
let bus_ids: Vec<(u64, u64)> = input
.split(',') .split(',')
.enumerate() .enumerate()
.filter_map(|(idx, num)| { .filter_map(|(idx, num)| {
@ -97,6 +98,8 @@ fn satisfies_constraint(solution: u64, (remainder, divisor): (u64, u64)) -> bool
mod tests { mod tests {
use super::*; use super::*;
const PROVIDED: &str = include_str!("../input/day13_provided.txt");
#[test] #[test]
fn part1_provided() { fn part1_provided() {
assert_eq!(part1(PROVIDED).unwrap(), 295); assert_eq!(part1(PROVIDED).unwrap(), 295);
@ -107,6 +110,21 @@ mod tests {
assert_eq!(part1(INPUT).unwrap(), 3269); assert_eq!(part1(INPUT).unwrap(), 3269);
} }
#[test]
fn part2_small_samples() {
let tests = &[
("17,x,13,19", 3417),
("67,7,59,61", 754018),
("67,x,7,59,61", 779210),
("67,7,x,59,61", 1261476),
("1789,37,47,1889", 1202161486),
];
for &(input, expected) in tests {
assert_eq!(find_timestamp(input).unwrap(), expected);
}
}
#[test] #[test]
fn part2_provided() { fn part2_provided() {
assert_eq!(part2(PROVIDED).unwrap(), 1068781); assert_eq!(part2(PROVIDED).unwrap(), 1068781);