Added day 13
This commit is contained in:
parent
7afe9cb6ba
commit
8c24b10fa4
|
@ -0,0 +1,2 @@
|
||||||
|
1000655
|
||||||
|
17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,571,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,13,x,x,x,x,23,x,x,x,x,x,29,x,401,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,19
|
|
@ -1,5 +1,2 @@
|
||||||
F10
|
939
|
||||||
N3
|
7,13,x,x,59,x,31,19
|
||||||
F7
|
|
||||||
R90
|
|
||||||
F11
|
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let file = File::open("inputs/day13.txt")?;
|
||||||
|
let mut lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
|
||||||
|
|
||||||
|
let depature_time: u32 = lines.nth(0).unwrap().parse().unwrap();
|
||||||
|
let buses: Vec<u32> = lines
|
||||||
|
.nth(0)
|
||||||
|
.unwrap()
|
||||||
|
.split(',')
|
||||||
|
.filter_map(|b| b.parse().ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let times: Vec<(u32, u32)> = buses
|
||||||
|
.iter()
|
||||||
|
.map(|b| (*b, b - (depature_time % b)))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let best_time = times
|
||||||
|
.iter()
|
||||||
|
.fold(times[0], |a, b| if a.1 < b.1 { a } else { *b });
|
||||||
|
|
||||||
|
println!("Line: {}, Wait Time: {}", best_time.0, best_time.1);
|
||||||
|
println!("Answer: {}", best_time.0 * best_time.1);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
fn next_depature(t: u64, b: u64) -> u64 {
|
||||||
|
if t % b == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
b - (t % b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_rests(t: u64, buses: &Vec<Option<u64>>) {
|
||||||
|
for i in 0..buses.len() {
|
||||||
|
if let Some(bus) = buses[i] {
|
||||||
|
println!("{} : {} {}", bus, i, t % bus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let file = File::open("inputs/day13.txt")?;
|
||||||
|
let mut lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
|
||||||
|
|
||||||
|
let buses: Vec<Option<u64>> = lines
|
||||||
|
.nth(1)
|
||||||
|
.unwrap()
|
||||||
|
.split(',')
|
||||||
|
.map(|b| b.parse().ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut step = buses[0].unwrap();
|
||||||
|
let mut t: u64 = 0;
|
||||||
|
for i in 1..buses.len() {
|
||||||
|
if let Some(bus) = buses[i] {
|
||||||
|
loop {
|
||||||
|
t = t + step;
|
||||||
|
if (t + i as u64) % bus == 0 {
|
||||||
|
step = step * bus;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", t);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue