Skip to content

Commit

Permalink
add day23
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 23, 2023
1 parent a8b2896 commit fa76907
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 2023/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ members = [
"day20",
"day21",
# "day22",
# "day23",
"day23",
# "day24",
# "day25",
]
2 changes: 0 additions & 2 deletions 2023/day15/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{collections::HashMap, ops::Index};

use indexmap::IndexMap;

fn hash(input: &str) -> usize {
Expand Down
19 changes: 19 additions & 0 deletions 2023/day23/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
input.txt
flamegraph.svg
perf.data*
### Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

10 changes: 10 additions & 0 deletions 2023/day23/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "day23"
authors = ["mirsella <[email protected]>"]
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
grid = "0.13.0"
28 changes: 28 additions & 0 deletions 2023/day23/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
use std::{env, fs};

fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> {
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
if contents != new_contents {
println!("Updating {}", file_path.display());
fs::write(file_path, new_contents)?;
}
Ok(())
}

fn main() -> io::Result<()> {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
replace_in_file(
&"../Cargo.toml".into(),
&format!("# \"{pkg_name}\""),
&format!("\"{pkg_name}\""),
)?;

replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?;

Ok(())
}
93 changes: 93 additions & 0 deletions 2023/day23/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use grid::Grid;

const DIRECTIONS: [(isize, isize, char); 4] =
[(0, 1, 'v'), (1, 0, '>'), (0, -1, '^'), (-1, 0, '<')];

fn dfs(grid: &mut Grid<char>, (x, y): (isize, isize), path: usize, paths: &mut Vec<usize>) {
// found the end
if y == grid.rows() as isize - 1 {
paths.push(path);
return;
}

for (dx, dy, c) in DIRECTIONS.iter() {
let nx = x + dx;
let ny = y + dy;
match grid.get(ny, nx).copied() {
Some(before) if before == '.' || before == *c => {
// print_grid(grid);
*grid.get_mut(ny, nx).unwrap() = 'O';
dfs(grid, (nx, ny), path + 1, paths);
*grid.get_mut(ny, nx).unwrap() = before;
}
_ => (),
}
}
}

fn part1(input: &str) -> usize {
let mut grid = Grid::from_vec(
input.replace('\n', "").chars().collect(),
input.find('\n').unwrap(),
);
let start_x = grid.iter_row(0).position(|&c| c == '.').unwrap();
let mut result = Vec::new();
dfs(&mut grid, (0, start_x as isize), 0, &mut result);
*result.iter().max().unwrap()
}

fn part2(input: &str) -> usize {
let mut grid = Grid::from_vec(
input
.replace('\n', "")
.replace(['<', '>', '^', 'v'], ".")
.chars()
.collect(),
input.find('\n').unwrap(),
);
let start_x = grid.iter_row(0).position(|&c| c == '.').unwrap();
let mut result = Vec::new();
dfs(&mut grid, (0, start_x as isize), 0, &mut result);
*result.iter().max().unwrap()
}

fn main() {
let input = include_str!("../input.txt");
println!("Part 1: {}", part1(input));
println!("Part 2: {}", part2(input));
}

#[cfg(test)]
mod tests {
const INPUT: &str = "#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#";
#[test]
fn part1() {
assert_eq!(super::part1(INPUT), 94);
}
#[test]
fn part2() {
assert_eq!(super::part2(INPUT), 154);
}
}

0 comments on commit fa76907

Please sign in to comment.