Compare commits

...

6 commits

13 changed files with 356 additions and 21 deletions

View file

@ -7,7 +7,9 @@ use std::{
use nom::{
bits::complete as bits,
multi::{many0, many_m_n}, error::ParseError, Offset,
error::ParseError,
multi::{many0, many_m_n},
Offset,
};
use nom::{combinator::map, sequence::pair};
use parsers::fold_till;
@ -68,7 +70,9 @@ impl PacketType {
}
}
fn parse_sub_packets<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, Vec<Packet>, E> {
fn parse_sub_packets<'a, E: ParseError<Input<'a>>>(
i: Input<'a>,
) -> IResult<'a, Vec<Packet>, E> {
enum LengthType {
Bits(usize),
Packets(usize),
@ -102,10 +106,7 @@ impl PacketType {
assert_eq!(i.0.offset(subpackets_end.0), i.0.offset(new_input.0));
assert_eq!(subpackets_end.1, new_input.1);
Ok((
new_input,
subpackets,
))
Ok((new_input, subpackets))
}
}
}
@ -199,7 +200,9 @@ fn main() {
.flatten()
.collect();
let packet = Packet::parse::<nom::error::Error<_>>((&bytes, 0)).unwrap().1;
let packet = Packet::parse::<nom::error::Error<_>>((&bytes, 0))
.unwrap()
.1;
println!("{}", packet.version_sum());
println!("{}", packet.typ.evaluate());
}

View file

@ -52,7 +52,7 @@ pub fn unscramble(line: &str) -> LineResult {
}
Some(c @ b'a'..=b'g') => {
freq += counts[c as usize - b'a' as usize];
},
}
Some(_) => {}
}
}

View file

@ -11,7 +11,7 @@ struct Sensor {
}
impl Sensor {
fn beacon_distance(self) -> usize {
fn beacon_distance(self) -> u32 {
self.closest_beacon.manhattan_dist(self.position)
}
}

View file

@ -30,7 +30,11 @@ fn main() {
.strip_prefix(b"[")
.and_then(|crate_| crate_.strip_suffix(b"]"))
else {
panic!("invalid crate {:?} {}", crate_, std::str::from_utf8(crate_).unwrap());
panic!(
"invalid crate {:?} {}",
crate_,
std::str::from_utf8(crate_).unwrap()
);
};
Some(*crate_ as char)

2
2023/data/day16.expected Normal file
View file

@ -0,0 +1,2 @@
6883
7228

110
2023/data/day16.input Normal file
View file

@ -0,0 +1,110 @@
\.........../../..\.\.\................|..-...\........./.......-.....................-...\...--....../..\....
........|............|............-.........|....\.................\-.........../...../....-../....\..........
.-....................\\..................|.\..........\.........../.............................-...-........
............-.....\/...........-........-......................-..-...../...............|................/....
.......\....-...-...............|.............-...................../....-../............./\..........-..\....
..................-.............\.........\........-......./..../..|....|......-/............\.............\..
-...............|.........\........../.\........../......./-..........-...-.....-............................|
.\\...\..........-............./...-|...................../.......-.................-....../.........|........
..................../........................./..\...................\.................-/......./.............
../...................|...../.-..................-.....|....-.|....................../.../\....-....|..\..|.\.
..................|..................................-....|.........\................................./.......
.........-...........-........\..|-...................--.............-.../......./....................\..-....
-..........-.................\.....|......|.\................|../..........\.........\.|.............-....\...
......./....-....|....-..........|........-...-....-.....-............................................/../....
.......\...........|.......\....\.//\.............\...../|....-...../..|....-................/................
.........\........./............\.-.|......./..........................|....\........-.-.............../..|-.|
.............................\......|....-../.....\.....-..../.........................\......|....../...\/...
.../.\.........../................................./.\.\...............\..............\.....|........-........
..................\......-......|..................\................................/.../.|...................
...\\............................\./................../............................-.......-..|...............
/.............\...-....\...//..-..\....-......-.\............../.....|../.........|..../........\......\......
./..........-.........\..-.................-.|...........\.......-.......|...................|../.............
.....-./.\..../.|.............../...........|..../....\...\/..-.........|....-.........|......................
...../.|.............../.....................\\../...|........\........./|-...........\/./.\\.................
..............\...-..............\.|......./........-...............-.-...|..\......-./..........-.......|../.
.....|...............\....|............../................./....../...-........-.....|........\.........||../|
.../................................../...............-......................................../.......-......
......../.........|..../...............|...../..|................./............|.../.....|....|..\........-...
.../................................//-.......\........./......../....-.....-....-............................
........../....................|................................./.-......|.....\........-..............|..|..
........|...-............|.\.../.......................\...\../...........|../\.\-..../|\..|-...\......\....-.
...-.\..../.....||.....\./.-.....\..............-.......\.../...-....|......\...........-.....-....../........
...|-...|..........\.........../.......................-....../...............\.......\.....|.................
....\..../\\..........-........\............................/...........|.........|......|.|...........|...|..
.............\............................../....|..\.\..../....................\..|...|..-............\....-.
.-/|./...\....-\....-...\...............-.../............-..../.--............................|||.............
....../../\\...|..-.................\.............../.|\/............................../.........\.....|......
..../....................\..................../......\............-//.........................................
.............\..../........../........-..............................|...-/....../..|....|.......-......../...
................-.|..\../...|.\..|..-.....-..../...................\..............\........................./.
........................../......\...............................-..../......................-...........-....
........\.-......................./...............\..............................\...../...................\..
..............................\..|..............|\.-..-....-..|..../.........\.............../.-..............
..........................\\./|.........\|\..............\......./.......-.-...................\.......-......
.............|.........-..\..................|......-..|.-|.................--..||............-.|.............
..............\..\.\.\.........-./....../\../|../.......................\-.....\....|.........................
.............................|.........\.....|..............................|.......././-.../.........-./....|
\.....................\.../..................................-......./........../..\.\.................|......
...............-......................../.....................................................................
..../........./........-.....|................/../....\.........../../.././........./......................-..
.......\................/............................................\........../..\.\......|/|\..............
...........\.............................\\......\\..|..-\................../.-.....................-.........
........|../..\...............|......./.................................|......|./....../......-..............
........./................\.............|...............-.........-.....\.............../.\..........|........
.........................|......./....................-......|........\.........................|/............
.\..-.......................................-....|.................................|\./.\.....................
\../................................-.-.....-..................../....../......../\...|..|......../.......\...
........................../..............\|............./..../.................../...\....\/..................
../-.........................\...\......\......................|.......\..\...\...\.................-..|....\.
....-...................\......-........../......../...............|................................/.........
.................................../......|.......|......................................|..\........-........
......|..|....-..................\./.............../.|...............|.|/.......\.\...........................
...............|...........|.....\................................................|................\.....|....
......../...|......../.................../../...........|...|............./.............\..|.../........-.....
.....................-............\...|..........|...-..........|..-..../...-...../......|....................
...\.......\.....-..................................|/.....\......|.....................|.......|......./.-...
.....................|........./...............|.-...........\........|......|...-/............../.-..........
......./....../.......-....-..............\../\.......\................................................|......
................../.......\\.......................|...\........|...\........-....\..-..........\...../..-....
...............................................-.........................\................\............../..\.
....|......../.........\.........-../....-....................\..................|............................
................./.-../\..........-.....|.........\.........\..../.............................|........--/...
................|./...............|.......\....-\........-.....................-.....|..................../-..
...........\........................|../...-./......\...............\........-./......|.................|..\..
.......|-....../...........|..........|............/.............../.|....../...................\.............
...../...../..........|.......................|....................|.........\|............................\..
........-........\......./....\.............//................................./......./...................|..
..-......../...................-...../.........-...................|..........................\....../.-......
./............................\...............................|............|...../.|................|.........
......\................|.............................-.........\.................................../-........\
./-/..........|.................................../........\....\.....\.....................\.\.........|.....
.-............../../........./.........-......\.....................\..\..\..\/......-./.../....|.............
.....|..................-...\||.......\./../........................-.........................-..........\....
...-................/.|........-....................................|..\....-...\......-..|............/......
..........\...............|.|...-.............-.././...................../.../................................
.......\./............./........../..\............\............-\......|......|....|.........../|.............
............\....|...........|...............-........\..|\..-.....-...|....................\..../.......-....
........\.....\...................|......./......-........./....\-...................|................\....-..
../......................../................-...|..........\........../...................................|.\.
.....................-..../......../........./........./.........\.......|/-...........|.........\...../...../
......//..................\.|....|..|..\...\-........./...........\............/..................|...-.....\.
.....-..-....../..................\....\........................|......................../....|........\..|.\.
.........-............\..............\..\.\../............................\..-...-................./-.........
....|./..........\..-.................\..\...../.\...................-................/............|...../....
...\.................../.....-...../.-.././....-/......|..................|................/.............-....
.....\.||.................\................\..|.....|.............\.\-.............-................./........
.......\..........\....................|................/..............|.../..................\...-.-.||......
.....-./..\.\.........................................|......./.............../....|..|.......-........\...\..
.....|............./.....-...................|.\..../-............\.........................-................\
........\/............./......\......|-...|................../.....|-......................./..............\-.
..............-..../.....-..........||.-.....-.../................././.../............\/................./../.
-.....................................|.........-.....|...................|..................../.../..........
.......................-..|................/...........\...........................|.--.....\..........|/..-..
...............\..................../..../.|..........................|.....................|..\...\....--....
....../.......-...............................|....../.........\.-........../.....................\.--...-....
.-......\............/...-........|.....\........../|........................|..........\...|....../-.........
/............................../.....................................\........../|........-...............\..-
.........\........\.|.|\..\.....-..............-.........-...........|........|...|...........-.............||
.-\................\.......|...................................../..................-.-.....|...\..\.|........
\............-/../.......|............./......\\.\./.|......-/.\.\.............\........./|\.....-............

View file

@ -0,0 +1,11 @@
[package]
name = "rust_2023_16"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../../../common/rust" }
enum-map = "2.7.3"
rayon = "1.8.0"

181
2023/day16/rust/src/main.rs Normal file
View file

@ -0,0 +1,181 @@
#![warn(clippy::pedantic)]
use std::{collections::HashMap, io::stdin};
use aoc::vec2::{Direction, Vec2};
use enum_map::EnumMap;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Device {
MirrorTopLeft,
MirrorTopRight,
SplitterHorizontal,
SplitterVertical,
}
impl Device {
pub fn get_outputs(self, input_direction: Direction) -> DeviceOutput {
use DeviceOutput::{One, Two};
match (self, input_direction) {
(Device::MirrorTopLeft, Direction::Up) | (Device::MirrorTopRight, Direction::Down) => {
One(Direction::Right)
}
(Device::MirrorTopLeft, Direction::Down) | (Device::MirrorTopRight, Direction::Up) => {
One(Direction::Left)
}
(Device::MirrorTopLeft, Direction::Left)
| (Device::MirrorTopRight, Direction::Right) => One(Direction::Down),
(Device::MirrorTopLeft, Direction::Right)
| (Device::MirrorTopRight, Direction::Left) => One(Direction::Up),
(Device::SplitterHorizontal, Direction::Up | Direction::Down) => {
Two(Direction::Left, Direction::Right)
}
(Device::SplitterVertical, Direction::Left | Direction::Right) => {
Two(Direction::Up, Direction::Down)
}
(Device::SplitterHorizontal, Direction::Left | Direction::Right)
| (Device::SplitterVertical, Direction::Up | Direction::Down) => One(input_direction),
}
}
pub fn parse(c: char) -> Option<Self> {
match c {
'/' => Some(Device::MirrorTopLeft),
'\\' => Some(Device::MirrorTopRight),
'-' => Some(Device::SplitterHorizontal),
'|' => Some(Device::SplitterVertical),
'.' => None,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Copy)]
enum DeviceOutput {
One(Direction),
Two(Direction, Direction),
}
#[derive(Debug, Clone, Copy)]
struct Field {
device: Option<Device>,
seen_beams: EnumMap<Direction, bool>,
}
#[derive(Debug, Clone, Copy)]
struct PathPosition {
pos: Vec2,
direction: Direction,
}
impl PathPosition {
pub fn advance_to(self, direction: Direction) -> Self {
Self {
pos: self.pos + Vec2::from(direction),
direction,
}
}
}
fn follow_path(start: PathPosition, fields: &mut HashMap<Vec2, Field>) {
let mut pos = start;
loop {
let Some(field) = fields.get_mut(&pos.pos) else {
return;
};
let seen = &mut field.seen_beams[pos.direction];
if *seen {
return;
}
*seen = true;
let outputs = match field.device {
Some(d) => d.get_outputs(pos.direction),
None => DeviceOutput::One(pos.direction),
};
match outputs {
DeviceOutput::One(d) => {
pos = pos.advance_to(d);
}
DeviceOutput::Two(d1, d2) => {
follow_path(pos.advance_to(d1), fields);
pos = pos.advance_to(d2);
}
}
}
}
fn energize_fields(start: PathPosition, mut fields: HashMap<Vec2, Field>) -> usize {
follow_path(start, &mut fields);
fields
.values()
.filter(|field| field.seen_beams.into_iter().any(|(_dir, seen)| seen))
.count()
}
fn main() {
let lines: Vec<_> = stdin().lines().map(Result::unwrap).collect();
let max_y = i32::try_from(lines.len()).unwrap() - 1;
let max_x = i32::try_from(lines[0].len()).unwrap() - 1;
let fields: HashMap<_, _> = lines
.iter()
.enumerate()
.flat_map(|(row, l)| {
l.chars().enumerate().map(move |(col, c)| {
let pos = Vec2::new(col.try_into().unwrap(), max_y - i32::try_from(row).unwrap());
let device = Device::parse(c);
(
pos,
Field {
device,
seen_beams: EnumMap::default(),
},
)
})
})
.collect();
println!(
"{}",
energize_fields(
PathPosition {
pos: Vec2::new(0, max_y),
direction: Direction::Right,
},
fields.clone()
),
);
let hor_starts = (0..=max_x).flat_map(|x| {
[
(Vec2::new(x, 0), Direction::Up),
(Vec2::new(x, max_y), Direction::Down),
]
});
let ver_starts = (0..=max_y).flat_map(|y| {
[
(Vec2::new(0, y), Direction::Right),
(Vec2::new(max_x - 1, y), Direction::Left),
]
});
let starts: Vec<_> = hor_starts.chain(ver_starts).collect();
let max_energized = starts
.into_par_iter()
.map(|(pos, direction)| energize_fields(PathPosition { pos, direction }, fields.clone()))
.max()
.unwrap();
println!("{max_energized}");
}

10
Cargo.lock generated
View file

@ -15,6 +15,7 @@ dependencies = [
name = "aoc"
version = "0.1.0"
dependencies = [
"enum-map",
"num-traits",
"strum 0.24.1",
"thiserror",
@ -702,6 +703,15 @@ dependencies = [
"aoc",
]
[[package]]
name = "rust_2023_16"
version = "0.1.0"
dependencies = [
"aoc",
"enum-map",
"rayon",
]
[[package]]
name = "rustversion"
version = "1.0.14"

View file

@ -37,4 +37,5 @@ members = [
"2023/day9/rust",
"2023/day11/rust",
"2023/day15/rust",
"2023/day16/rust",
]

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
enum-map = "2.7.3"
num-traits = "0.2.15"
strum = { version = "0.24.1", features = ["derive"] }
thiserror = "1.0.38"

View file

@ -1,9 +1,9 @@
#![warn(clippy::pedantic)]
pub mod grid;
mod section_range;
pub mod vec2;
pub mod vecn;
pub mod grid;
use std::{mem, path::Path};

View file

@ -4,6 +4,8 @@ use std::{
str::FromStr,
};
use enum_map::Enum;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Vec2 {
pub x: i32, // increases toward right
@ -24,22 +26,30 @@ impl Vec2 {
}
}
#[must_use]
pub fn map2(self, other: Vec2, mut f: impl FnMut(i32, i32) -> i32) -> Self {
Self {
x: f(self.x, other.x),
y: f(self.y, other.y),
}
}
#[must_use]
pub fn len(self) -> f64 {
(f64::from(self.x).powi(2) + f64::from(self.y).powi(2)).sqrt()
}
/// Calculates the manhattan distance between `self` and `other`.
#[must_use]
pub fn manhattan_dist(self, other: Vec2) -> usize {
let Vec2 { x, y } = (other - self).map(i32::abs);
let x = usize::try_from(x).unwrap();
let y = usize::try_from(y).unwrap();
pub fn manhattan_dist(self, other: Vec2) -> u32 {
let x = self.x.abs_diff(other.x);
let y = self.y.abs_diff(other.y);
x + y
}
#[must_use]
pub fn on_x_with_manhattan_dist(self, x: i32, dist: usize) -> Option<(Vec2, Vec2)> {
let dx = usize::try_from((x - self.x).abs()).ok()?;
pub fn on_x_with_manhattan_dist(self, x: i32, dist: u32) -> Option<(Vec2, Vec2)> {
let dx = x.abs_diff(self.x);
let dy = dist.checked_sub(dx)?;
let dy = i32::try_from(dy).ok()?;
@ -55,8 +65,8 @@ impl Vec2 {
}
#[must_use]
pub fn on_y_with_manhattan_dist(self, y: i32, dist: usize) -> Option<(Vec2, Vec2)> {
let dy = usize::try_from((y - self.y).abs()).ok()?;
pub fn on_y_with_manhattan_dist(self, y: i32, dist: u32) -> Option<(Vec2, Vec2)> {
let dy = y.abs_diff(self.y);
let dx = dist.checked_sub(dy)?;
let dx = i32::try_from(dx).ok()?;
@ -161,7 +171,7 @@ impl From<Direction> for Vec2 {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Enum)]
pub enum Direction {
Up,
Down,
@ -222,7 +232,9 @@ impl Iterator for LinePoints {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
let Some(line) = self.line.as_mut() else { return None };
let Some(line) = self.line.as_mut() else {
return None;
};
let delta = (line.end - line.start).map(i32::signum);