mirror of
https://github.com/theoludwig/advent_of_code_2023.git
synced 2024-12-08 00:45:53 +01:00
refactor: day 1 to separate lib and main
This commit is contained in:
parent
760686db4c
commit
df6721b198
@ -9,7 +9,3 @@ end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.txt]
|
||||
trim_trailing_whitespace = unset
|
||||
insert_final_newline = unset
|
||||
|
79
day_1/src/lib.rs
Normal file
79
day_1/src/lib.rs
Normal file
@ -0,0 +1,79 @@
|
||||
pub fn part_1(input: &str) -> usize {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let characters_digits = line
|
||||
.chars()
|
||||
.filter(|&character| character.is_ascii_digit())
|
||||
.collect::<Vec<char>>();
|
||||
|
||||
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
|
||||
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
|
||||
let number = format!("{}{}", first_digit, last_digit);
|
||||
let number: usize = number.parse().expect("Should parse as a `usize`.");
|
||||
number
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn part_2(input: &str) -> usize {
|
||||
let numbers_spelled_out_with_letters = [
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
|
||||
];
|
||||
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut characters_digits: Vec<char> = vec![];
|
||||
let mut temporary = String::from("");
|
||||
for character in line.chars() {
|
||||
temporary += &character.to_string();
|
||||
|
||||
let mut temporary_spelled_number_index = None;
|
||||
for (index, spelled_number) in numbers_spelled_out_with_letters.iter().enumerate() {
|
||||
if temporary.contains(spelled_number) {
|
||||
temporary_spelled_number_index = Some(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some(temporary_spelled_number_index) = temporary_spelled_number_index {
|
||||
let number = temporary_spelled_number_index + 1;
|
||||
characters_digits.push(
|
||||
number
|
||||
.to_string()
|
||||
.chars()
|
||||
.next()
|
||||
.expect("Number should be single-character digit."),
|
||||
);
|
||||
temporary = character.to_string();
|
||||
}
|
||||
|
||||
if character.is_ascii_digit() {
|
||||
characters_digits.push(character);
|
||||
temporary = String::from("");
|
||||
}
|
||||
}
|
||||
|
||||
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
|
||||
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
|
||||
let number = format!("{}{}", first_digit, last_digit);
|
||||
let number: usize = number.parse().expect("Should parse as a `usize`.");
|
||||
number
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod day_1_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_1_example() {
|
||||
assert_eq!(part_1(include_str!("../input_example_1.txt")), 142);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_2_example() {
|
||||
assert_eq!(part_2(include_str!("../input_example_2.txt")), 281);
|
||||
}
|
||||
}
|
@ -1,67 +1,4 @@
|
||||
fn part_1(input: &str) -> i32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let characters_digits = line
|
||||
.chars()
|
||||
.filter(|&character| character.is_ascii_digit())
|
||||
.collect::<Vec<char>>();
|
||||
|
||||
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
|
||||
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
|
||||
let number = format!("{}{}", first_digit, last_digit);
|
||||
let number: i32 = number.parse().expect("Should parse as a i32.");
|
||||
number
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn part_2(input: &str) -> i32 {
|
||||
let numbers_spelled_out_with_letters = [
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
|
||||
];
|
||||
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut characters_digits: Vec<char> = vec![];
|
||||
let mut temporary = String::from("");
|
||||
for character in line.chars() {
|
||||
temporary += &character.to_string();
|
||||
|
||||
let mut temporary_spelled_number_index = None;
|
||||
for (index, spelled_number) in numbers_spelled_out_with_letters.iter().enumerate() {
|
||||
if temporary.contains(spelled_number) {
|
||||
temporary_spelled_number_index = Some(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some(temporary_spelled_number_index) = temporary_spelled_number_index {
|
||||
let number = temporary_spelled_number_index + 1;
|
||||
characters_digits.push(
|
||||
number
|
||||
.to_string()
|
||||
.chars()
|
||||
.next()
|
||||
.expect("Number should be single-character digit."),
|
||||
);
|
||||
temporary = character.to_string();
|
||||
}
|
||||
|
||||
if character.is_ascii_digit() {
|
||||
characters_digits.push(character);
|
||||
temporary = String::from("");
|
||||
}
|
||||
}
|
||||
|
||||
let first_digit = characters_digits.first().unwrap_or(&'0').to_owned();
|
||||
let last_digit = characters_digits.last().unwrap_or(&'0').to_owned();
|
||||
let number = format!("{}{}", first_digit, last_digit);
|
||||
let number: i32 = number.parse().expect("Should parse as a number.");
|
||||
number
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
use day_1::{part_1, part_2};
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
@ -69,18 +6,3 @@ fn main() {
|
||||
println!("Answer Part 1: {}", part_1(input));
|
||||
println!("Answer Part 2: {}", part_2(input));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod day_1_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_1_example() {
|
||||
assert_eq!(142, part_1(include_str!("../input_example_1.txt")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_2_example() {
|
||||
assert_eq!(281, part_2(include_str!("../input_example_2.txt")));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user