Day 3 completed !
This commit is contained in:
parent
e348d05d1c
commit
3ccc160326
|
|
@ -7,7 +7,20 @@ struct Match {
|
||||||
value: u32,
|
value: u32,
|
||||||
line: usize,
|
line: usize,
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
is_valid: bool
|
is_valid: bool,
|
||||||
|
gears: Vec<Point>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum Part {
|
||||||
|
One,
|
||||||
|
Two
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct Point {
|
||||||
|
x: u32,
|
||||||
|
y: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_symbol(line: &[char], range: Range<usize>) -> bool {
|
fn is_symbol(line: &[char], range: Range<usize>) -> bool {
|
||||||
|
|
@ -20,8 +33,21 @@ fn is_symbol(line: &[char], range: Range<usize>) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_gears(line: &[char], range: Range<usize>, line_offset: usize) -> Vec<Point> {
|
||||||
|
let mut results: Vec<Point> = Vec::new();
|
||||||
|
let x_offset = range.start;
|
||||||
|
|
||||||
|
for (i, c) in line[range].iter().enumerate() {
|
||||||
|
if *c == '*' {
|
||||||
|
results.push(Point { x: (x_offset + i) as u32, y: line_offset as u32 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn solve(input: Vec<String>) -> u32 {
|
pub fn solve(input: Vec<String>, part: Part) -> u32 {
|
||||||
let regex = Regex::new(r"\d+").unwrap();
|
let regex = Regex::new(r"\d+").unwrap();
|
||||||
|
|
||||||
let mut matches: Vec<Match> = Vec::new();
|
let mut matches: Vec<Match> = Vec::new();
|
||||||
|
|
@ -31,7 +57,7 @@ pub fn solve(input: Vec<String>) -> u32 {
|
||||||
for (i, line) in input.iter().enumerate() {
|
for (i, line) in input.iter().enumerate() {
|
||||||
for capture in regex.captures_iter(line) {
|
for capture in regex.captures_iter(line) {
|
||||||
if let Some(capture) = capture.get(0) {
|
if let Some(capture) = capture.get(0) {
|
||||||
matches.push(Match { value: capture.as_str().parse::<u32>().unwrap(), line: i, range: capture.range(), is_valid: false });
|
matches.push(Match { value: capture.as_str().parse::<u32>().unwrap(), line: i, range: capture.range(), is_valid: false, gears: Vec::new() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dataset.push(line.chars().collect());
|
dataset.push(line.chars().collect());
|
||||||
|
|
@ -47,14 +73,19 @@ pub fn solve(input: Vec<String>) -> u32 {
|
||||||
if item.line >= 1 {
|
if item.line >= 1 {
|
||||||
|
|
||||||
b1 = is_symbol(dataset.get(item.line-1).unwrap(), range.to_owned());
|
b1 = is_symbol(dataset.get(item.line-1).unwrap(), range.to_owned());
|
||||||
|
item.gears.append(&mut find_gears(dataset.get(item.line-1).unwrap(), range.to_owned(), item.line-1));
|
||||||
|
|
||||||
println!("\tChecking line #{} - {}\t'{:?}'", item.line-1, b1, &dataset.get(item.line-1).unwrap()[range.to_owned()]);
|
println!("\tChecking line #{} - {}\t'{:?}'", item.line-1, b1, &dataset.get(item.line-1).unwrap()[range.to_owned()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let b2 = is_symbol(dataset.get(item.line).unwrap(), range.to_owned());
|
let b2 = is_symbol(dataset.get(item.line).unwrap(), range.to_owned());
|
||||||
|
item.gears.append(&mut find_gears(dataset.get(item.line).unwrap(), range.to_owned(), item.line));
|
||||||
println!("\tChecking line #{} - {}\t'{:?}'", item.line, b2, &dataset.get(item.line).unwrap()[range.to_owned()]);
|
println!("\tChecking line #{} - {}\t'{:?}'", item.line, b2, &dataset.get(item.line).unwrap()[range.to_owned()]);
|
||||||
|
|
||||||
if item.line < dataset.len() - 1 {
|
if item.line < dataset.len() - 1 {
|
||||||
b3 = is_symbol(dataset.get(item.line+1).unwrap(), range.to_owned());
|
b3 = is_symbol(dataset.get(item.line+1).unwrap(), range.to_owned());
|
||||||
|
item.gears.append(&mut find_gears(dataset.get(item.line+1).unwrap(), range.to_owned(), item.line+1));
|
||||||
|
|
||||||
println!("\tChecking line #{} - {}\t'{:?}'", item.line+1, b1, &dataset.get(item.line+1).unwrap()[range.to_owned()]);
|
println!("\tChecking line #{} - {}\t'{:?}'", item.line+1, b1, &dataset.get(item.line+1).unwrap()[range.to_owned()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +99,22 @@ pub fn solve(input: Vec<String>) -> u32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sum
|
let mut sum_gears = 0;
|
||||||
|
for (i, item) in matches.iter().enumerate() {
|
||||||
|
for gears_point in &item.gears {
|
||||||
|
for other_match in &matches[(i+1)..] {
|
||||||
|
if other_match.gears.contains(gears_point) {
|
||||||
|
println!("Match gear at {:?} -> {} * {}", gears_point, item.value, other_match.value);
|
||||||
|
sum_gears += item.value * other_match.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match part {
|
||||||
|
Part::One => sum,
|
||||||
|
Part::Two => sum_gears
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -80,6 +126,13 @@ mod tests {
|
||||||
fn parse() {
|
fn parse() {
|
||||||
let input: Vec<String> = "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598..".lines().map(|v| v.to_owned()).collect();
|
let input: Vec<String> = "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598..".lines().map(|v| v.to_owned()).collect();
|
||||||
|
|
||||||
assert_eq!(solve(input), 4361);
|
assert_eq!(solve(input, Part::One), 4361);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_part2() {
|
||||||
|
let input: Vec<String> = "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598..".lines().map(|v| v.to_owned()).collect();
|
||||||
|
|
||||||
|
assert_eq!(solve(input, Part::Two), 467835);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,10 +9,6 @@ mod utils;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt";
|
|
||||||
let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"));
|
|
||||||
|
|
||||||
println!("result: {}", result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -44,8 +40,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn day3() {
|
fn day3() {
|
||||||
let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt";
|
let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt";
|
||||||
let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"));
|
let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::One);
|
||||||
|
let result2 = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::Two);
|
||||||
|
|
||||||
assert_eq!(result, 522726);
|
assert_eq!(result, 522726);
|
||||||
|
assert_eq!(result2, 81721933);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue