diff --git a/day13/Cargo.toml b/day13/Cargo.toml new file mode 100644 index 0000000..f0447b8 --- /dev/null +++ b/day13/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day13" +version = "0.1.0" +authors = ["henine "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day13/input b/day13/input new file mode 100644 index 0000000..91abf0b --- /dev/null +++ b/day13/input @@ -0,0 +1,2 @@ +1000104 +41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,659,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,x,x,19,x,x,x,x,x,x,x,x,x,29,x,937,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17 diff --git a/day13/src/main.rs b/day13/src/main.rs new file mode 100644 index 0000000..086816b --- /dev/null +++ b/day13/src/main.rs @@ -0,0 +1,78 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() { + let file = File::open("input").unwrap(); + + let input_buffer = BufReader::new(&file); + let mut lines = input_buffer.lines(); + + let time: i64 = lines.next().unwrap().unwrap().parse().unwrap(); + let (mut buses, mut counts, _): (Vec, Vec, i64) = lines.next().unwrap().unwrap() + .split(',') + .map(|num| match num.parse() { + Ok(num) => Some(num), + Err(_e) => None + }) + .fold((Vec::new(), Vec::new(), 0), + |(mut ids, mut counts, count), id| match id { + Some(id) => { + ids.push(id); + counts.push(count); + (ids, counts, count + 1) + } + None => (ids, counts, count + 1) + }); + + println!("{}", time); + println!("{:?}", buses); + println!("{:?}", counts); + + let mut i = 0; + loop { + if i == buses.len() - 1 { break; } + if buses[i + 1] > buses[i] { + buses.swap(i, i + 1); + counts.swap(i, i + 1); + if i > 0 { i -= 1 } + } else { + i += 1 + } + } + + println!("{:?}", buses); + println!("{:?}", counts); + + let mut xi = (buses[0] - counts[0]).rem_euclid(buses[0]); + + let mut prod_n = buses[0]; + + for i in 1..buses.len() { + let ai = (buses[i] - counts[i]).rem_euclid( buses[i]) ; + let ni = buses[i]; + + + loop { + if xi % ni == ai { break; } + xi += prod_n; + } + + prod_n *= ni; + } + + println!("{}", xi); + + // let mut min_time = *buses.iter().max().unwrap(); + // let mut min_bus= 0; + // for bus_id in buses.iter() { + // + // let wait_time = bus_id-time%bus_id; + // + // if wait_time < min_time { + // min_time = wait_time; + // min_bus = *bus_id; + // } + // } + // + // println!("{}", min_bus*min_time); +} diff --git a/day13/test b/day13/test new file mode 100644 index 0000000..e473080 --- /dev/null +++ b/day13/test @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 \ No newline at end of file