Complete day 13
This commit is contained in:
parent
7b7e983e9a
commit
a3b9b63181
4 changed files with 262 additions and 0 deletions
day-13/src
172
day-13/src/main.rs
Normal file
172
day-13/src/main.rs
Normal file
|
@ -0,0 +1,172 @@
|
|||
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_gauss(&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(1))
|
||||
.collect();
|
||||
|
||||
Ok((estimate, ids))
|
||||
}
|
||||
|
||||
fn find_closest_bus(time: u32, ids: &Vec<u32>) -> (u32, u32) {
|
||||
ids.iter()
|
||||
.filter(|id| **id != 1)
|
||||
.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_gauss(ids: &Vec<u32>) -> u128 {
|
||||
let ids: Vec<u128> = ids.iter().map(|id| *id as u128).collect();
|
||||
let product: u128 = ids.iter().product();
|
||||
|
||||
let gauss = |mut n: u128, mut d: u128, m: u128| -> u128 {
|
||||
while d != 1 {
|
||||
if m == 1 {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let multiplier = if m > d {
|
||||
m / d + (m % d != 0) as u128
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
n = (n * multiplier) % m;
|
||||
d = (d * multiplier) % m;
|
||||
}
|
||||
|
||||
n / d
|
||||
};
|
||||
|
||||
(0..ids.len())
|
||||
.map(|a| {
|
||||
let b = product / ids[a];
|
||||
let bi = gauss(1, b, ids[a]);
|
||||
let a = (ids[a] * a as u128 - a as u128) % ids[a] as u128;
|
||||
|
||||
a * b * bi
|
||||
})
|
||||
.sum::<u128>()
|
||||
% product
|
||||
}
|
||||
|
||||
#[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_gauss(&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_gauss(&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_gauss(&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_gauss(&ids);
|
||||
|
||||
assert_eq!(timestamp, 779210);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue