Skip to content

Commit

Permalink
make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 13, 2024
1 parent 0a446e6 commit 78242cd
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions src/day10.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashSet;
use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::HashSet;

#[aoc_generator(day10)]
pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
Expand All @@ -9,19 +9,29 @@ pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
.enumerate()
.map(|(y, s)| {
s.chars()
.enumerate()
.enumerate()
.map(|(x, l)| {
if l == '0' {
starts.push((y,x))
} l.to_digit(10).unwrap() as usize
})
if l == '0' {
starts.push((y, x))
}
l.to_digit(10).unwrap() as usize
})
.collect()
})
.collect()
}

pub fn hike(input: &[Vec<usize>], pos: (usize, usize), current: usize, visited: &mut HashSet<(usize,usize)>) -> usize {
pub fn hike(
input: &[Vec<usize>],
pos: (usize, usize),
current: usize,
part2: bool,
visited: &mut HashSet<(usize, usize)>,
) -> usize {
if current == 9 {
if part2 {
return 1;
}
if !visited.contains(&(pos.0, pos.1)) {
visited.insert((pos.0, pos.1));
return 1;
Expand All @@ -32,41 +42,57 @@ pub fn hike(input: &[Vec<usize>], pos: (usize, usize), current: usize, visited:

let directions = [
(0, -1), // left,
(0, 1), // right,
(0, 1), // right,
(-1, 0), // up,
(1, 0), // down,
(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);
&& 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,
part2,
visited,
);
}

}
path
}

#[aoc(day10, part1)]
pub fn part1(input: &[Vec<usize>]) -> usize {
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);
let mut visited: HashSet<(usize, usize)> = HashSet::new();
score += hike(input, (y, x), 0, false, &mut visited);
}
}
}
score
}

#[aoc(day10, part2)]
pub fn part2(input: &[Vec<usize>]) -> usize {
let mut score = 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, true, &mut visited);
}
}
}
dbg!(count, score);
score
}

Expand All @@ -87,4 +113,9 @@ mod tests {
fn test_part1() {
assert_eq!(36, part1(&input_generator(INPUT)))
}

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

0 comments on commit 78242cd

Please sign in to comment.