#[path = "../lib.rs"] mod lib; fn get_digit(line: &str, offset: usize, allow_spelled: bool) -> Option { let l = &line[offset..]; if l.starts_with("1") || (allow_spelled && l.starts_with("one")) { return Some(1); } else if l.starts_with("2") || (allow_spelled && l.starts_with("two")) { return Some(2); } else if l.starts_with("3") || (allow_spelled && l.starts_with("three")) { return Some(3); } else if l.starts_with("4") || (allow_spelled && l.starts_with("four")) { return Some(4); } else if l.starts_with("5") || (allow_spelled && l.starts_with("five")) { return Some(5); } else if l.starts_with("6") || (allow_spelled && l.starts_with("six")) { return Some(6); } else if l.starts_with("7") || (allow_spelled && l.starts_with("seven")) { return Some(7); } else if l.starts_with("8") || (allow_spelled && l.starts_with("eight")) { return Some(8); } else if l.starts_with("9") || (allow_spelled && l.starts_with("nine")) { return Some(9); } else { return None; } } fn get_number(line: &str, allow_spelled: bool) -> u32 { let mut a = 0; let mut b = 0; for i in 0..line.len() { match get_digit(line, i, allow_spelled) { Some(x) => { a = x; break }, None => {}, } } for i in (0..line.len()).rev() { match get_digit(line, i, allow_spelled) { Some(x) => { b = x; break }, None => {}, } } return a * 10 + b; } fn main() { let mut sum1 = 0; let mut sum2 = 0; for line in lib::iter_input() { sum1 += get_number(&line, false); sum2 += get_number(&line, true); } println!("part1: {}", sum1); println!("part2: {}", sum2); }