2020-12-13 15:22:23 +01:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
use anyhow::{Context, Result};
|
2020-12-13 15:22:23 +01:00
|
|
|
|
|
|
|
const INPUT: &str = include_str!("../input/day13.txt");
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
pub fn run() -> Result<String> {
|
2020-12-13 15:22:23 +01:00
|
|
|
let mut res = String::with_capacity(128);
|
|
|
|
|
|
|
|
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
2020-12-13 17:28:29 +01:00
|
|
|
writeln!(res, "part 2: {}", part2(INPUT)?)?;
|
2020-12-13 15:22:23 +01:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
fn part1(input: &str) -> Result<u64> {
|
2020-12-13 15:22:23 +01:00
|
|
|
let mut lines = input.lines();
|
|
|
|
|
|
|
|
let earliest_timestamp = lines
|
|
|
|
.next()
|
2020-12-14 18:08:16 +01:00
|
|
|
.context("input was empty")?
|
2020-12-13 15:22:23 +01:00
|
|
|
.parse::<u64>()
|
2020-12-14 18:08:16 +01:00
|
|
|
.context("couldn't parse first line")?;
|
2020-12-13 15:22:23 +01:00
|
|
|
|
|
|
|
let bus_ids = lines
|
|
|
|
.next()
|
2020-12-14 18:08:16 +01:00
|
|
|
.context("no second line")?
|
2020-12-13 15:22:23 +01:00
|
|
|
.split(',')
|
|
|
|
.filter_map(|num| {
|
|
|
|
if num == "x" {
|
|
|
|
None
|
|
|
|
} else {
|
2020-12-14 18:08:16 +01:00
|
|
|
Some(num.parse::<u64>().map_err(anyhow::Error::new))
|
2020-12-13 15:22:23 +01:00
|
|
|
}
|
|
|
|
})
|
2020-12-14 18:08:16 +01:00
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2020-12-13 15:22:23 +01:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
fn part2(input: &str) -> Result<u64> {
|
2020-12-13 17:28:29 +01:00
|
|
|
let mut lines = input.lines();
|
|
|
|
|
|
|
|
// we don't need the first line anymore, skip it
|
2020-12-14 18:08:16 +01:00
|
|
|
lines.next().context("input was empty")?;
|
2020-12-13 17:28:29 +01:00
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
find_timestamp(lines.next().context("no second line")?)
|
2020-12-13 17:47:58 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
fn find_timestamp(input: &str) -> Result<u64> {
|
2020-12-13 17:47:58 +01:00
|
|
|
let bus_ids: Vec<(u64, u64)> = input
|
2020-12-13 17:28:29 +01:00
|
|
|
.split(',')
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(idx, num)| {
|
|
|
|
if num == "x" {
|
|
|
|
None
|
|
|
|
} else {
|
2020-12-14 18:08:16 +01:00
|
|
|
Some((idx as u64, num.parse::<u64>().map_err(anyhow::Error::new)))
|
2020-12-13 17:28:29 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|(idx, res)| match res {
|
|
|
|
Ok(num) => Ok((idx, num)),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
})
|
2020-12-14 18:08:16 +01:00
|
|
|
.collect::<Result<_>>()?;
|
2020-12-13 17:28:29 +01:00
|
|
|
|
|
|
|
// previous constraints is empty for now
|
|
|
|
let mut current_solution = 0;
|
|
|
|
let mut step = 1;
|
|
|
|
|
|
|
|
for constraint in bus_ids {
|
|
|
|
while !satisfies_constraint(current_solution, constraint) {
|
|
|
|
current_solution += step;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (_, divisor) = constraint;
|
|
|
|
|
|
|
|
step *= divisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(current_solution)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn satisfies_constraint(solution: u64, (remainder, divisor): (u64, u64)) -> bool {
|
|
|
|
((solution + remainder) % divisor) == 0
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:22:23 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-12-13 17:47:58 +01:00
|
|
|
const PROVIDED: &str = include_str!("../input/day13_provided.txt");
|
|
|
|
|
2020-12-13 15:22:23 +01:00
|
|
|
#[test]
|
|
|
|
fn part1_provided() {
|
|
|
|
assert_eq!(part1(PROVIDED).unwrap(), 295);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part1_real() {
|
|
|
|
assert_eq!(part1(INPUT).unwrap(), 3269);
|
|
|
|
}
|
2020-12-13 17:28:29 +01:00
|
|
|
|
2020-12-13 17:47:58 +01:00
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 17:28:29 +01:00
|
|
|
#[test]
|
|
|
|
fn part2_provided() {
|
|
|
|
assert_eq!(part2(PROVIDED).unwrap(), 1068781);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_real() {
|
|
|
|
assert_eq!(part2(INPUT).unwrap(), 672754131923874);
|
|
|
|
}
|
2020-12-13 15:22:23 +01:00
|
|
|
}
|