Skip to content

Commit

Permalink
Miscellaneous improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 27, 2023
1 parent 1b160fb commit eab7917
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 153 deletions.
39 changes: 19 additions & 20 deletions rs/src/day10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ enum Direction {
}

#[dynamic]
static LUT: BTreeMap<(Direction, char), Direction> = [
((Direction::U, '|'), Direction::U),
((Direction::U, '7'), Direction::L),
((Direction::U, 'F'), Direction::R),
((Direction::L, '-'), Direction::L),
((Direction::L, 'F'), Direction::D),
((Direction::L, 'L'), Direction::U),
((Direction::D, '|'), Direction::D),
((Direction::D, 'L'), Direction::R),
((Direction::D, 'J'), Direction::L),
((Direction::R, '-'), Direction::R),
((Direction::R, 'J'), Direction::U),
((Direction::R, '7'), Direction::D),
static LUT: BTreeMap<(Direction, u8), Direction> = [
((Direction::U, b'|'), Direction::U),
((Direction::U, b'7'), Direction::L),
((Direction::U, b'F'), Direction::R),
((Direction::L, b'-'), Direction::L),
((Direction::L, b'F'), Direction::D),
((Direction::L, b'L'), Direction::U),
((Direction::D, b'|'), Direction::D),
((Direction::D, b'L'), Direction::R),
((Direction::D, b'J'), Direction::L),
((Direction::R, b'-'), Direction::R),
((Direction::R, b'J'), Direction::U),
((Direction::R, b'7'), Direction::D),
]
.into();

Expand All @@ -36,19 +36,18 @@ fn step((y, x): (usize, usize), dir: Direction) -> Option<(usize, usize)> {
})
}

fn part1_helper(maze: &[&str]) -> Option<Vec<(usize, usize)>> {
fn part1_helper(maze: &[&[u8]]) -> Option<Vec<(usize, usize)>> {
for (y, line) in maze.iter().enumerate() {
for (x, char) in line.char_indices() {
if char != 'S' {
for (x, char) in line.iter().enumerate() {
if *char != b'S' {
continue;
}
let start_pos = (y, x);
for mut dir in [Direction::U, Direction::L, Direction::D, Direction::R] {
let mut pos = start_pos;
let mut path = iter::from_fn(|| {
pos = step(pos, dir)?;
dir =
*LUT.get(&(dir, maze[pos.0..].iter().next()?[pos.1..].chars().next()?))?;
dir = *LUT.get(&(dir, maze[pos.0][pos.1]))?;
Some(pos)
})
.collect::<Vec<_>>();
Expand All @@ -63,11 +62,11 @@ fn part1_helper(maze: &[&str]) -> Option<Vec<(usize, usize)>> {
}

pub fn part1(data: &str) -> Option<usize> {
Some(part1_helper(&data.lines().collect::<Vec<_>>())?.len() / 2)
Some(part1_helper(&data.lines().map(|line| line.as_bytes()).collect::<Vec<_>>())?.len() / 2)
}

pub fn part2(data: &str) -> Option<usize> {
let path = part1_helper(&data.lines().collect::<Vec<_>>())?;
let path = part1_helper(&data.lines().map(|line| line.as_bytes()).collect::<Vec<_>>())?;
let (n, m) = path
.iter()
.zip(path[1..].iter().chain(path.iter()))
Expand Down
32 changes: 14 additions & 18 deletions rs/src/day11.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
fn solve(data: &str, n: usize) -> usize {
let lines = data.lines().collect::<Vec<_>>();
let mut counts = Vec::<usize>::new();
solve1(
&lines
.iter()
.map(|line| line.chars().filter(|&c| c == '#').count())
.collect::<Vec<_>>(),
n,
) + solve1(
&(0..lines
.iter()
.map(|line| line.len())
.max()
.unwrap_or_default())
.map(|x| {
lines
.iter()
.filter(|line| line[x..].starts_with('#'))
.count()
&data
.lines()
.map(|line| {
let mut count = 0;
counts.extend(std::iter::repeat(0).take(line.len()).skip(counts.len()));
for (i, c) in line.char_indices() {
if c == '#' {
counts[i] += 1;
count += 1;
}
}
count
})
.collect::<Vec<_>>(),
n,
)
) + solve1(&counts, n)
}

fn solve1(data: &[usize], n: usize) -> usize {
Expand Down
21 changes: 11 additions & 10 deletions rs/src/day12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ fn solve<const N: usize>(line: &str) -> Option<usize> {
.map(|x| x.parse::<usize>().ok())
.collect::<Option<Vec<_>>>()?;
let string = Itertools::intersperse([&lhs; N].into_iter(), &"?")
.flat_map(|s| s.chars())
.flat_map(|s| s.as_bytes())
.copied()
.collect::<Vec<_>>();
let runs = [&rhs; N].into_iter().flatten().copied().collect::<Vec<_>>();
let last_run = runs.last()?;
let counts = runs.iter().rev().skip(1).fold(
(0..string.len())
.map(|i| {
if i + last_run > string.len()
|| i != 0 && string[i - 1] == '#'
|| string[i..i + last_run].iter().any(|&c| c == '.')
|| string[i + last_run..].iter().any(|&c| c == '#')
|| i != 0 && string[i - 1] == b'#'
|| string[i..i + last_run].iter().any(|&c| c == b'.')
|| string[i + last_run..].iter().any(|&c| c == b'#')
{
0
} else {
Expand All @@ -30,9 +31,9 @@ fn solve<const N: usize>(line: &str) -> Option<usize> {
(0..string.len())
.map(|i| {
if i + run >= string.len()
|| i != 0 && string[i - 1] == '#'
|| string[i..i + run].iter().any(|&c| c == '.')
|| string[i + run] == '#'
|| i != 0 && string[i - 1] == b'#'
|| string[i..i + run].iter().any(|&c| c == b'.')
|| string[i + run] == b'#'
{
0
} else {
Expand All @@ -41,8 +42,8 @@ fn solve<const N: usize>(line: &str) -> Option<usize> {
.zip(
string[i + run + 1..]
.iter()
.take_while(|&&c| c != '#')
.chain([&'#']),
.take_while(|&&c| c != b'#')
.chain([&b'#']),
)
.map(|(&count, _)| count)
.sum()
Expand All @@ -54,7 +55,7 @@ fn solve<const N: usize>(line: &str) -> Option<usize> {
Some(
counts
.into_iter()
.zip(string.into_iter().take_while(|&c| c != '#').chain(['#']))
.zip(string.into_iter().take_while(|&c| c != b'#').chain([b'#']))
.map(|(count, _)| count)
.sum(),
)
Expand Down
41 changes: 23 additions & 18 deletions rs/src/day13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ fn find_reflection<T: Eq, F: Fn(&[T], &[T]) -> bool>(data: &[T], eq: F) -> usize
.unwrap_or_default()
}

fn solve<F: Fn(&[&str], &[&str]) -> bool>(lines: &[&str], eq: F) -> usize {
fn solve<F: Fn(&[&[u8]], &[&[u8]]) -> bool>(lines: &[&[u8]], eq: F) -> usize {
let width = lines
.iter()
.map(|line| line.len())
.max()
.unwrap_or_default();
let transpose = (0..width)
.map(|i| {
lines
.iter()
.flat_map(|line| line[i..i + 1].chars())
.collect::<String>()
})
.map(|i| lines.iter().map(|line| line[i]).collect::<Vec<_>>())
.collect::<Vec<_>>();
let transpose = transpose.iter().map(|line| &line[..]).collect::<Vec<_>>();

Expand All @@ -26,24 +21,34 @@ fn solve<F: Fn(&[&str], &[&str]) -> bool>(lines: &[&str], eq: F) -> usize {
pub fn part1(data: &str) -> usize {
data.split("\n\n")
.map(|group| {
solve(&group.lines().collect::<Vec<_>>(), |x, y| {
x.iter().rev().zip(y.iter()).all(|(a, b)| a == b)
})
solve(
&group
.lines()
.map(|line| line.as_bytes())
.collect::<Vec<_>>(),
|x, y| x.iter().rev().zip(y.iter()).all(|(a, b)| a == b),
)
})
.sum()
}

pub fn part2(data: &str) -> usize {
data.split("\n\n")
.map(|group| {
solve(&group.lines().collect::<Vec<_>>(), |x, y| {
let mut iter = x
.iter()
.rev()
.zip(y.iter())
.flat_map(|(a, b)| a.chars().zip(b.chars()).filter(|(c, d)| c != d));
iter.next().is_some() && iter.next().is_none()
})
solve(
&group
.lines()
.map(|line| line.as_bytes())
.collect::<Vec<_>>(),
|x, y| {
let mut iter = x
.iter()
.rev()
.zip(y.iter())
.flat_map(|(a, b)| a.iter().zip(b.iter()).filter(|(c, d)| c != d));
iter.next().is_some() && iter.next().is_none()
},
)
})
.sum()
}
Expand Down
42 changes: 22 additions & 20 deletions rs/src/day16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ enum Direction {
}

#[dynamic]
static LUT: BTreeMap<(Direction, char), &'static [Direction]> = {
static LUT: BTreeMap<(Direction, u8), &'static [Direction]> = {
static DIRECTIONS: [Direction; 4] = [Direction::U, Direction::D, Direction::L, Direction::R];
[
((Direction::U, '/'), &DIRECTIONS[3..4]),
((Direction::U, '\\'), &DIRECTIONS[2..3]),
((Direction::U, '-'), &DIRECTIONS[2..4]),
((Direction::L, '/'), &DIRECTIONS[1..2]),
((Direction::L, '\\'), &DIRECTIONS[0..1]),
((Direction::L, '|'), &DIRECTIONS[0..2]),
((Direction::D, '/'), &DIRECTIONS[2..3]),
((Direction::D, '\\'), &DIRECTIONS[3..4]),
((Direction::D, '-'), &DIRECTIONS[2..4]),
((Direction::R, '/'), &DIRECTIONS[0..1]),
((Direction::R, '\\'), &DIRECTIONS[1..2]),
((Direction::R, '|'), &DIRECTIONS[0..2]),
((Direction::U, b'/'), &DIRECTIONS[3..4]),
((Direction::U, b'\\'), &DIRECTIONS[2..3]),
((Direction::U, b'-'), &DIRECTIONS[2..4]),
((Direction::L, b'/'), &DIRECTIONS[1..2]),
((Direction::L, b'\\'), &DIRECTIONS[0..1]),
((Direction::L, b'|'), &DIRECTIONS[0..2]),
((Direction::D, b'/'), &DIRECTIONS[2..3]),
((Direction::D, b'\\'), &DIRECTIONS[3..4]),
((Direction::D, b'-'), &DIRECTIONS[2..4]),
((Direction::R, b'/'), &DIRECTIONS[0..1]),
((Direction::R, b'\\'), &DIRECTIONS[1..2]),
((Direction::R, b'|'), &DIRECTIONS[0..2]),
]
.into()
};
Expand All @@ -40,14 +40,11 @@ fn step(y: usize, x: usize, dir: Direction) -> Option<(usize, usize)> {
})
}

fn fill(data: &[&str], y: usize, x: usize, d: Direction) -> Option<usize> {
fn fill(data: &[&[u8]], y: usize, x: usize, d: Direction) -> Option<usize> {
let mut stack = vec![(y, x, d)];
let mut visited = stack.iter().copied().collect::<BTreeSet<_>>();
while let Some((y, x, d)) = stack.pop() {
for &d in *LUT
.get(&(d, data[y][x..].chars().next()?))
.unwrap_or(&&[d][..])
{
for &d in *LUT.get(&(d, data[y][x])).unwrap_or(&&[d][..]) {
let Some((y, x)) = step(y, x, d) else {
continue;
};
Expand All @@ -66,11 +63,16 @@ fn fill(data: &[&str], y: usize, x: usize, d: Direction) -> Option<usize> {
}

pub fn part1(data: &str) -> Option<usize> {
fill(&data.lines().collect::<Vec<_>>(), 0, 0, Direction::R)
fill(
&data.lines().map(|line| line.as_bytes()).collect::<Vec<_>>(),
0,
0,
Direction::R,
)
}

pub fn part2(data: &str) -> Option<usize> {
let data = data.lines().collect::<Vec<_>>();
let data = data.lines().map(|line| line.as_bytes()).collect::<Vec<_>>();
(0..data.len())
.map(|y| (y, 0, Direction::R))
.chain((0..data.first()?.len()).map(|x| (0, x, Direction::D)))
Expand Down
2 changes: 1 addition & 1 deletion rs/src/day17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
P: Fn(usize) -> bool,
F: Fn(Direction, usize) -> T,
T: IntoIterator,
<T as IntoIterator>::Item: Into<Direction>,
T::Item: Into<Direction>,
{
let maze = data
.lines()
Expand Down
Loading

0 comments on commit eab7917

Please sign in to comment.