Skip to content

Commit

Permalink
Day 6 - Slow
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhurt committed Dec 6, 2024
1 parent 5a3c1b5 commit d42a217
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `39.0µs` | `49.2µs` |
| [Day 2](./src/bin/02.rs) | `167.5µs` | `200.6µs` |
| [Day 3](./src/bin/03.rs) | `31.5µs` | `35.2µs` |
| [Day 4](./src/bin/04.rs) | `1.3ms` | `888.7µs` |
| [Day 5](./src/bin/05.rs) | `78.1µs` | `164.6µs` |

**Total: 2.95ms**
| [Day 1](./src/bin/01.rs) | `37.4µs` | `51.0µs` |
| [Day 2](./src/bin/02.rs) | `166.2µs` | `200.4µs` |
| [Day 3](./src/bin/03.rs) | `34.6µs` | `33.7µs` |
| [Day 4](./src/bin/04.rs) | `1.4ms` | `859.1µs` |
| [Day 5](./src/bin/05.rs) | `75.8µs` | `166.7µs` |
| [Day 6](./src/bin/06.rs) | `239.2µs` | `648.2ms` |

**Total: 651.46ms**
<!--- benchmarking table --->

---
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 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
104 changes: 104 additions & 0 deletions src/bin/06.rs
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));
}
}
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ impl Compass {
D::S => D::N,
}
}

pub fn turn_right(&self) -> Self {
use Compass as D;
match self {
D::E => D::S,
D::N => D::E,
D::W => D::N,
D::S => D::W,
}
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down

0 comments on commit d42a217

Please sign in to comment.