81 lines
3.0 KiB
Rust
81 lines
3.0 KiB
Rust
|
use std::cmp;
|
||
|
use std::collections::HashMap;
|
||
|
use std::fs;
|
||
|
|
||
|
// Extract all numbers, digits and letter written digits
|
||
|
fn extract_numbers(line: &str) -> Vec<u32> {
|
||
|
let written_digits: Vec<&str> = vec![
|
||
|
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
|
||
|
];
|
||
|
let mut max_tail_overlaps: HashMap<&str, u32> = HashMap::new();
|
||
|
// Compute the size of the suffix of number i overlaping with prefix of number j:
|
||
|
for suffix_number in &written_digits {
|
||
|
max_tail_overlaps.insert(suffix_number.clone(), 0);
|
||
|
for prefix_number in &written_digits {
|
||
|
if suffix_number != prefix_number {
|
||
|
let mut overlap: u32 = 0;
|
||
|
for i in 0..(cmp::min(prefix_number.len(), suffix_number.len())) {
|
||
|
if suffix_number.chars().nth_back(i) == prefix_number.chars().nth(i) {
|
||
|
overlap += 1;
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if overlap > max_tail_overlaps[suffix_number] {
|
||
|
max_tail_overlaps.insert(suffix_number, overlap);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
let mut numbers: Vec<u32> = Vec::new();
|
||
|
let mut current_digit_word: String = "".to_owned();
|
||
|
for character in line.chars() {
|
||
|
if character.is_digit(10) {
|
||
|
numbers.push(character.to_digit(10).unwrap());
|
||
|
current_digit_word = "".to_owned(); // Reset writing word
|
||
|
} else {
|
||
|
current_digit_word.push(character);
|
||
|
// Check for the presence of any of the digit words
|
||
|
for (index, digit_writing) in written_digits.iter().enumerate() {
|
||
|
let word_length = digit_writing.len();
|
||
|
let positions: Vec<(usize, &str)> =
|
||
|
current_digit_word.match_indices(digit_writing).collect();
|
||
|
if positions.len() > 0 {
|
||
|
let digit = index as u32 + 1;
|
||
|
numbers.push(digit);
|
||
|
let start_position = positions[0].0;
|
||
|
let end_position =
|
||
|
start_position + word_length - max_tail_overlaps[digit_writing] as usize;
|
||
|
let current_digit_word_slice = ¤t_digit_word[end_position..];
|
||
|
current_digit_word = current_digit_word_slice.to_owned();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
numbers
|
||
|
}
|
||
|
|
||
|
// Extract number including letters
|
||
|
fn extract_number(line: &str) -> u32 {
|
||
|
let numbers: Vec<u32> = extract_numbers(line);
|
||
|
let first: u32 = numbers[0];
|
||
|
let last: u32 = numbers[numbers.len() - 1];
|
||
|
let number: u32 = first * 10 + last;
|
||
|
number
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let input_filename: &str = "../data/input.txt";
|
||
|
|
||
|
let contents =
|
||
|
fs::read_to_string(input_filename).expect("Should have been able to read the file");
|
||
|
|
||
|
let mut sum: u32 = 0;
|
||
|
for line in contents.lines() {
|
||
|
let number = extract_number(line);
|
||
|
println!("{}\t{:?}", line, number);
|
||
|
sum += number;
|
||
|
}
|
||
|
println!("{:?}", sum);
|
||
|
}
|