feat: added day 10 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
parent
b9a480c97a
commit
15de4852fa
@ -11,7 +11,7 @@ jobs:
|
|||||||
name: Challenge for day
|
name: Challenge for day
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
runs-on: rust-bookworm
|
runs-on: rust-bookworm
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
|
6
day10/Cargo.toml
Normal file
6
day10/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day10"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
80
day10/src/main.rs
Normal file
80
day10/src/main.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::{env, fs};
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
fs::read_to_string(path).expect("Cannot read file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> (Vec<Vec<usize>>, HashSet<(usize, usize)>) {
|
||||||
|
let mut map = Vec::new();
|
||||||
|
let mut trail_heads = HashSet::new();
|
||||||
|
for (row, line) in input.lines().filter(|line| !line.is_empty()).enumerate() {
|
||||||
|
map.push(Vec::new());
|
||||||
|
for (col, char) in line.chars().enumerate() {
|
||||||
|
map.last_mut()
|
||||||
|
.unwrap()
|
||||||
|
.push(char.to_digit(10).unwrap() as usize);
|
||||||
|
if char == '0' {
|
||||||
|
trail_heads.insert((row, col));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(map, trail_heads)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trails_count(
|
||||||
|
map: &Vec<Vec<usize>>,
|
||||||
|
current: (usize, usize),
|
||||||
|
nines: &mut HashSet<(usize, usize)>,
|
||||||
|
) -> usize {
|
||||||
|
if map[current.0][current.1] == 9 {
|
||||||
|
nines.insert(current);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for direction in [(0, 1), (1, 0), (0, -1), (-1, 0)] {
|
||||||
|
let target = (
|
||||||
|
current.0 as i32 + direction.0,
|
||||||
|
current.1 as i32 + direction.1,
|
||||||
|
);
|
||||||
|
if (0..map.len() as i32).contains(&target.0)
|
||||||
|
&& (0..map[target.0 as usize].len() as i32).contains(&target.1)
|
||||||
|
{
|
||||||
|
let target = (target.0 as usize, target.1 as usize);
|
||||||
|
if map[target.0][target.1] == map[current.0][current.1] + 1 {
|
||||||
|
sum += trails_count(map, target, nines);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(map: &Vec<Vec<usize>>, trail_heads: &HashSet<(usize, usize)>) -> usize {
|
||||||
|
trail_heads
|
||||||
|
.iter()
|
||||||
|
.map(|(row, col)| {
|
||||||
|
let mut nines = HashSet::new();
|
||||||
|
trails_count(map, (*row, *col), &mut nines);
|
||||||
|
nines.len()
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(map: &Vec<Vec<usize>>, trail_heads: &HashSet<(usize, usize)>) -> usize {
|
||||||
|
trail_heads
|
||||||
|
.iter()
|
||||||
|
.map(|(row, col)| trails_count(map, (*row, *col), &mut HashSet::new()))
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
let input = read_input(&arg);
|
||||||
|
let (map, trail_heads) = parse_input(&input);
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", part_1(&map, &trail_heads));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", part_2(&map, &trail_heads));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user