Add working day2 solution
This commit is contained in:
parent
5989e4f10f
commit
5a5c7d019c
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -15,6 +15,7 @@ dependencies = [
|
||||||
name = "day1"
|
name = "day1"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
"regex",
|
"regex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
aho-corasick = "1.1.2"
|
||||||
regex = "1.10.2"
|
regex = "1.10.2"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
mod part1;
|
mod part1;
|
||||||
|
mod part2;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Part1: {}", part1::main());
|
println!("Part1: {}", part1::main());
|
||||||
println!("Part2: TODO");
|
println!("Part2: {}", part2::main());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
pub(crate) fn main() -> i32 {
|
pub fn main() -> i32 {
|
||||||
parse(include_str!("input.txt"))
|
parse(include_str!("input.txt"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse(input: &str) -> i32 {
|
pub fn parse(input: &str) -> i32 {
|
||||||
input
|
input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|m| calibration_values(num_from_line(m)))
|
.map(|m| calibration_values(num_from_line(m)))
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn num_from_line(line: &str) -> Vec<i32> {
|
pub fn num_from_line(line: &str) -> Vec<i32> {
|
||||||
Regex::new(r"(\d){1}")
|
Regex::new(r"(\d){1}")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.find_iter(line)
|
.find_iter(line)
|
||||||
|
@ -23,7 +23,7 @@ pub(crate) fn num_from_line(line: &str) -> Vec<i32> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn calibration_values(num_vec: Vec<i32>) -> i32 {
|
pub fn calibration_values(num_vec: Vec<i32>) -> i32 {
|
||||||
([num_vec[0], num_vec[num_vec.len() - 1]])
|
([num_vec[0], num_vec[num_vec.len() - 1]])
|
||||||
.iter()
|
.iter()
|
||||||
.fold(String::new(), |a, b| a + &b.to_string())
|
.fold(String::new(), |a, b| a + &b.to_string())
|
||||||
|
|
54
day1/src/part2.rs
Normal file
54
day1/src/part2.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
use crate::part1::calibration_values;
|
||||||
|
use aho_corasick::AhoCorasick;
|
||||||
|
|
||||||
|
pub fn main() -> i32 {
|
||||||
|
parse(include_str!("input.txt"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(input: &str) -> i32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|m| calibration_values(num_from_line(m)))
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn num_from_line(line: &str) -> Vec<i32> {
|
||||||
|
// Convert word versions of numbers to i32, preserving order
|
||||||
|
let patterns = [
|
||||||
|
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "1", "2", "3", "4",
|
||||||
|
"5", "6", "7", "8", "9",
|
||||||
|
];
|
||||||
|
AhoCorasick::new(patterns)
|
||||||
|
.unwrap()
|
||||||
|
.find_overlapping_iter(line)
|
||||||
|
.map(|m| match patterns[m.pattern()] {
|
||||||
|
"one" => 1,
|
||||||
|
"two" => 2,
|
||||||
|
"three" => 3,
|
||||||
|
"four" => 4,
|
||||||
|
"five" => 5,
|
||||||
|
"six" => 6,
|
||||||
|
"seven" => 7,
|
||||||
|
"eight" => 8,
|
||||||
|
"nine" => 9,
|
||||||
|
any => any.parse().expect("Should work due to previous filtering"),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example() {
|
||||||
|
let input = "two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen";
|
||||||
|
assert_eq!(parse(input), 281)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue