use std::fs::read_to_string; fn main() { let (password, _) = read_to_string("../input.txt") .expect("failed to open input file") .lines() .fold((0u32, 50), |(zero_count, position), rotation| { let (direction, distance) = rotation.split_at(1); let distance = distance.parse::().expect("failed to parse distance"); let modulo_distance = distance % 100; let (new_position, overflow) = match direction { "L" => { if modulo_distance > position { (100 - (modulo_distance - position), position != 0) } else { (position - modulo_distance, position == modulo_distance) } } "R" => { let turned = position + modulo_distance; (turned % 100, turned >= 100) } other => panic!("unexpected direction {other}"), }; let new_count = zero_count + (u32::from(distance) / 100) + if overflow { 1 } else { 0 }; (new_count, new_position) }); println!("The door password is {password}!"); }