day 03 part 1

This commit is contained in:
Spectre 2023-12-08 13:59:09 +01:00
parent 529e2fad5c
commit d8816f70d7
10 changed files with 260 additions and 25 deletions

View file

@ -1,5 +1,6 @@
use shared::{Answer, Solution};
use regex::Regex;
use substring::Substring;
pub struct Day03;
@ -7,25 +8,57 @@ impl Solution for Day03 {
fn part_1(&self, input: &str) -> Answer {
let lines: Vec<&str> = input.split("\n").collect();
let pattern = Regex::new(
// r"[^\d.^]?(\d+)[\d.]|[^\d.](\d+)[\d.$]?"
r"(\d+)"
).unwrap();
let mut out: u64 = 0;
let mut sum: u64 = 0;
let re = Regex::new(r"\d+").unwrap();
let special = Regex::new(r"[^\d.]").unwrap();
for i in 0..lines.len() {
for m in pattern.find_iter(lines[i]) {
match lines.get(i-1) {
_ => (),
for (i, line) in lines.iter().enumerate() {
for capture in re.find_iter(line) {
let prev = if i > 0 { lines.get(i - 1) } else { None };
let curr = lines.get(i);
let next = lines.get(i+1);
if match prev {
Some(l) => {
let part = l.substring(1.max(capture.start()) - 1, capture.end() + 1);
special.is_match(part)
},
None => false,
} | match curr {
Some(l) => {
let part = l.substring(1.max(capture.start()) - 1, capture.end() + 1);
special.is_match(part)
},
None => false,
} | match next {
Some(l) => {
let part = l.substring(1.max(capture.start()) - 1, capture.end() + 1);
special.is_match(part)
},
None => false,
} {
out += capture.as_str().parse::<u64>().unwrap();
}
}
}
Answer::Unimplemented
Answer::from(out)
}
fn part_2(&self, input: &str) -> Answer {
todo!()
let lines: Vec<&str> = input.split("\n").collect();
let mut out: u64 = 0;
for (i, line) in lines.iter().enumerate() {
}
Answer::Unimplemented
}
}
fn extract_num(line: &str, pos: usize) -> u64 {
todo!()
}