Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ziegfried committed Dec 16, 2023
1 parent cffe1a2 commit 19fe5be
Showing 1 changed file with 50 additions and 99 deletions.
149 changes: 50 additions & 99 deletions 2023/day16/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Problem: https://adventofcode.com/2023/day/16

use std::collections::{HashMap, HashSet};
use itertools::Itertools;
use std::collections::{HashMap, HashSet, VecDeque};

type Result = usize;

Expand Down Expand Up @@ -31,124 +31,77 @@ enum Dir {
W,
}
use Dir::*;
impl Dir {
fn mv(&self, (r, c): &Point) -> Point {
match self {
N => (r - 1, *c),
E => (*r, c + 1),
S => (r + 1, *c),
W => (*r, c - 1),
}

fn step((r, c): &Point, dir: Dir) -> Point {
match dir {
N => (r - 1, *c),
E => (*r, c + 1),
S => (r + 1, *c),
W => (*r, c - 1),
}
}

// ------------------------------------------

fn part1(input: &Input) -> Result {
p1(input, ((0, 0), E))
fn redirect(ch: char, dir: Dir) -> Vec<Dir> {
match ch {
'.' => vec![dir],
'|' => match dir {
N | S => vec![dir],
E | W => vec![N, S],
},
'-' => match dir {
N | S => vec![E, W],
E | W => vec![dir],
},
'\\' => vec![match dir {
N => W,
E => S,
S => E,
W => N,
}],
'/' => vec![match dir {
N => E,
E => N,
S => W,
W => S,
}],
_ => panic!("{}", ch),
}
}

fn p1(input: &Input, start: (Point, Dir)) -> Result {
// let mut cur_pos = (0, 0);
// let mut cur_dir = E;
fn energized_particles(input: &Input, start: (Point, Dir)) -> usize {
let mut seen = HashSet::new();
// seen.insert((cur_pos, cur_dir));

let mut queue = vec![start];

while let Some((point, dir)) = queue.pop() {
if seen.contains(&(point, dir)) {
continue;
}
if !input.contains_key(&point) {
continue;
}
let mut queue = VecDeque::from(vec![start]);
while let Some((point, dir)) = queue.pop_front() {
seen.insert((point, dir));
// let next = dir.mv(&point);
let ch = input[&point];

match ch {
'.' => {
queue.push((dir.mv(&point), dir));
}
'\\' => {
let nd = match dir {
N => W,
E => S,
S => E,
W => N,
};
queue.push((nd.mv(&point), nd));
}
'/' => {
let nd = match dir {
N => E,
E => N,
S => W,
W => S,
};
queue.push((nd.mv(&point), nd));
}
'|' => match dir {
N | S => {
queue.push((dir.mv(&point), dir));
}
E | W => {
queue.push((N.mv(&point), N));
queue.push((S.mv(&point), S));
}
},
'-' => match dir {
E | W => {
queue.push((dir.mv(&point), dir));
}
N | S => {
queue.push((E.mv(&point), E));
queue.push((W.mv(&point), W));
}
},
_ => {
panic!("{}", ch);
for nd in redirect(input[&point], dir) {
let next = step(&point, nd);
if input.contains_key(&next) && !seen.contains(&(next, nd)) {
queue.push_back((next, nd));
}
}
}

seen.iter().map(|(pos, _)| pos).unique().count()
}

fn part1(input: &Input) -> Result {
energized_particles(input, ((0, 0), E))
}

#[test]
fn test_part1() {
let input = parse_input(include_str!("test.txt"));
assert_eq!(part1(&input), 46);
}

// ------------------------------------------

fn part2(input: &Input) -> Result {
let max_row = *input.keys().map(|(r, _)| r).max().unwrap();
let max_col = *input.keys().map(|(_, c)| c).max().unwrap();
let mut mx = 0;
for c in 0..=max_col {
let r = p1(input, ((0, c), S));
if r > mx {
mx = r
};
let r = p1(input, ((max_row , c), N));
if r > mx {
mx = r
};
}
for r in 0..=max_row {
let v = p1(input, ((r, 0), E));
if v > mx {
mx = v
};
let v = p1(input, ((r, max_col), W));
if v > mx {
mx = v
};
}
mx
(0..=max_col)
.flat_map(|c| vec![((0, c), S), ((max_row, c), N)])
.chain((0..=max_row).flat_map(|r| vec![((r, 0), E), ((r, max_col), W)]))
.map(|point| energized_particles(input, point))
.max()
.unwrap()
}

#[test]
Expand All @@ -157,8 +110,6 @@ fn test_part2() {
assert_eq!(part2(&input), 51);
}

// ------------------------------------------

fn main() {
let input = parse_input(include_str!("input.txt"));
println!("Part 1: {:?}", part1(&input));
Expand Down

0 comments on commit 19fe5be

Please sign in to comment.