No way I could figure out part 2 without googling. I was playing around with least common divisor + gcd + clever looping. Finally googling around I found the chinese remainder theorem.
rust rust rust :)
usestd::usize::MAX;usestd::collections::HashMap;#[aoc(day13,part1)]fnbus_wait_time(input:&(usize,HashMap<usize,usize>))->usize{lettime=input.0;letbusses=&input.1;letmutmin_time=MAX;letmutbest_bus_id=0;for(&i,bus)inbusses.iter(){letr=time%*bus;letwaiting=*bus+time-r;ifwaiting<min_time{min_time=waiting;best_bus_id=i;}}busses[&best_bus_id]*(min_time-time)}#[aoc(day13,part2)]fnmagic_timestamp(input:&(usize,HashMap<usize,usize>))->usize{letmutbuses:Vec<(usize,usize)>=input.1.iter().map(|(&pos,&id)|(pos,id)).collect();buses.sort_by(|a,b|a.0.partial_cmp(&b.0).unwrap());letmuttimestamp=0;letmutinc=buses[0].1;for&(i,bus)in&buses[1..]{// friggin CRT sieve garbage see: https://en.wikipedia.org/wiki/Chinese_remainder_theorem#Computationwhile(timestamp+i)%bus!=0{timestamp+=inc;}// adjust for the next moduloinc*=bus;}timestamp}
No way I could figure out part 2 without googling. I was playing around with least common divisor + gcd + clever looping. Finally googling around I found the chinese remainder theorem.
rust rust rust :)
Man, this turned out really concise. Also, thank you for letting me shamelessly steal your logic for part 2 :D