From 8f6f0b46129c651b089de8bf25ad54f449abfe8a Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Wed, 28 Oct 2015 13:43:48 +0100 Subject: [PATCH 1/2] Express the main loop through an iterator --- csa.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/csa.rs b/csa.rs index 064be62..8757cbc 100644 --- a/csa.rs +++ b/csa.rs @@ -25,21 +25,19 @@ impl Connection { } fn csa_main_loop(timetable: &[Connection], arrival_station: usize, earliest_arrival: &mut [u32], in_connection: &mut [usize]) { - let mut earliest = std::u32::MAX; - - for (i, connection) in timetable.iter().enumerate() { + timetable.iter().enumerate().fold(std::u32::MAX, |earliest, (i, connection)| { if connection.departure_timestamp >= earliest_arrival[connection.departure_station] && - connection.arrival_timestamp < earliest_arrival[connection.arrival_station] { + connection.arrival_timestamp < earliest_arrival[connection.arrival_station] { earliest_arrival[connection.arrival_station] = connection.arrival_timestamp; in_connection[connection.arrival_station] = i; + } - if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest { - earliest = connection.arrival_timestamp; - } - } else if connection.arrival_timestamp > earliest { - break; + if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest { + connection.arrival_timestamp + } else { + earliest } - } + }); } fn csa_print_result(timetable: &Vec, in_connection: &[usize], arrival_station: usize) { From 0e2e81a24a692705d82a20ba10f9329b8e36a2b7 Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Wed, 28 Oct 2015 14:12:50 +0100 Subject: [PATCH 2/2] Change Vec to slice throughout --- csa.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/csa.rs b/csa.rs index 8757cbc..1abf09a 100644 --- a/csa.rs +++ b/csa.rs @@ -40,7 +40,7 @@ fn csa_main_loop(timetable: &[Connection], arrival_station: usize, earliest_arri }); } -fn csa_print_result(timetable: &Vec, in_connection: &[usize], arrival_station: usize) { +fn csa_print_result(timetable: &[Connection], in_connection: &[usize], arrival_station: usize) { if in_connection[arrival_station] == std::u32::MAX as usize { println!("NO_SOLUTION"); } else { @@ -60,7 +60,7 @@ fn csa_print_result(timetable: &Vec, in_connection: &[usize], arriva println!(""); } -fn csa_compute(timetable: &Vec, departure_station: usize, arrival_station: usize, departure_time: u32) +fn csa_compute(timetable: &[Connection], departure_station: usize, arrival_station: usize, departure_time: u32) { let mut in_connection = vec!(std::u32::MAX as usize; MAX_STATIONS); let mut earliest_arrival = vec!(std::u32::MAX; MAX_STATIONS); @@ -81,7 +81,7 @@ fn main() { let timetable = buffered_in.map(|r| { r.ok().expect("failed to read connection line") }) .take_while(|l| { !l.is_empty() }) .map(|l| { Connection::parse(l.trim_right()) }) - .collect(); + .collect::>(); // Responding to requests from stdin @@ -98,6 +98,6 @@ fn main() { let arrival_station = params[1] as usize; let departure_time = params[2]; - csa_compute(&timetable, departure_station, arrival_station, departure_time); + csa_compute(&timetable[..], departure_station, arrival_station, departure_time); }).collect::>(); }