Skip to content

Commit 0a446e6

Browse files
committed
Add day10
1 parent e71638c commit 0a446e6

File tree

7 files changed

+156
-68
lines changed

7 files changed

+156
-68
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/advent-of-code-2024.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/aws.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

input/2024/day10.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
212521982345455432198898732343201001454321076767899872
2+
101430671156766718089087001432102122367410789898721701
3+
234549560049859809670106126549893432198523650127630652
4+
345678432132343218543215437456784543007694540134544543
5+
434989010101034567894326458941089656912784343296531014
6+
123968523218954326287478947032178767843895256787432543
7+
037879654305869210156565432142189626756706101208956632
8+
010968760456778981432101943443076212389617890312347701
9+
323459821678765476583201875654561003476526345423698801
10+
011234734569012345694112766743432876565430256784510932
11+
010345654554212014785073450898101986676321100898929841
12+
143456703623103423456787321287632987685672901289838750
13+
212769812514398512987896544379443456894589874356745669
14+
301898453405487600121095434568543670123601065105210178
15+
498782345654676321436784323877612589010892110234312321
16+
567101908723521030545654012981003438756763323987603430
17+
323877819810437841690343021542312321065654332109544543
18+
014966521923456956781252120455401487654505445678037652
19+
565455430810398105898763214345566590123218764789128781
20+
678321876760567234237454905216697687689439653234569390
21+
549050945651456340145667876107788014578321342106778434
22+
432167834512301254206758989038979123065430211067884521
23+
212058723003456763219843210123460132104321203458990690
24+
103449012124567895430764505674321043895650432143021788
25+
214530101013098986721256034985452344786766501032134659
26+
345621232322121234890340125676965435689897865401235678
27+
238789985421030345234543234767876105476101974321945234
28+
129376576534781676167652149866765256785232389450876165
29+
023403456215696787018967019875454343494341071265210074
30+
016512567304345698101278112562343852014556780374391189
31+
187876458412234598790349603401438961025698895489580543
32+
098962389561103347685456784876547873234767216785671672
33+
123451049870101256576545692910687210189854306894502981
34+
212945432943232345677834501431296332182344345663213870
35+
301876501854569431988925232321345345091103216756344561
36+
676510345763078520123810123410543456780234109865432150
37+
783401256762107019654320294567632109876542106776843001
38+
892313879856234198734521287678932323438943769089987612
39+
341054965447895287645632789456541010567659858123656543
40+
250123832332196014532745610367692101298365647654567698
41+
167814001541087123691821001298789854303234737843238787
42+
078905123456789234780934789656730763214159826943129898
43+
980876432965410165690695678749821278934067015652010789
44+
801986501874321074321783265637832123653458234761001656
45+
212567988965010787210654104521945034562109101891012347
46+
123498677654321298323545003010876453078045610123456798
47+
034014576103432186789276512123289342199236769032347810
48+
145623985412545085652189467898100256787100898741016921
49+
898767234307696198543011056967221105107231239653455430
50+
745678101268987585654322343254339012218774381012768741
51+
234989089456789676789113698107448763329783498019889650
52+
101345674301410566541004567898758954458692567325676341
53+
013216765210323455632123476127667763067501101234765432
54+
322109876323454556789012981034554892155432101289876501

src/day10.rs

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,75 @@
1+
use std::collections::HashSet;
12
use aoc_runner_derive::{aoc, aoc_generator};
23

34
#[aoc_generator(day10)]
45
pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
6+
let mut starts = vec![];
57
input
68
.lines()
7-
.map(|s| {
9+
.enumerate()
10+
.map(|(y, s)| {
811
s.chars()
9-
.map(|l| l.to_digit(10).unwrap() as usize)
12+
.enumerate()
13+
.map(|(x, l)| {
14+
if l == '0' {
15+
starts.push((y,x))
16+
} l.to_digit(10).unwrap() as usize
17+
})
1018
.collect()
1119
})
1220
.collect()
1321
}
1422

15-
// #[memoize]
16-
// pub fn blink(stone: usize, counter: usize, limit: usize) -> usize {
17-
// let mut stack = 0;
18-
// if counter == limit {
19-
// return 1;
20-
// }
23+
pub fn hike(input: &[Vec<usize>], pos: (usize, usize), current: usize, visited: &mut HashSet<(usize,usize)>) -> usize {
24+
if current == 9 {
25+
if !visited.contains(&(pos.0, pos.1)) {
26+
visited.insert((pos.0, pos.1));
27+
return 1;
28+
}
29+
return 0;
30+
}
31+
let mut path = 0;
32+
33+
let directions = [
34+
(0, -1), // left,
35+
(0, 1), // right,
36+
(-1, 0), // up,
37+
(1, 0), // down,
38+
];
39+
40+
for (dy, dx) in directions {
41+
let ny = pos.0 as isize + dy;
42+
let nx = pos.1 as isize + dx;
43+
if ny >= 0
44+
&& ny < input.len() as isize
45+
&& nx >= 0
46+
&& nx < input[pos.0].len() as isize
47+
&& input[ny as usize][nx as usize] == current + 1 {
48+
path += hike(input, (ny as usize, nx as usize), current+1, visited);
49+
}
2150

22-
// if stone == 0 {
23-
// stack += blink(1, counter + 1, limit);
24-
// } else {
25-
// let mut div = stone;
26-
// let mut count = 0;
27-
// while div > 0 {
28-
// div /= 10;
29-
// count += 1;
30-
// }
31-
// if count % 2 == 0 {
32-
// stack += blink(stone / 10_usize.pow(count / 2), counter + 1, limit);
33-
// stack += blink(stone % 10_usize.pow(count / 2), counter + 1, limit);
34-
// } else {
35-
// stack += blink(stone * 2024, counter + 1, limit);
36-
// }
37-
// }
38-
// stack
39-
// }
51+
}
52+
path
53+
}
4054

4155
#[aoc(day10, part1)]
4256
pub fn part1(input: &[Vec<usize>]) -> usize {
43-
// let directions = [
44-
// (0, 1), // right
45-
// (0, -1), // left
46-
// (1, 0), // up
47-
// (-1, 0), // down
48-
// ];
49-
// for (row_idx, row) in input.iter().enumerate() {
50-
// dbg!(row_idx, row);
51-
// for (col_idx, col) in row.iter().enumerate() {
52-
// dbg!(col_idx, col);
53-
// if *col == 0 {
54-
// for &(dy, dx) in &directions {
55-
// let ny: isize = y as isize + i * dy;
56-
// let nx = x as isize + i * dx;
57-
// if ny >= 0
58-
// && ny < input.len() as isize
59-
// && nx >= 0
60-
// && nx < input[y].len() as isize
61-
// {
62-
// match *col {}
63-
// }
64-
// }
65-
// // match input[row_idx][col_idx + 1]
66-
// }
67-
// }
68-
// }
69-
0
57+
let mut score = 0;
58+
let mut count = 0;
59+
for (y, row) in input.iter().enumerate() {
60+
for (x, col) in row.iter().enumerate() {
61+
if *col == 0 {
62+
let mut visited: HashSet<(usize,usize)> = HashSet::new();
63+
score += hike(input, (y, x), 0, &mut visited);
64+
count +=1;
65+
dbg!(count, score);
66+
}
67+
}
68+
}
69+
dbg!(count, score);
70+
score
7071
}
7172

72-
// #[aoc(day10, part2)]
73-
// pub fn part2(input: &[usize]) -> usize {
74-
// let mut lenght = 0;
75-
// for stone in input {
76-
// lenght += blink(*stone, 0, 75);
77-
// }
78-
// lenght
79-
// }
80-
8173
#[cfg(test)]
8274
mod tests {
8375
use super::*;
@@ -93,11 +85,6 @@ mod tests {
9385

9486
#[test]
9587
fn test_part1() {
96-
assert_eq!(55312, part1(&input_generator(INPUT)))
88+
assert_eq!(36, part1(&input_generator(INPUT)))
9789
}
98-
99-
// #[test]
100-
// fn test_part2() {
101-
// assert_eq!(55312, part2(&input_generator(INPUT)))
102-
// }
10390
}

0 commit comments

Comments
 (0)