Naive solution
This commit is contained in:
parent
7b7e983e9a
commit
45ab053ef4
4 changed files with 271 additions and 0 deletions
day-13/src
181
day-13/src/main.rs
Normal file
181
day-13/src/main.rs
Normal file
|
@ -0,0 +1,181 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
let (time, ids) = parse_notes(&input)?;
|
||||
|
||||
// Part 1
|
||||
let (earliest, arrival) = find_closest_bus(time, &ids);
|
||||
println!("{}", (arrival - time) * earliest);
|
||||
|
||||
// Part 2
|
||||
let timestamp = find_incrementing(&ids);
|
||||
println!("{}", timestamp);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_notes(input: &str) -> Result<(u32, Vec<u32>), &str> {
|
||||
let mut lines = input.lines();
|
||||
|
||||
let estimate = lines
|
||||
.next()
|
||||
.ok_or("Input too short")?
|
||||
.parse()
|
||||
.map_err(|_| "Estimate is not a number")?;
|
||||
|
||||
let ids = lines
|
||||
.next()
|
||||
.ok_or("Input too short")?
|
||||
.split(',')
|
||||
.map(|id| id.parse().unwrap_or(0))
|
||||
.collect();
|
||||
|
||||
Ok((estimate, ids))
|
||||
}
|
||||
|
||||
fn find_closest_bus(time: u32, ids: &Vec<u32>) -> (u32, u32) {
|
||||
ids.iter()
|
||||
.filter(|id| **id != 0)
|
||||
.map(|id| {
|
||||
let mut arrival = *id;
|
||||
|
||||
while arrival < time {
|
||||
arrival += id
|
||||
}
|
||||
|
||||
(*id, arrival)
|
||||
})
|
||||
.min_by_key(|id| id.1)
|
||||
.expect("Must have at least one bus ID")
|
||||
}
|
||||
|
||||
fn find_incrementing(ids: &Vec<u32>) -> u128 {
|
||||
let ids: Vec<u128> = ids.iter().map(|id| *id as u128).collect();
|
||||
let (largest, increment) = ids
|
||||
.iter()
|
||||
.enumerate()
|
||||
.max_by_key(|id| id.1)
|
||||
.expect("Must have at least one bus ID");
|
||||
|
||||
let mut iterations = (14166999999493 - largest as u128) / increment;
|
||||
|
||||
loop {
|
||||
iterations += 1;
|
||||
let max = increment * iterations;
|
||||
|
||||
if ids.iter().enumerate().rev().all(|(i, number)| {
|
||||
if *number == 0 {
|
||||
true
|
||||
} else {
|
||||
let target = max - largest as u128 + i as u128;
|
||||
target % number == 0
|
||||
}
|
||||
}) {
|
||||
break max - largest as u128;
|
||||
} else {
|
||||
if iterations % 100000000 == 0 {
|
||||
let fun = ids
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, number)| {
|
||||
if **number == 0 {
|
||||
true
|
||||
} else {
|
||||
let target = max - largest as u128 + *i as u128;
|
||||
target % **number == 0
|
||||
}
|
||||
})
|
||||
.count();
|
||||
|
||||
println!("{} - {}/{}", max - largest as u128, fun, ids.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"939
|
||||
7,13,x,x,59,x,31,19
|
||||
"
|
||||
);
|
||||
|
||||
let (time, ids) = parse_notes(input)?;
|
||||
let (earliest, arrival) = find_closest_bus(time, &ids);
|
||||
|
||||
assert_eq!(arrival, 944);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"939
|
||||
17,x,13,19
|
||||
"
|
||||
);
|
||||
|
||||
let (time, ids) = parse_notes(input)?;
|
||||
let timestamp = find_incrementing(&ids);
|
||||
|
||||
assert_eq!(timestamp, 3417);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple3() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"939
|
||||
7,13,x,x,59,x,31,19
|
||||
"
|
||||
);
|
||||
|
||||
let (time, ids) = parse_notes(input)?;
|
||||
let timestamp = find_incrementing(&ids);
|
||||
|
||||
assert_eq!(timestamp, 1068781);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple4() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"939
|
||||
67,7,59,61
|
||||
"
|
||||
);
|
||||
|
||||
let (time, ids) = parse_notes(input)?;
|
||||
let timestamp = find_incrementing(&ids);
|
||||
|
||||
assert_eq!(timestamp, 754018);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple5() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"939
|
||||
67,x,7,59,61
|
||||
"
|
||||
);
|
||||
|
||||
let (time, ids) = parse_notes(input)?;
|
||||
let timestamp = find_incrementing(&ids);
|
||||
|
||||
assert_eq!(timestamp, 779210);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue