-
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
Showing
4 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
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,104 @@ | ||
use std::collections::HashSet; | ||
|
||
use advent_of_code::{Compass, Grid}; | ||
|
||
advent_of_code::solution!(6); | ||
|
||
fn get_seen_positions(start: usize, grid: &Grid<char>) -> HashSet<usize> { | ||
let mut seen = HashSet::new(); | ||
let mut d = Compass::N; | ||
let mut p = start; | ||
|
||
seen.insert(p); | ||
|
||
while let Some(next) = grid.step_from_index(p, d) { | ||
if grid.data[next] == '#' { | ||
d = d.turn_right(); | ||
} else { | ||
p = next; | ||
seen.insert(p); | ||
} | ||
} | ||
|
||
seen | ||
} | ||
|
||
pub fn part_one(input: &str) -> Option<usize> { | ||
let grid: Grid<char> = Grid::parse_lines(input); | ||
|
||
let start = grid | ||
.data | ||
.iter() | ||
.enumerate() | ||
.find_map(|(i, c)| (*c == '^').then_some(i)) | ||
.unwrap(); | ||
|
||
Some(get_seen_positions(start, &grid).len()) | ||
} | ||
|
||
fn has_cycle(start: usize, grid: &Grid<char>) -> bool { | ||
let mut d = Compass::N; | ||
let mut p = start; | ||
|
||
let mut seen = HashSet::new(); | ||
seen.insert((p, d)); | ||
|
||
while let Some(next) = grid.step_from_index(p, d) { | ||
if grid.data[next] == '#' { | ||
d = d.turn_right(); | ||
} else { | ||
p = next; | ||
if !seen.insert((p, d)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
pub fn part_two(input: &str) -> Option<usize> { | ||
let mut grid: Grid<char> = Grid::parse_lines(input); | ||
let start = grid | ||
.data | ||
.iter() | ||
.enumerate() | ||
.find_map(|(i, c)| (*c == '^').then_some(i)) | ||
.unwrap(); | ||
|
||
let possible_positions = get_seen_positions(start, &grid); | ||
|
||
let result = possible_positions | ||
.into_iter() | ||
.filter(|ob| { | ||
grid.data[*ob] = '#'; | ||
|
||
let result = has_cycle(start, &grid); | ||
|
||
grid.data[*ob] = '.'; | ||
|
||
result | ||
}) | ||
.count(); | ||
|
||
Some(result) | ||
} | ||
|
||
#[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)); | ||
} | ||
} |
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