Skip to content

Commit

Permalink
day 23: done for now
Browse files Browse the repository at this point in the history
  • Loading branch information
livexia committed Dec 23, 2023
1 parent 1e0a24f commit 94a7630
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions aoc23/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::error::Error;
use std::io::{self, Read, Write};
use std::time::Instant;
Expand All @@ -22,17 +22,17 @@ fn parse_input<T: AsRef<str>>(input: T) -> Vec<Vec<char>> {

fn neighbors(pos: Coord, trails: &[Vec<char>], part2: bool) -> Vec<Coord> {
let (x, y) = pos;
let possible = if !part2 {
let possible: &[_] = if !part2 {
match trails[x][y] {
'.' => vec![(-1, 0), (1, 0), (0, -1), (0, 1)],
'>' => vec![(0, 1)],
'<' => vec![(0, -1)],
'^' => vec![(-1, 0)],
'v' => vec![(1, 0)],
'.' => &[(-1, 0), (1, 0), (0, -1), (0, 1)],
'>' => &[(0, 1)],
'<' => &[(0, -1)],
'^' => &[(-1, 0)],
'v' => &[(1, 0)],
_ => return vec![],
}
} else if trails[x][y] != '#' {
vec![(-1, 0), (1, 0), (0, -1), (0, 1)]
&[(-1, 0), (1, 0), (0, -1), (0, 1)]
} else {
return vec![];
};
Expand All @@ -55,7 +55,7 @@ fn neighbors(pos: Coord, trails: &[Vec<char>], part2: bool) -> Vec<Coord> {
fn dfs_grid(
pos: Coord,
trails: &[Vec<char>],
visited: &mut HashSet<Coord>,
visited: &mut [Vec<bool>],
part2: bool,
) -> Option<usize> {
if pos.0 == trails.len() - 1 {
Expand All @@ -67,11 +67,12 @@ fn dfs_grid(
} else {
let mut count = 0;
for next in neighbors(pos, trails, part2) {
if visited.insert(next) {
if !visited[next.0][next.1] {
visited[next.0][next.1] = true;
if let Some(remain) = dfs_grid(next, trails, visited, part2) {
count = count.max(1 + remain);
}
visited.remove(&next);
visited[next.0][next.1] = false;
}
}
if count == 0 {
Expand Down Expand Up @@ -117,18 +118,18 @@ fn dfs_graph(
pos: Coord,
end: Coord,
graph: &HashMap<Coord, Vec<(Coord, usize)>>,
visited: &mut HashSet<Coord>,
visited: &mut [Vec<bool>],
) -> Option<usize> {
if pos == end {
return Some(0);
}
let neighbors = graph.get(&pos).unwrap();
neighbors
graph[&pos]
.iter()
.filter_map(|&(next, d)| {
if visited.insert(next) {
if !visited[next.0][next.1] {
visited[next.0][next.1] = true;
let d = dfs_graph(next, end, graph, visited).map(|r| r + d);
visited.remove(&next);
visited[next.0][next.1] = false;
d
} else {
None
Expand All @@ -144,14 +145,41 @@ fn part1(trails: &[Vec<char>]) -> Result<usize> {
0,
(0..trails[0].len()).find(|&y| trails[0][y] == '.').unwrap(),
);
let result = dfs_grid(start, trails, &mut HashSet::new(), false).unwrap();
let result = dfs_grid(
start,
trails,
&mut vec![vec![false; trails[0].len()]; trails.len()],
false,
)
.unwrap();

writeln!(io::stdout(), "Part 1: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}

fn part2(trails: &[Vec<char>]) -> Result<usize> {
#[allow(dead_code)]
fn part2_grid(trails: &[Vec<char>]) -> Result<usize> {
let _start = Instant::now();

let start = (
0,
(0..trails[0].len()).find(|&y| trails[0][y] == '.').unwrap(),
);
let result = dfs_grid(
start,
trails,
&mut vec![vec![false; trails[0].len()]; trails.len()],
true,
)
.unwrap();

writeln!(io::stdout(), "Part 2 with grid: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}

fn part2_graph(trails: &[Vec<char>]) -> Result<usize> {
let _start = Instant::now();

let start = (
Expand All @@ -166,9 +194,15 @@ fn part2(trails: &[Vec<char>]) -> Result<usize> {
);
let mut graph = gen_graph(trails, true);
prune_graph(&mut graph);
let result = dfs_graph(start, end, &graph, &mut HashSet::new()).unwrap();
let result = dfs_graph(
start,
end,
&graph,
&mut vec![vec![false; trails[0].len()]; trails.len()],
)
.unwrap();

writeln!(io::stdout(), "Part 2: {result}")?;
writeln!(io::stdout(), "Part 2 with graph: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}
Expand All @@ -179,7 +213,8 @@ fn main() -> Result<()> {

let trails = parse_input(input);
part1(&trails)?;
part2(&trails)?;
part2_graph(&trails)?;
// part2_grid(&trails)?;
Ok(())
}

Expand Down Expand Up @@ -210,14 +245,15 @@ fn example_input() {
#####################.#";
let trails = parse_input(input);
assert_eq!(part1(&trails).unwrap(), 94);
assert_eq!(part2(&trails).unwrap(), 154);
assert_eq!(part2_graph(&trails).unwrap(), 154);
assert_eq!(part2_grid(&trails).unwrap(), 154);
}

#[test]
fn real_input() {
let input = std::fs::read_to_string("input/input.txt").unwrap();
let trails = parse_input(input);
assert_eq!(part1(&trails).unwrap(), 2394);
assert_eq!(part2(&trails).unwrap(), 6554);
assert_eq!(2, 2);
assert_eq!(part2_graph(&trails).unwrap(), 6554);
// assert_eq!(part2_grid(&trails).unwrap(), 6554);
}

0 comments on commit 94a7630

Please sign in to comment.