1
use std::collections::VecDeque;10
pub fn racecar(target: i32) -> i32 {11
let mut frontier = VecDeque::new();12
frontier.push_back(State {17
while let Some(State { speed, pos, moves }) = frontier.pop_front() {21
// Always allow accelerating.22
frontier.push_back(State {27
if allow_reverse(target, pos, speed) {28
frontier.push_back(State {29
speed: -1 * speed.signum(),39
pub fn allow_reverse(target: i32, position: i32, speed: i32) -> bool {40
// If speed + pos would overshoot target in one direction, allow reverse.41
let next_to_target = target - (speed + position);42
// If we're before the target and moving forwards, or after the target43
// and moving backwards, then we're moving in the correct direction and44
// shouldn't allow reverse--this corresponds to a positive sign of46
speed * next_to_target < 0