Created
December 11, 2023 19:09
-
-
Save BlueZeeKing/d3b5bb226256ed27f62f0b2e60ebd607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
fn main() { | |
// Do a little parsing | |
let mut lines = stdin().lines(); | |
let first_line = lines.next().unwrap().unwrap(); | |
let mut first_line_parts = first_line.split(" "); | |
// Get the number of steps and trails from the input | |
let num_steps: usize = first_line_parts.next().unwrap().parse().unwrap(); | |
let num_trails: usize = first_line_parts.next().unwrap().parse().unwrap(); | |
// Parse the next line as a list of trail distances in feed | |
let trails = lines | |
.next() | |
.unwrap() | |
.unwrap() | |
.split(' ') | |
.take(num_trails) | |
.map(|val| val.parse::<usize>().unwrap() * 5280) | |
.collect::<Vec<_>>(); | |
let mut min = None; | |
// For every trail figure out the number of trails that would have to be traveled | |
'outer: for (index, trail) in trails.iter().enumerate() { | |
if num_steps < *trail { | |
// Shortcut for the case when one trail is larger that the number of steps needed. This | |
// is the guaranteed minimum and we can end processes early | |
println!("1"); | |
return; | |
} | |
// Setup some stuff up for the loop loop | |
let mut dist_left = num_steps - trail; | |
let mut idx = index + 1; | |
if idx == trails.len() { | |
// This solution does not account for wrapping so if this is the last trail skip this | |
// iteration | |
continue 'outer; | |
} | |
let mut num_trails = 1; // We have already traveled one trail | |
'inner: loop { | |
num_trails += 1; // increase our counter for the number of trails traveled | |
if dist_left <= trails[idx] { | |
// if we have traveled the desired number of steps stop | |
// processing, otherwise update the distance left to go. | |
break 'inner; | |
} else { | |
dist_left -= trails[idx]; | |
} | |
// Move to the next trail, if there are no trails, skip to the next iteration of the | |
// outer loop | |
idx += 1; | |
if idx == trails.len() { | |
continue 'outer; | |
} | |
} | |
// If this is the minimum number of trails required or there is no minimum yet, update the | |
// minimum | |
if !matches!(min, Some(min) if min < num_trails) { | |
min = Some(num_trails); | |
} | |
} | |
if let Some(val) = min { | |
println!("{}", val); | |
} else { | |
println!("IMPOSSIBLE"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment