Implement 2023 day 10 part 1

This commit is contained in:
2023-12-10 10:38:16 +01:00
parent 126eeb7587
commit f1b23b0116
3 changed files with 106 additions and 4 deletions

View File

@@ -146,11 +146,9 @@ pub fn get_both<T>(slice: &mut [T], first: usize, second: usize) -> (&mut T, &mu
} }
} }
#[allow(unused)]
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct IndexSet(Vec<u32>); pub struct IndexSet(Vec<u32>);
#[allow(unused)]
impl IndexSet { impl IndexSet {
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self(Vec::with_capacity( Self(Vec::with_capacity(
@@ -278,6 +276,12 @@ impl<'a> Grid<'a> {
.chunks_exact(self.width) .chunks_exact(self.width)
.map(move |row| &row[..width]) .map(move |row| &row[..width])
} }
pub fn find(&self, c: u8) -> Option<(usize, usize)> {
let pos = self.data.iter().position(|&d| d == c)?;
Some((pos % self.width, pos / self.width))
}
} }
impl<'a> Index<usize> for Grid<'a> { impl<'a> Index<usize> for Grid<'a> {

View File

@@ -1,7 +1,100 @@
pub fn part1(_input: &[u8]) -> anyhow::Result<String> { use std::collections::VecDeque;
anyhow::bail!("Not implemented")
use anyhow::Context;
use crate::common::Grid;
use crate::common::IndexSet;
const LEFT: u8 = 1;
const RIGHT: u8 = 2;
const UP: u8 = 4;
const DOWN: u8 = 8;
fn get_connections(c: u8) -> u8 {
match c {
b'|' => UP | DOWN,
b'-' => LEFT | RIGHT,
b'F' => DOWN | RIGHT,
b'J' => LEFT | UP,
b'L' => RIGHT | UP,
b'7' => LEFT | DOWN,
_ => 0,
}
}
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
let map = Grid::new(input)?;
let (start_x, start_y) = map.find(b'S').context("Couldn't find starting point")?;
let mut visited = IndexSet::with_capacity(map.width() * map.height());
let mut todo = VecDeque::new();
visited.insert(start_y * map.width() + start_x);
if start_x > 0 && (get_connections(map[start_y][start_x - 1]) & RIGHT) != 0 {
todo.push_back((1, (start_x - 1, start_y)));
visited.insert(start_y * map.width() + start_x - 1);
}
if start_x + 1 < map.width() && (get_connections(map[start_y][start_x + 1]) & LEFT) != 0 {
todo.push_back((1, (start_x + 1, start_y)));
visited.insert(start_y * map.width() + start_x + 1);
}
if start_y > 0 && (get_connections(map[start_y - 1][start_x]) & DOWN) != 0 {
todo.push_back((1, (start_x, start_y - 1)));
visited.insert((start_y - 1) * map.width() + start_x);
}
if start_y + 1 < map.height() && (get_connections(map[start_y + 1][start_x]) & DOWN) != 0 {
todo.push_back((1, (start_x, start_y + 1)));
visited.insert((start_y + 1) * map.width() + start_x);
}
let mut max_dist = 1;
while let Some((dist, (x, y))) = todo.pop_front() {
let mut enqueue = |x, y| {
if visited.insert(y * map.width() + x) {
todo.push_back((dist + 1, (x, y)));
// Can elide comparison because we do a BFS and length is strictly increasing
max_dist = dist + 1;
}
};
let connections = get_connections(map[y][x]);
if (connections & LEFT) != 0 {
enqueue(x - 1, y);
}
if (connections & RIGHT) != 0 {
enqueue(x + 1, y);
}
if (connections & UP) != 0 {
enqueue(x, y - 1);
}
if (connections & DOWN) != 0 {
enqueue(x, y + 1);
}
}
Ok(max_dist.to_string())
} }
pub fn part2(_input: &[u8]) -> anyhow::Result<String> { pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Not implemented") anyhow::bail!("Not implemented")
} }
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &[u8] = include_bytes!("samples/10.txt");
#[test]
fn sample_part1() {
assert_eq!("8", part1(SAMPLE).unwrap());
}
}

5
2023/src/samples/10.txt Normal file
View File

@@ -0,0 +1,5 @@
..F7.
.FJ|.
SJ.L7
|F--J
LJ...