Solve day01 - part 1, error on part 2
This commit is contained in:
commit
d0fbdcd1e2
|
@ -0,0 +1,14 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "part2"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "part2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,57 @@
|
||||||
|
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 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 start_position = positions[0].0;
|
||||||
|
let end_position = start_position + word_length;
|
||||||
|
let current_digit_word_slice = ¤t_digit_word[end_position..];
|
||||||
|
current_digit_word = current_digit_word_slice.to_owned();
|
||||||
|
let digit = index as u32 + 1;
|
||||||
|
numbers.push(digit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "trebuchet"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "trebuchet"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,33 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn extract_number(line: &str) -> u32 {
|
||||||
|
let mut has_digit: bool = false;
|
||||||
|
let mut first: u32 = 0;
|
||||||
|
let mut last: u32 = 0;
|
||||||
|
for character in line.chars() {
|
||||||
|
if character.is_digit(10) {
|
||||||
|
if !has_digit {
|
||||||
|
has_digit = true;
|
||||||
|
first = character.to_digit(10).unwrap();
|
||||||
|
}
|
||||||
|
last = character.to_digit(10).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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!("{:?}", number);
|
||||||
|
sum += number;
|
||||||
|
}
|
||||||
|
println!("{:?}", sum);
|
||||||
|
}
|
Loading…
Reference in New Issue