wip part2
This commit is contained in:
parent
b57ebdc97f
commit
513448bbbd
53
2023/day-01/src/part2.rs
Normal file
53
2023/day-01/src/part2.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use regex::Regex;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("./input.txt");
|
||||
let output = process(input);
|
||||
println!("{}", output);
|
||||
}
|
||||
|
||||
fn process(input: &str) -> i32 {
|
||||
let lines: i32 = input
|
||||
.lines()
|
||||
.into_iter()
|
||||
.map(|x| replacer(x))
|
||||
.map(|line| {
|
||||
println!("{line}");
|
||||
let line_vec = line
|
||||
.chars()
|
||||
.filter(|c| c.is_digit(10))
|
||||
.collect::<Vec<char>>();
|
||||
let first = line_vec.clone().into_iter().nth(0);
|
||||
let last = line_vec.clone().into_iter().nth_back(0);
|
||||
[first.unwrap(), last.unwrap()].into_iter().collect()
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.iter()
|
||||
.map(|x| x.parse::<i32>().unwrap())
|
||||
.sum();
|
||||
lines
|
||||
}
|
||||
|
||||
fn replacer(x: &str) -> String {
|
||||
let re = Regex::new("(one|two|three|four|five|six|seven|eight|nine)");
|
||||
|
||||
x.replace("one", "1")
|
||||
.replace("two", "2")
|
||||
.replace("three", "3")
|
||||
.replace("four", "4")
|
||||
.replace("five", "5")
|
||||
.replace("six", "6")
|
||||
.replace("seven", "7")
|
||||
.replace("eight", "8")
|
||||
.replace("nine", "9")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_logic() {
|
||||
let input = include_str!("./testinput2.txt");
|
||||
assert_eq!(process(input), 281);
|
||||
}
|
||||
}
|
7
2023/day-01/src/testinput2.txt
Normal file
7
2023/day-01/src/testinput2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Reference in New Issue
Block a user