day3 solved

This commit is contained in:
simon
2024-09-30 13:17:13 +02:00
parent 215c4dae8b
commit cae09fba68
2 changed files with 75 additions and 14 deletions

View File

@@ -1,3 +1,4 @@
--- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
@@ -29,7 +30,42 @@ In this schematic, two numbers are not part numbers because they are not adjacen
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
To begin, get your puzzle input.
Your puzzle answer was 512794.
--- Part Two ---
Answer:
You can also [Shareon Twitter Mastodon] this puzzle.
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
Consider the same engine schematic again:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.
What is the sum of all of the gear ratios in your engine schematic?
Your puzzle answer was 67779080.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Shareon Twitter Mastodon] this puzzle.

View File

@@ -1,11 +1,9 @@
use core::num;
use regex::Regex;
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
fs::File,
io::{self, BufRead},
path::Path,
process::exit,
};
#[macro_use(c)]
extern crate cute;
@@ -15,16 +13,43 @@ fn main() {
if let Ok(lines) = read_lines("./input") {
// Consumes the iterator, returns an (Optional) String
let lines_array: Vec<String> = lines.flatten().collect::<Vec<String>>();
let mut symbols: HashMap<(i32, i32), Vec<i32>> = c! {key => Vec::new(), for key in c![(line_index as i32, char_index as i32), for char_index in 0..lines_array[line_index].len(), for line_index in 0..lines_array.len()]};
println!("{symbols:?}");
exit(0);
for (line_index, line) in lines_array.iter().enumerate() {
let digit_regex = Regex::new(r"\d+").unwrap();
for finding in digit_regex.find_iter(line) {
println!("{}", finding.start());
let symbols_list = c![(line_index as i32, char_index as i32), for char_index in 0..lines_array[line_index].len(), for line_index in 0..lines_array.len(), if !ignore_list.contains(lines_array[line_index].chars().nth(char_index).unwrap())];
let mut chars: HashMap<(i32, i32), Vec<i32>> =
c! {key => Vec::new(), for key in symbols_list};
let digit_regex = Regex::new(r"\d+").unwrap();
for (r, row) in lines_array.iter().enumerate() {
for n in digit_regex.find_iter(row) {
let edge: HashSet<(i32, i32)> = HashSet::from_iter(
c![(a as i32, c as i32), for a in [(r as i32) -1,r as i32, r as i32 + 1], for c in ((n.start() as i32) -1)..((n.end() as i32)+1)],
);
let intersection: Vec<_> =
edge.into_iter().filter(|x| chars.contains_key(x)).collect();
for o in intersection {
chars
.get_mut(&o)
.unwrap()
.push(n.as_str().parse::<i32>().unwrap())
}
}
}
let part1: i32 = chars
.values()
.map(|x: &Vec<i32>| x.iter().sum::<i32>())
.collect::<Vec<i32>>()
.iter()
.sum();
println!("Part 1 {part1:?}",);
let part2: i32 = chars
.values()
.filter(|x| x.len() == 2)
.to_owned()
.collect::<Vec<_>>()
.into_iter()
.map(|x| x.get(0).unwrap() * x.get(1).unwrap())
.collect::<Vec<i32>>()
.iter()
.sum();
println!("Part 2 {part2:?}");
}
}