Skip to content

Commit

Permalink
Add day10
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 12, 2024
1 parent e71638c commit 0a446e6
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 68 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

11 changes: 11 additions & 0 deletions .idea/advent-of-code-2024.iml

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

14 changes: 14 additions & 0 deletions .idea/aws.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

54 changes: 54 additions & 0 deletions input/2024/day10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
212521982345455432198898732343201001454321076767899872
101430671156766718089087001432102122367410789898721701
234549560049859809670106126549893432198523650127630652
345678432132343218543215437456784543007694540134544543
434989010101034567894326458941089656912784343296531014
123968523218954326287478947032178767843895256787432543
037879654305869210156565432142189626756706101208956632
010968760456778981432101943443076212389617890312347701
323459821678765476583201875654561003476526345423698801
011234734569012345694112766743432876565430256784510932
010345654554212014785073450898101986676321100898929841
143456703623103423456787321287632987685672901289838750
212769812514398512987896544379443456894589874356745669
301898453405487600121095434568543670123601065105210178
498782345654676321436784323877612589010892110234312321
567101908723521030545654012981003438756763323987603430
323877819810437841690343021542312321065654332109544543
014966521923456956781252120455401487654505445678037652
565455430810398105898763214345566590123218764789128781
678321876760567234237454905216697687689439653234569390
549050945651456340145667876107788014578321342106778434
432167834512301254206758989038979123065430211067884521
212058723003456763219843210123460132104321203458990690
103449012124567895430764505674321043895650432143021788
214530101013098986721256034985452344786766501032134659
345621232322121234890340125676965435689897865401235678
238789985421030345234543234767876105476101974321945234
129376576534781676167652149866765256785232389450876165
023403456215696787018967019875454343494341071265210074
016512567304345698101278112562343852014556780374391189
187876458412234598790349603401438961025698895489580543
098962389561103347685456784876547873234767216785671672
123451049870101256576545692910687210189854306894502981
212945432943232345677834501431296332182344345663213870
301876501854569431988925232321345345091103216756344561
676510345763078520123810123410543456780234109865432150
783401256762107019654320294567632109876542106776843001
892313879856234198734521287678932323438943769089987612
341054965447895287645632789456541010567659858123656543
250123832332196014532745610367692101298365647654567698
167814001541087123691821001298789854303234737843238787
078905123456789234780934789656730763214159826943129898
980876432965410165690695678749821278934067015652010789
801986501874321074321783265637832123653458234761001656
212567988965010787210654104521945034562109101891012347
123498677654321298323545003010876453078045610123456798
034014576103432186789276512123289342199236769032347810
145623985412545085652189467898100256787100898741016921
898767234307696198543011056967221105107231239653455430
745678101268987585654322343254339012218774381012768741
234989089456789676789113698107448763329783498019889650
101345674301410566541004567898758954458692567325676341
013216765210323455632123476127667763067501101234765432
322109876323454556789012981034554892155432101289876501
123 changes: 55 additions & 68 deletions src/day10.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,75 @@
use std::collections::HashSet;
use aoc_runner_derive::{aoc, aoc_generator};

#[aoc_generator(day10)]
pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
let mut starts = vec![];
input
.lines()
.map(|s| {
.enumerate()
.map(|(y, s)| {
s.chars()
.map(|l| l.to_digit(10).unwrap() as usize)
.enumerate()
.map(|(x, l)| {
if l == '0' {
starts.push((y,x))
} l.to_digit(10).unwrap() as usize
})
.collect()
})
.collect()
}

// #[memoize]
// pub fn blink(stone: usize, counter: usize, limit: usize) -> usize {
// let mut stack = 0;
// if counter == limit {
// return 1;
// }
pub fn hike(input: &[Vec<usize>], pos: (usize, usize), current: usize, visited: &mut HashSet<(usize,usize)>) -> usize {
if current == 9 {
if !visited.contains(&(pos.0, pos.1)) {
visited.insert((pos.0, pos.1));
return 1;
}
return 0;
}
let mut path = 0;

let directions = [
(0, -1), // left,
(0, 1), // right,
(-1, 0), // up,
(1, 0), // down,
];

for (dy, dx) in directions {
let ny = pos.0 as isize + dy;
let nx = pos.1 as isize + dx;
if ny >= 0
&& ny < input.len() as isize
&& nx >= 0
&& nx < input[pos.0].len() as isize
&& input[ny as usize][nx as usize] == current + 1 {
path += hike(input, (ny as usize, nx as usize), current+1, visited);
}

// if stone == 0 {
// stack += blink(1, counter + 1, limit);
// } else {
// let mut div = stone;
// let mut count = 0;
// while div > 0 {
// div /= 10;
// count += 1;
// }
// if count % 2 == 0 {
// stack += blink(stone / 10_usize.pow(count / 2), counter + 1, limit);
// stack += blink(stone % 10_usize.pow(count / 2), counter + 1, limit);
// } else {
// stack += blink(stone * 2024, counter + 1, limit);
// }
// }
// stack
// }
}
path
}

#[aoc(day10, part1)]
pub fn part1(input: &[Vec<usize>]) -> usize {
// let directions = [
// (0, 1), // right
// (0, -1), // left
// (1, 0), // up
// (-1, 0), // down
// ];
// for (row_idx, row) in input.iter().enumerate() {
// dbg!(row_idx, row);
// for (col_idx, col) in row.iter().enumerate() {
// dbg!(col_idx, col);
// if *col == 0 {
// for &(dy, dx) in &directions {
// let ny: isize = y as isize + i * dy;
// let nx = x as isize + i * dx;
// if ny >= 0
// && ny < input.len() as isize
// && nx >= 0
// && nx < input[y].len() as isize
// {
// match *col {}
// }
// }
// // match input[row_idx][col_idx + 1]
// }
// }
// }
0
let mut score = 0;
let mut count = 0;
for (y, row) in input.iter().enumerate() {
for (x, col) in row.iter().enumerate() {
if *col == 0 {
let mut visited: HashSet<(usize,usize)> = HashSet::new();
score += hike(input, (y, x), 0, &mut visited);
count +=1;
dbg!(count, score);
}
}
}
dbg!(count, score);
score
}

// #[aoc(day10, part2)]
// pub fn part2(input: &[usize]) -> usize {
// let mut lenght = 0;
// for stone in input {
// lenght += blink(*stone, 0, 75);
// }
// lenght
// }

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -93,11 +85,6 @@ mod tests {

#[test]
fn test_part1() {
assert_eq!(55312, part1(&input_generator(INPUT)))
assert_eq!(36, part1(&input_generator(INPUT)))
}

// #[test]
// fn test_part2() {
// assert_eq!(55312, part2(&input_generator(INPUT)))
// }
}

0 comments on commit 0a446e6

Please sign in to comment.