1
0
mirror of https://gitlab.com/MisterBiggs/aoc-2023-rust.git synced 2025-07-23 22:51:32 +00:00

omg I did day 3

This commit is contained in:
2023-12-03 11:49:10 -07:00
parent d78aee48a7
commit e5203321eb
5 changed files with 247 additions and 8 deletions

83
src/day3.rs Normal file
View File

@@ -0,0 +1,83 @@
use std::{fs, vec};
use itertools::Itertools;
use regex::Regex;
pub fn run() {
println!("Day 1:");
let input = fs::read_to_string("./inputs/day3.txt").expect("Could not read file");
println!("\tPart 1: {}", part1(&input));
// println!("\tPart 2: {}", part2(&input));
}
fn part1(schematic: &str) -> usize {
let mut sum_of_parts = 0;
let padded_schematic = format!(".\n{schematic}\n.");
let re = Regex::new(r"\d+").unwrap();
let windows = padded_schematic
.lines()
.tuple_windows::<(_, _, _)>()
.collect::<Vec<_>>();
for (top, middle, bottom) in windows {
for number in re.find_iter(middle) {
let search_range = std::ops::Range {
start: number.range().start.saturating_sub(1),
end: number.range().end + 1,
};
let mut search_values: Vec<char> = vec![];
for index in search_range {
search_values.push(top.chars().nth(index).unwrap_or('.'));
search_values.push(middle.chars().nth(index).unwrap_or('.'));
search_values.push(bottom.chars().nth(index).unwrap_or('.'));
}
if search_values
.into_iter()
.filter(|c| !c.is_ascii_digit())
.filter(|c| c != &'.')
.count()
!= 0
{
sum_of_parts += number.as_str().parse::<usize>().unwrap();
}
}
}
sum_of_parts
}
// fn part2(calibration_input: &str) -> usize {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let input = "467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..";
assert_eq!(part1(input), 4361);
}
// #[test]
// fn test_2() {
// let input = "two1nine
// eightwothree
// abcone2threexyz
// xtwone3four
// 4nineeightseven2
// zoneight234
// 7pqrstsixteen";
// assert_eq!(part2(input), 281);
// }
}

View File

@@ -3,9 +3,11 @@ extern crate scan_fmt;
mod day1;
mod day2;
mod day3;
fn main() {
println!("Running Advent of Code 2023");
day1::run();
day2::run();
day3::run();
}