Skip to content

Commit

Permalink
feat: day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 6, 2024
1 parent ad76ed1 commit 7462f62
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ chrono = { version = "0.4.38", optional = true }
dhat = { version = "0.3.3", optional = true }
itertools = "0.13.0"
pico-args = "0.5.0"
rayon = "1.10.0"
regex = "1.11.1"
tinyjson = "2.5.1"

Expand Down
10 changes: 10 additions & 0 deletions data/examples/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
88 changes: 88 additions & 0 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use rayon::prelude::*;
use std::collections::HashSet;

advent_of_code::solution!(6);

const DIRS: [(isize, isize); 4] = [(-1, 0), (0, 1), (1, 0), (0, -1)];

fn in_bounds(map: &Vec<Vec<char>>, pos: (isize, isize)) -> bool {
pos.0 >= 0 && pos.1 >= 0 && pos.0 < map.len() as isize && pos.1 < map[0].len() as isize
}

fn check_map(map: &Vec<Vec<char>>, start: (isize, isize)) -> (bool, Vec<(isize, isize)>) {
let mut visited: HashSet<(isize, isize)> = HashSet::new();
let mut visited_dir: HashSet<((isize, isize), usize)> = HashSet::new();
let mut dir = 0;
let mut current = start;
loop {
if visited_dir.contains(&(current, dir)) {
return (true, Vec::new());
}
visited.insert(current);
visited_dir.insert((current, dir));
let next = (current.0 + DIRS[dir].0, current.1 + DIRS[dir].1);
if !in_bounds(map, next) {
return (false, visited.iter().map(|x| *x).collect());
}
if map[next.0 as usize][next.1 as usize] == '#' {
dir = (dir + 1) % 4;
continue;
}
current = next;
}
}

fn parse_input(input: &str) -> (Vec<Vec<char>>, (isize, isize)) {
let map: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let mut start: (isize, isize) = (0, 0);
for i in 0..map.len() {
for j in 0..map[i].len() {
if map[i][j] == '^' {
start = (i as isize, j as isize);
}
}
}
(map, start)
}

pub fn part_one(input: &str) -> Option<u32> {
let (map, start) = parse_input(input);
let (_, visited) = check_map(&map, start);
Some(visited.len() as u32)
}

pub fn part_two(input: &str) -> Option<u32> {
let (map, start) = parse_input(input);
let (_, visited) = check_map(&map, start);
Some(
visited
.par_iter()
.filter(|pos| {
if pos.0 == start.0 && pos.1 == start.1 {
false;
}
let mut map = map.clone();
map[pos.0 as usize][pos.1 as usize] = '#';
let (looped, _) = check_map(&map, start);
looped
})
.count() as u32,
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(41));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(6));
}
}

0 comments on commit 7462f62

Please sign in to comment.