Skip to content

Commit

Permalink
2024: Add solution to Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoles committed Dec 10, 2024
1 parent 4aff04b commit 211d8d6
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 18 deletions.
3 changes: 3 additions & 0 deletions 2024/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cargo run --bin day01
7. [Bridge Repair](day07) 🌟🌟
8. [Resonant Collinearity](day08) 🌟🌟
9. [Disk Fragmenter](day09) 🌟🌟
10. [](day10)
10. [Hoof It](day10) 🌟🌟
11. [](day11)
12. [](day12)
13. [](day13)
Expand Down
1 change: 1 addition & 0 deletions 2024/day10/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lib = { path = "../lib" }
4 changes: 4 additions & 0 deletions 2024/day10/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0123
1234
8765
9876
7 changes: 7 additions & 0 deletions 2024/day10/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
...0...
...1...
...2...
6543456
7.....7
8.....8
9.....9
8 changes: 8 additions & 0 deletions 2024/day10/example5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
7 changes: 7 additions & 0 deletions 2024/day10/example6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.....0.
..4321.
..5..2.
..6543.
..7..4.
..8765.
..9....
7 changes: 7 additions & 0 deletions 2024/day10/example7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
53 changes: 53 additions & 0 deletions 2024/day10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
65456780121098450187634569763232345103213014321223456
56565097632787543296323478890141234234102125630310767
67874198545601612345610165765650105145693496543407898
32923238723432709654326543034789076008786587854343210
41010129610549878723017832129878989019895674963210349
03498034534698965014898901038965432123107895674310478
12567012345787854198743230123453219654256456789321565
01698107659834723085650134562104508723340349876459874
98787238543225610176590121875698698812451210145960123
07456549850110765203485430984788767904322109236871234
12349654763234894312176534983109456765012218347120105
45898763120125234523054325672112309876544367898034876
36765812034576127654369010766043218987432458765565989
29876903107681038923478001897652345898901089014877856
10945789218996987610569123078901236754321198123966987
89032654309345678510787430121230109865120567001855496
70121023498210709425698945230381210778034056532743345
63210110567601812334321876545496321689765123443412210
54216787014503956745430965496587434509894329856301101
09105496923212345866787634987676543213456018763456932
18012387836781078975898523432345232122347890345367873
27601078745896769784309410541230143021038901276216784
34587669654305854693210305670321056523427987689105698
03490548741214903543211234781987897012310094576543567
12321239230903212650106521892016798987454123467612430
12332102107812121786787430752105678901960013438900421
01489237876543080695890124567234787017871012543321530
90574323965012891234301233208943296323702387654487651
87665010121026700343210120112350185410213498703599341
96556901212345619656903234013461234554396569812678210
03467832303434328743874105684570789689487876101432387
12346345694540345012565106799688778776536964566501498
01653210789691236522210239888799669890123453677890567
18764101988780987121329845677234550187652122989108906
89765001877101071030456776540112341290343001010267210
67896982769612132549105689432101032301234578981354310
78767853458743247678014988654312121000225665432458981
69656765432658958976529812763243029810116767898567652
30345899891067867789438701890156710723209852101438943
21230014780890986290105610981260823654367643067324321
10101423634761870121234327876501994569498543458015100
45672344543052765436543234566782987678534412109176211
34985495652143781287430110545093456767623303678989321
43856784743430690398710325432112129865414510510076450
32981012892121589459621234521001036772301623423165569
01670143345023498764560149693456345681066739654234678
12365294256510107643498238782347652397659848765985589
03454385107898212532567345601098701498943707678876432
12763476543789123431052101598709876510232110569845001
29854307630076034521043015691612389323101023454032132
38765218921165087654321126780543432134569878998108941
47894356712234198765230105621234567023878565089237650
21012349803343289890121234310123498012967432176546321
120 changes: 106 additions & 14 deletions 2024/day10/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@
//! https://adventofcode.com/2024/day/10
use std::{fs, io};
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use lib::vector::Vector;

type Vec2 = Vector<i32, 2>;

const UP: Vec2 = Vec2::new([0, -1]);
const DOWN: Vec2 = Vec2::new([0, 1]);
const LEFT: Vec2 = Vec2::new([-1, 0]);
const RIGHT: Vec2 = Vec2::new([1, 0]);

fn main() {
let input = Input::from_file(format!("{}/input.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
//let input = Input::from_file(format!("{}/example1.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
println!("{input:?}");
//let input = Input::from_file(format!("{}/input.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
//let input = Input::from_file(format!("{}/example5.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
//println!("{input:?}");

// Part 1
println!("Part 1: {}", part1(&input));
Expand All @@ -17,24 +26,107 @@ fn main() {
}

fn part1(input: &Input) -> usize {
0
let trailheads: BTreeSet<Vec2> = input.heights.iter()
.filter_map(|(&p, &h)| (h == 0).then_some(p)).collect();

trailheads.into_iter().map(|head| score(head, &input.heights)).sum()
}

fn score(trailhead: Vec2, heights: &BTreeMap<Vec2, u8>) -> usize {
let mut score = 0;

let mut visited: BTreeSet<Vec2> = [trailhead].into_iter().collect();
let mut edge = vec![trailhead];

while let Some(pos) = edge.pop() {
let height = heights[&pos];

if height == 9 {
score += 1;
continue;
}

for dir in [UP, DOWN, LEFT, RIGHT] {
let next_pos = pos + dir;
if visited.contains(&next_pos) {
continue;
}

if heights.get(&next_pos).copied() == Some(height + 1) {
visited.insert(next_pos);
edge.push(next_pos);
}
}
}

score
}


fn part2(input: &Input) -> usize {
0
let trailheads: BTreeSet<Vec2> = input.heights.iter()
.filter_map(|(&p, &h)| (h == 0).then_some(p)).collect();

trailheads.into_iter().map(|head| rating(head, &input.heights)).sum()
}

fn rating(trailhead: Vec2, heights: &BTreeMap<Vec2, u8>) -> usize {
let mut rating = 0;

let mut visited: BTreeSet<Vec<Vec2>> = [vec![trailhead]].into_iter().collect();
let mut edge = vec![vec![trailhead]];

while let Some(trail) = edge.pop() {
let pos = *trail.last().unwrap();
let height = heights[&pos];
if height == 9 {
rating += 1;
continue;
}

for dir in [UP, DOWN, LEFT, RIGHT] {
let next_pos = pos + dir;
let new_trail: Vec<_> = trail.iter().copied().chain([next_pos]).collect();

if visited.contains(&new_trail) {
continue;
}

if heights.get(&next_pos).copied() == Some(height + 1) {
visited.insert(new_trail.clone());
edge.push(new_trail);
}
}
}

rating
}

#[derive(Debug, Clone)]
struct Input {
values: Vec<String>,
heights: BTreeMap<Vec2, u8>,
}

impl Input {
fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
let input = fs::read_to_string(path)?;
let values = input.lines().map(str::to_string).collect();

Ok(Self { values })
let mut heights = BTreeMap::new();

for (x, line) in input.lines().enumerate() {
for (y, c) in line.trim().chars().enumerate() {
if c == '.' {
continue;
}

let pos = Vec2::new([x as i32, y as i32]);
let height = c.to_digit(10).unwrap() as u8;

heights.insert(pos, height);
}
}

Ok(Self { heights })
}
}

Expand All @@ -44,29 +136,29 @@ mod test {

#[test]
fn test_part1() {
let input = Input::from_file("example1.txt").unwrap();
let input = Input::from_file("example5.txt").unwrap();

assert_eq!(part1(&input), 0);
assert_eq!(part1(&input), 36);
}

#[test]
fn test_part1_solution() {
let input = Input::from_file("input.txt").unwrap();

assert_eq!(part1(&input), 0);
assert_eq!(part1(&input), 776);
}

#[test]
fn test_part2() {
let input = Input::from_file("example1.txt").unwrap();
let input = Input::from_file("example5.txt").unwrap();

assert_eq!(part2(&input), 0);
assert_eq!(part2(&input), 81);
}

#[test]
fn test_part2_solution() {
let input = Input::from_file("input.txt").unwrap();

assert_eq!(part2(&input), 0);
assert_eq!(part2(&input), 1657);
}
}
4 changes: 2 additions & 2 deletions 2024/lib/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign};
pub struct Vector<T: Copy + Default, const N: usize>([T; N]);

impl<T: Copy + Default, const N: usize> Vector<T, N> {
pub fn new(vec: [T; N]) -> Self {
pub const fn new(vec: [T; N]) -> Self {
Vector(vec)
}

pub fn dimensions(self) -> usize {
pub const fn dimensions(self) -> usize {
N
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ These are mostly written in [Rust](https://www.rust-lang.org/).
I can also be found on BlueSky: [@dcoles.net](https://bsky.app/profile/dcoles.net)

## Solutions
- [Advent of Code 2024](2024#readme): 18🌟
- [Advent of Code 2024](2024#readme): 20🌟
- [Advent of Code 2023](2023#readme): 45🌟
- [Advent of Code 2022](2022#readme): 50🌟!
- [Advent of Code 2021](2021#readme): 50🌟!
Expand Down

0 comments on commit 211d8d6

Please sign in to comment.