init project, add day 1

This commit is contained in:
Lea 2023-12-01 09:39:08 +01:00
commit 1e2d34a17b
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
3 changed files with 1061 additions and 0 deletions

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "day_1-2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.10.2"

1000
input.txt Normal file

File diff suppressed because it is too large Load diff

52
src/main.rs Normal file
View file

@ -0,0 +1,52 @@
use regex::Regex;
const WORDS: [&str; 19] = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "0",
];
fn main() {
let regex = Regex::new(WORDS.join("|").as_str()).unwrap();
let input = include_str!("../input.txt");
let mut results: Vec<i32> = Vec::new();
for line in input
.split("\n")
.filter(|line| !line.is_empty()
) {
let mut numbers: Vec<char> = Vec::new();
let mut index: i32 = 0;
while let Some(m) = regex.find_at(&line, index as usize) {
index = m.start() as i32 + 1;
numbers.push(match m.as_str() {
"one" => '1',
"two" => '2',
"three" => '3',
"four" => '4',
"five" => '5',
"six" => '6',
"seven" => '7',
"eight" => '8',
"nine" => '9',
x => x.chars().next().unwrap()
});
}
let num: i32 = format!("{}{}", numbers.first().unwrap(), numbers.last().unwrap())
.parse()
.unwrap();
results.push(num);
}
println!("Result: {}", results.iter().sum::<i32>());
}