day 3 part 1
This commit is contained in:
parent
94b3b3fd3b
commit
386b296c47
3 changed files with 109 additions and 0 deletions
54
2024/day3/part1/Cargo.lock
generated
Normal file
54
2024/day3/part1/Cargo.lock
generated
Normal file
|
@ -0,0 +1,54 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
7
2024/day3/part1/Cargo.toml
Normal file
7
2024/day3/part1/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.11.1"
|
48
2024/day3/part1/src/main.rs
Normal file
48
2024/day3/part1/src/main.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use regex::Regex;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn main() {
|
||||
let lines: Vec<String> = read_to_string("input.txt")
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let pattern1 = Regex::new(r"mul\((\d+,\d+)\)").unwrap();
|
||||
let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap();
|
||||
|
||||
let mut finalvec1 = mul_parse(lines, pattern1);
|
||||
let finalint = mul_process(finalvec1,pattern2);
|
||||
println!("{:?}",finalint)
|
||||
}
|
||||
|
||||
|
||||
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
|
||||
let mut mulvec: Vec<String> = Vec::new();
|
||||
for i in thelines {
|
||||
let tempvec: Vec<String> =
|
||||
pattern.find_iter(&i)
|
||||
.filter_map(|fr| fr.as_str().parse::<String>().ok()).collect();
|
||||
|
||||
for i2 in tempvec {
|
||||
mulvec.push(i2);
|
||||
}
|
||||
|
||||
}
|
||||
mulvec
|
||||
|
||||
}
|
||||
|
||||
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
|
||||
let mut mulint: i64 = 0;
|
||||
for i in mulvec {
|
||||
let captures = pattern.captures(&i).unwrap();
|
||||
let capture1 = captures.get(1).unwrap().as_str().parse::<i32>().unwrap();
|
||||
|
||||
let capture2 = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
|
||||
mulint += (capture1 * capture2) as i64;
|
||||
}
|
||||
|
||||
|
||||
mulint
|
||||
|
||||
}
|
Loading…
Reference in a new issue