From d0fbdcd1e2d3b6d9244009bef02f0869019b5c57 Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Fri, 1 Dec 2023 13:17:46 +0100 Subject: [PATCH] Solve day01 - part 1, error on part 2 --- days/.gitignore | 14 +++++++++ days/01/part2/Cargo.lock | 7 +++++ days/01/part2/Cargo.toml | 8 +++++ days/01/part2/src/main.rs | 57 +++++++++++++++++++++++++++++++++++ days/01/trebuchet/Cargo.lock | 7 +++++ days/01/trebuchet/Cargo.toml | 8 +++++ days/01/trebuchet/src/main.rs | 33 ++++++++++++++++++++ 7 files changed, 134 insertions(+) create mode 100755 days/.gitignore create mode 100644 days/01/part2/Cargo.lock create mode 100644 days/01/part2/Cargo.toml create mode 100644 days/01/part2/src/main.rs create mode 100644 days/01/trebuchet/Cargo.lock create mode 100644 days/01/trebuchet/Cargo.toml create mode 100644 days/01/trebuchet/src/main.rs diff --git a/days/.gitignore b/days/.gitignore new file mode 100755 index 0000000..6985cf1 --- /dev/null +++ b/days/.gitignore @@ -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 diff --git a/days/01/part2/Cargo.lock b/days/01/part2/Cargo.lock new file mode 100644 index 0000000..d292d19 --- /dev/null +++ b/days/01/part2/Cargo.lock @@ -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" diff --git a/days/01/part2/Cargo.toml b/days/01/part2/Cargo.toml new file mode 100644 index 0000000..e73cff3 --- /dev/null +++ b/days/01/part2/Cargo.toml @@ -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] diff --git a/days/01/part2/src/main.rs b/days/01/part2/src/main.rs new file mode 100644 index 0000000..1f654a7 --- /dev/null +++ b/days/01/part2/src/main.rs @@ -0,0 +1,57 @@ +use std::fs; + +// Extract all numbers, digits and letter written digits +fn extract_numbers(line: &str) -> Vec { + let written_digits: Vec<&str> = vec![ + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + ]; + let mut numbers: Vec = 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 = 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); +} diff --git a/days/01/trebuchet/Cargo.lock b/days/01/trebuchet/Cargo.lock new file mode 100644 index 0000000..fca7758 --- /dev/null +++ b/days/01/trebuchet/Cargo.lock @@ -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" diff --git a/days/01/trebuchet/Cargo.toml b/days/01/trebuchet/Cargo.toml new file mode 100644 index 0000000..5d49fb2 --- /dev/null +++ b/days/01/trebuchet/Cargo.toml @@ -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] diff --git a/days/01/trebuchet/src/main.rs b/days/01/trebuchet/src/main.rs new file mode 100644 index 0000000..cf66ae9 --- /dev/null +++ b/days/01/trebuchet/src/main.rs @@ -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); +}