Dag 10 aoc, deel 2 was makkelijker dan deel 1
This commit is contained in:
6
advent_of_code/2024/10/Cargo.toml
Normal file
6
advent_of_code/2024/10/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
55
advent_of_code/2024/10/input.txt
Normal file
55
advent_of_code/2024/10/input.txt
Normal file
@@ -0,0 +1,55 @@
|
||||
0123450128945212434498107654876212322345432110787870123
|
||||
1214563287654302345347278923945003411276741025696983254
|
||||
2307874390901201436256361010232156500985891234899874765
|
||||
3458965481896543457101454322121267898764210348760565856
|
||||
4547889332787612898654456541010128709654321409651487943
|
||||
9656971245610502348742363456723489214545670512342396512
|
||||
8746560330541461059231072019894876523039987651229603401
|
||||
0130450101232878762108981923785955432128896560318712101
|
||||
1221321432543969456676870831276843201017698431105657892
|
||||
4349898540158950387985106740189652122012587120234598761
|
||||
0456797643267341293234203651076561043403436011231034650
|
||||
1245687653212210982178312312345878654312345190345125141
|
||||
4334567064307807891089425405658969763233438987656776032
|
||||
5321018165616906702376596534787439890120107678949889120
|
||||
6910789278965215610345687321096521763011234589030672101
|
||||
7821898347654334321256786543212310652101347654121543432
|
||||
6734787658901223456105897610105432543276978903443430563
|
||||
2105676987911010897834978923076501034789877412352321694
|
||||
3456989876854323708929870134589632385676966543261430785
|
||||
4367810105763432612210165245678745690123457898170567014
|
||||
3210121234176501523878954354776544787430342347089698923
|
||||
2345665893080787438965410167889432156561231456798789654
|
||||
1058756702191898341014321054974321047892120321887018763
|
||||
0569845212012567634323015123125410430121011010986323454
|
||||
6578954307623498765487654304034543221030345654345432125
|
||||
5434356788545349854599001216787652107845210787216701034
|
||||
0125643699237898703678104325894567856956789891209834345
|
||||
7876212765103203612363215454383898945787698900340125876
|
||||
0980101894454114503054356965212432430694543215489876965
|
||||
1098234583467023212125407870106541021583210676098920145
|
||||
2347899602898908763256910187017865652678701587187813236
|
||||
3256678711743219854567823298894976569549652490296704367
|
||||
0100345620651278343289654350765987478230743321345410198
|
||||
9251201234230341230178760541034300300121890120034326789
|
||||
8349212945145650789078921632133211212010581631128965632
|
||||
7658767876054787632107634780124504321123498745489874541
|
||||
6107323945763096549616543995435665210898589654788103450
|
||||
5236014539892124328723012876343786789867670123694012367
|
||||
4345867622101015610654322301212891270184561054543231018
|
||||
2109988913412126789961001454309750301293432163210102309
|
||||
3458776804569234697872156545678543432789430673456943212
|
||||
4567566543678985586543267836787612545676521982987856103
|
||||
0103457012987876487434586927898707654568701201276547894
|
||||
1212388967876567393325698810989898965439632320567030985
|
||||
0327890658905058212016784543298781012344543011498121076
|
||||
9456541243014149801134569650185632307655676322399876125
|
||||
8767632332123232100123678745670546998764985431087565436
|
||||
3498234501104343034598988764321457884643891056016501098
|
||||
2567107698612352125667639058901210745012342347121432167
|
||||
1989278786783961012787540147654323654321435218930345236
|
||||
0876989695894878109896039236569456788760324306543210145
|
||||
0105874504185769854385128545478998699354413457850105256
|
||||
1234763213096854763014537654360187543263509766969876567
|
||||
2303452342187943212323456963201236984102678876878103498
|
||||
3212301056789810103410567870102345676101278987989012567
|
||||
143
advent_of_code/2024/10/src/main.rs
Normal file
143
advent_of_code/2024/10/src/main.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
fn parse(input: &str) -> Vec<Vec<u32>> {
|
||||
const RADIX: u32 = 10;
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.chars().map(|c| c.to_digit(RADIX).unwrap()).collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn solve_1(input: &str) -> u32 {
|
||||
let map = parse(input);
|
||||
|
||||
let h = map.len();
|
||||
let w = map[0].len();
|
||||
|
||||
let mut map_info: HashMap<(usize, usize), HashSet<(usize, usize)>> = HashMap::new();
|
||||
|
||||
for (j, row) in map.iter().enumerate() {
|
||||
for (i, elevation) in row.iter().enumerate() {
|
||||
if *elevation == 9 {
|
||||
map_info.insert((i, j), HashSet::from([(i, j)]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for e in (0..=8).rev() {
|
||||
let mut new_map_info: HashMap<(usize, usize), HashSet<(usize, usize)>> = HashMap::new();
|
||||
|
||||
for ((i, j), trailheads) in map_info.into_iter() {
|
||||
let mut positions = vec![];
|
||||
if i > 0 {
|
||||
positions.push((i - 1, j));
|
||||
}
|
||||
if i < w - 1 {
|
||||
positions.push((i + 1, j));
|
||||
}
|
||||
if j > 0 {
|
||||
positions.push((i, j - 1));
|
||||
}
|
||||
if j < h - 1 {
|
||||
positions.push((i, j + 1));
|
||||
}
|
||||
|
||||
for (x, y) in positions.into_iter() {
|
||||
if map[y][x] == e {
|
||||
match new_map_info.get(&(x, y)) {
|
||||
None => new_map_info.insert((x, y), trailheads.clone()),
|
||||
Some(&ref t) => new_map_info.insert(
|
||||
(x, y),
|
||||
t.union(&trailheads).map(|ts| ts.to_owned()).collect(),
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
map_info = new_map_info;
|
||||
}
|
||||
|
||||
map_info
|
||||
.values()
|
||||
.map(|trailheads| trailheads.len() as u32)
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn solve_2(input: &str) -> u32 {
|
||||
let map = parse(input);
|
||||
|
||||
let h = map.len();
|
||||
let w = map[0].len();
|
||||
|
||||
let mut map_info: HashMap<(usize, usize), u32> = HashMap::new();
|
||||
|
||||
for (j, row) in map.iter().enumerate() {
|
||||
for (i, elevation) in row.iter().enumerate() {
|
||||
if *elevation == 9 {
|
||||
map_info.insert((i, j), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for e in (0..=8).rev() {
|
||||
let mut new_map_info: HashMap<(usize, usize), u32> = HashMap::new();
|
||||
|
||||
for ((i, j), rating) in map_info.into_iter() {
|
||||
let mut positions = vec![];
|
||||
if i > 0 {
|
||||
positions.push((i - 1, j));
|
||||
}
|
||||
if i < w - 1 {
|
||||
positions.push((i + 1, j));
|
||||
}
|
||||
if j > 0 {
|
||||
positions.push((i, j - 1));
|
||||
}
|
||||
if j < h - 1 {
|
||||
positions.push((i, j + 1));
|
||||
}
|
||||
|
||||
for (x, y) in positions.into_iter() {
|
||||
if map[y][x] == e {
|
||||
match new_map_info.get(&(x, y)) {
|
||||
None => new_map_info.insert((x, y), rating),
|
||||
Some(r) => new_map_info.insert((x, y), r + rating),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
map_info = new_map_info;
|
||||
}
|
||||
|
||||
map_info.values().sum()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
|
||||
let input = include_str!("../input.txt");
|
||||
|
||||
let result_1 = solve_1(input);
|
||||
println!("The sum of scores of all trailheads is {}", result_1);
|
||||
|
||||
let result_2 = solve_2(input);
|
||||
println!("The sum of ratings of all trailheads is {}", result_2);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
let test_input = include_str!("../test.txt");
|
||||
assert_eq!(solve_1(test_input), 36);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
let test_input = include_str!("../test.txt");
|
||||
assert_eq!(solve_2(test_input), 81);
|
||||
}
|
||||
}
|
||||
8
advent_of_code/2024/10/test.txt
Normal file
8
advent_of_code/2024/10/test.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732
|
||||
Reference in New Issue
Block a user