generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad76ed1
commit 7462f62
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |