advent-of-code-2023/day3/src/part1.rs

129 lines
2.9 KiB
Rust

use regex::{Match, Regex};
use std::{fmt::Display, num::ParseIntError};
#[derive(Debug)]
struct Symbol {
value: char,
row: usize,
col: usize,
}
impl Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: ({},{})", self.value, self.row + 1, self.col + 1)
}
}
#[derive(Debug)]
struct Number {
value: u32,
row: usize,
start: usize,
end: usize,
}
impl Display for Number {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{0}: ({1},{2}) -> ({1},{3})",
self.value,
self.row + 1,
self.start + 1,
self.end + 1,
)
}
}
impl Number {
fn valid(&self, symbols: &[Symbol]) -> bool {
let adjacent: Vec<usize> = symbols
.iter()
.filter_map(|m| {
let start = if self.row > 1 { self.row - 1 } else { 1 };
let end = self.row + 1;
if (start..=end).contains(&m.row) {
Some(m.col)
} else {
None
}
})
.collect();
let start = if self.start > 1 { self.start - 1 } else { 1 };
let end = self.end + 1;
(start..=end).any(|m| adjacent.contains(&m))
}
}
impl TryFrom<Match<'_>> for Number {
type Error = ParseIntError;
fn try_from(value: Match) -> Result<Self, Self::Error> {
Ok(Self {
value: value.as_str().parse()?,
row: 0,
start: value.start(),
end: value.end(),
})
}
}
fn get_symbols(input: &str) -> Vec<Symbol> {
let mut items: Vec<Symbol> = Vec::new();
for (row, line) in input.lines().enumerate() {
for (col, value) in line.chars().enumerate() {
if !value.is_ascii_digit() && value != '.' {
items.push(Symbol { value, row, col })
}
}
}
items
}
fn get_numbers(input: &str) -> Vec<Number> {
let rstring = r"(\d)+";
let mut items: Vec<Number> = Vec::new();
for (row, line) in input.lines().enumerate() {
for item in Regex::new(rstring).unwrap().find_iter(line) {
let mut number = Number::try_from(item).unwrap();
number.row = row;
items.push(number);
}
}
items
}
pub fn main(input: &str) -> u32 {
let symbols = get_symbols(input);
get_numbers(input)
.iter()
.filter_map(|m| {
if m.valid(&symbols) {
Some(m.value)
} else {
None
}
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example() {
let input = "467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..";
assert_eq!(main(input), 4361);
}
}