#![no_std] use common::{print, read}; fn main() { let buf = [0u8; 65000]; read(buf.as_ptr(), buf.len()); let input = str::from_utf8(&buf).unwrap(); let result_one = one(input); let result_two = two(input); print!(32, "one: {}\ntwo: {}\n", result_one, result_two); } fn one(input: &str) -> u16 { let mut zeros: u16 = 0; let mut pos: i16 = 50; for ins in input.split("\n") { // Last entry is all unused bytes. if ins.len() > 10 { continue; } let (direction, amount_s) = ins.split_at(1); let amount = amount_s.parse::().unwrap(); match direction { "R" => { pos += amount; while pos > 99 { pos -= 100; } } "L" => { pos -= amount; while pos < 0 { pos += 100; } } _ => unreachable!(), } if pos == 0 { zeros += 1; } } zeros } #[cfg(test)] mod test { use super::*; const EXAMPLE_INPUT: &str = r#"L68 L30 R48 L5 R60 L55 L1 L99 R14 L82"#; #[test] fn example() { assert_eq!(one(EXAMPLE_INPUT), 3); } }