8 Commits

Author SHA1 Message Date
edd14a0e3d Update to Clap 3.0.0! 2022-01-02 23:06:32 +01:00
4d7188e1ff Replace hashset with bitset 2022-01-02 22:38:28 +01:00
255edaca79 Implement day 24 2022-01-02 21:47:07 +01:00
8ea716cba8 Properly use TryFrom 2022-01-02 18:49:25 +01:00
601de2c565 Readability 2022-01-02 18:30:13 +01:00
894524bc81 Implement part 2
Turns out you can incorrectly implement the problem and still get the
right answer for part 1. If you then correct it, it's a lot faster.
2022-01-02 18:28:04 +01:00
f19bf28f34 Properly implemented A* estimate 2022-01-02 16:17:01 +01:00
de3a24a87c Implementation day 23 2022-01-02 16:17:01 +01:00
8 changed files with 699 additions and 43 deletions

View File

@@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
clap = { version = "3.0.0-rc.0", features = ["derive"] }
clap = { version = "3", features = ["derive"] }
itertools = "0.10"
nom = "7"

View File

@@ -90,7 +90,49 @@ where
let mut buffer = Vec::new();
input.read_to_end(&mut buffer).unwrap();
let (_, output) = parser(&buffer).finish().unwrap();
output
match parser(&buffer).finish() {
Ok((_, output)) => output,
Err(err) => {
panic!(
"Failed to parse input with error {:?} at \"{}\"",
err.code,
String::from_utf8_lossy(err.input)
);
}
}
}
pub struct BitSet {
buffer: Vec<u32>,
}
impl BitSet {
pub fn new() -> Self {
Self::with_capacity(0)
}
pub fn with_capacity(capacity: usize) -> Self {
let buffer = Vec::with_capacity(capacity);
Self { buffer }
}
pub fn insert(&mut self, value: usize) -> bool {
let chunk = value / 32;
let bit = 1 << (31 - (value % 32));
if self.buffer.len() <= chunk + 1 {
self.buffer.resize(chunk + 1, 0);
}
let not_present = self.buffer[chunk] & bit;
self.buffer[chunk] |= bit;
not_present == 0
}
pub fn len(&self) -> usize {
self.buffer.iter().map(|c| c.count_ones() as usize).sum()
}
}

View File

@@ -1,69 +1,91 @@
use std::collections::HashMap;
use std::io::Read;
use std::iter::repeat;
use nom::bytes::complete::tag;
use nom::sequence::tuple;
use nom::Finish;
use nom::character::complete::newline;
use nom::combinator::map;
use nom::multi::separated_list1;
use nom::sequence::separated_pair;
use nom::IResult;
use crate::common::ordered;
use crate::common::LineIter;
use crate::common::read_input;
use crate::common::BitSet;
type Coord = (u16, u16);
fn coordinates(input: &str) -> IResult<&str, Coord> {
use nom::character::complete;
fn coordinates(input: &[u8]) -> IResult<&[u8], Coord> {
use nom::character::complete::char;
use nom::character::complete::u16;
let (input, (x, _, y)) = tuple((complete::u16, complete::char(','), complete::u16))(input)?;
Ok((input, (x, y)))
separated_pair(u16, char(','), u16)(input)
}
fn line_definition(input: &str) -> IResult<&str, (Coord, Coord)> {
let (input, (begin, _, end)) = tuple((coordinates, tag(" -> "), coordinates))(input)?;
fn parse_input(input: &[u8]) -> IResult<&[u8], Vec<(Coord, Coord)>> {
let read_line = map(
separated_pair(coordinates, tag(" -> "), coordinates),
|(begin, end)| ordered(begin, end),
);
// Sorting the coordinates saves trouble later
Ok((input, ordered(begin, end)))
separated_list1(newline, read_line)(input)
}
fn stripe(
map: &mut HashMap<Coord, u16>,
once: &mut BitSet,
twice: &mut BitSet,
width: usize,
xs: impl Iterator<Item = u16>,
ys: impl Iterator<Item = u16>,
) {
for (x, y) in xs.zip(ys) {
*map.entry((x, y)).or_default() += 1;
let index = x as usize + y as usize * width;
if !once.insert(index) {
twice.insert(index);
}
}
}
fn part_common(input: &mut dyn Read, diagonals: bool) -> String {
let mut reader = LineIter::new(input);
let mut map = HashMap::new();
let lines = read_input(input, parse_input);
while let Some(line) = reader.next() {
let (begin, end) = line_definition(line).finish().unwrap().1;
let width = lines.iter().map(|&((_, x_max), _)| x_max).max().unwrap() as usize + 1;
let mut once_map = BitSet::new();
let mut twice_map = BitSet::new();
for (begin, end) in lines {
if begin.0 == end.0 {
let y_range = begin.1..=end.1;
stripe(&mut map, repeat(begin.0), y_range);
stripe(
&mut once_map,
&mut twice_map,
width,
repeat(begin.0),
y_range,
);
} else if begin.1 == end.1 {
let x_range = begin.0..=end.0;
stripe(&mut map, x_range, repeat(begin.1));
stripe(
&mut once_map,
&mut twice_map,
width,
x_range,
repeat(begin.1),
);
} else if diagonals {
let x_range = begin.0..=end.0;
let y_range = (begin.1.min(end.1))..=(begin.1.max(end.1));
if begin.1 > end.1 {
// For a downward slope we need to reverse Y
stripe(&mut map, x_range, y_range.rev());
stripe(&mut once_map, &mut twice_map, width, x_range, y_range.rev());
} else {
stripe(&mut map, x_range, y_range);
stripe(&mut once_map, &mut twice_map, width, x_range, y_range);
}
}
}
map.values().filter(|&&v| v > 1).count().to_string()
twice_map.len().to_string()
}
pub fn part1(input: &mut dyn Read) -> String {
@@ -82,11 +104,6 @@ mod tests {
const SAMPLE: &[u8] = include_bytes!("samples/05.txt");
#[test]
fn test_parser() {
assert_eq!(line_definition("6,4 -> 2,0"), Ok(("", ((2, 0), (6, 4)))));
}
#[test]
fn sample_part1() {
test_implementation(part1, SAMPLE, 5)

View File

@@ -182,7 +182,7 @@ mod tests {
fn sample_part1() {
let answers = [16, 12, 23, 31];
for (&sample, answer) in SAMPLE.into_iter().zip(answers) {
for (&sample, answer) in SAMPLE.iter().zip(answers) {
test_implementation(part1, sample, answer);
}
}

View File

@@ -1,9 +1,451 @@
use std::cmp::Reverse;
use std::collections::hash_map::Entry;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::fmt::Display;
use std::io::Read;
use std::mem::swap;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
use crate::common::LineIter;
type Item<const S: usize> = (u32, State<S>);
type Todo<const S: usize> = BinaryHeap<Reverse<Item<S>>>;
type Visited<const S: usize> = HashMap<State<S>, u32>;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)]
enum Pod {
A,
B,
C,
D,
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
impl Pod {
pub fn cost(self) -> u32 {
match self {
Pod::A => 1,
Pod::B => 10,
Pod::C => 100,
Pod::D => 1000,
}
}
pub fn dest(self) -> usize {
self as usize
}
}
impl TryFrom<usize> for Pod {
type Error = usize;
fn try_from(index: usize) -> Result<Self, Self::Error> {
match index {
0 => Ok(Pod::A),
1 => Ok(Pod::B),
2 => Ok(Pod::C),
3 => Ok(Pod::D),
_ => Err(index),
}
}
}
impl TryFrom<char> for Pod {
type Error = &'static str;
fn try_from(c: char) -> Result<Self, Self::Error> {
match c {
'A' => Ok(Pod::A),
'B' => Ok(Pod::B),
'C' => Ok(Pod::C),
'D' => Ok(Pod::D),
_ => Err("Invalid pod"),
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
struct State<const S: usize> {
hallway: [Option<Pod>; 11],
rooms: [[Option<Pod>; S]; 4],
}
fn room_hallway_pos(room: usize) -> usize {
room * 2 + 2
}
fn abs_delta(a: usize, b: usize) -> usize {
if a < b {
b - a
} else {
a - b
}
}
impl<const S: usize> State<S> {
const VALID_HALLWAY_POS: [usize; 7] = [0, 1, 3, 5, 7, 9, 10];
pub fn is_done(&self) -> bool {
self == &State {
hallway: Default::default(),
rooms: [
[Some(Pod::A); S],
[Some(Pod::B); S],
[Some(Pod::C); S],
[Some(Pod::D); S],
],
}
}
fn add_to_queue(self, cost: u32, todo: &mut Todo<S>, visited: &mut Visited<S>) {
let entry = visited.entry(self.clone());
if matches!(&entry, Entry::Occupied(entry) if *entry.get() <= cost) {
// Already got a better one
return;
}
// nightly only :'(
// entry.insert(cost);
*entry.or_default() = cost;
todo.push(Reverse((cost + self.estimate(), self)))
}
fn estimate(&self) -> u32 {
// A* estimate. For every entry that is not already "at rest", the cost is the cost
// required to get it to the top of its intended room.
// Cost to enter the hole for all pods that still need to
let enter_estimate: u32 = self
.rooms
.iter()
.enumerate()
.map(|(index, room)| {
let pod = Pod::try_from(index).unwrap();
room.iter()
.enumerate()
.rev()
.skip_while(|&(_, &entry)| entry == Some(pod))
.map(|(index, _)| index as u32 + 1)
.sum::<u32>()
* pod.cost()
})
.sum();
// Cost for all of the hallway to move to above their intended rooms
let hallway_estimate: u32 = self
.hallway
.iter()
.enumerate()
.filter_map(|(pos, &pod)| {
let pod = pod?;
let destination_pos = room_hallway_pos(pod.dest());
Some(abs_delta(pos, destination_pos) as u32 * pod.cost())
})
.sum();
// Cost to move out of the room and above the correct rooms
let rooms_estimate: u32 = self
.rooms
.iter()
.enumerate()
.map(|(room_index, room)| {
let hallway_pos = room_hallway_pos(room_index);
room.iter()
.enumerate()
.rev()
.skip_while(|&(_, &entry)| {
entry.map(|pod| pod.dest() == room_index).unwrap_or(false)
})
.filter_map(|(room_pos, &pod)| {
let pod = pod?;
let destination_pos = room_hallway_pos(pod.dest());
let steps = 1 + room_pos + abs_delta(hallway_pos, destination_pos).max(2);
Some(steps as u32 * pod.cost())
})
.sum::<u32>()
})
.sum();
enter_estimate + hallway_estimate + rooms_estimate
}
pub fn generate_next(&self, cost: u32, todo: &mut Todo<S>, visited: &mut Visited<S>) {
self.room_to_hallway(cost, todo, visited);
self.hallway_to_room(cost, todo, visited);
}
fn room_to_hallway(&self, cost: u32, todo: &mut Todo<S>, visited: &mut Visited<S>) {
for (index, room) in self.rooms.iter().enumerate() {
// Check if we even want to move anything out of this room
if room
.iter()
.all(|entry| entry.map(|pod| pod.dest() == index).unwrap_or(true))
{
continue;
}
let (pos, pod) = room
.iter()
.enumerate()
.find_map(|(pos, entry)| entry.map(|pod| (pos, pod)))
.unwrap(); // Safe unwrap, we know it exists from above.
let base_cost = 1 + pos;
let hallway_pos = room_hallway_pos(index);
let mut queue_new = |new_pos, new_cost| {
let mut new_state = self.clone();
swap(
&mut new_state.hallway[new_pos],
&mut new_state.rooms[index][pos],
);
new_state.add_to_queue(new_cost + cost, todo, visited)
};
// Check positions to the left
for new_pos in (0..hallway_pos).rev() {
if self.hallway[new_pos].is_some() {
// Hit an occupied room
break;
}
if !Self::VALID_HALLWAY_POS.contains(&new_pos) {
// Not allowed to stop here
continue;
}
let new_cost = (base_cost + hallway_pos - new_pos) as u32 * pod.cost();
queue_new(new_pos, new_cost);
}
// And to the right
for new_pos in hallway_pos..self.hallway.len() {
if self.hallway[new_pos].is_some() {
// Hit an occupied room
break;
}
if !Self::VALID_HALLWAY_POS.contains(&new_pos) {
// Not allowed to stop here
continue;
}
let new_cost = (base_cost + new_pos - hallway_pos) as u32 * pod.cost();
queue_new(new_pos, new_cost);
}
}
}
fn hallway_to_room(&self, cost: u32, todo: &mut Todo<S>, visited: &mut Visited<S>) {
for (pos, pod) in self
.hallway
.iter()
.enumerate()
.filter_map(|(pos, pod)| pod.map(|pod| (pos, pod)))
{
let room = pod.dest();
let new_hallway_pos = room_hallway_pos(room);
// Check if the path is free
let in_between = if new_hallway_pos < pos {
&self.hallway[(new_hallway_pos + 1)..pos]
} else {
&self.hallway[(pos + 1)..new_hallway_pos]
};
if in_between.iter().any(Option::is_some) {
// Something's in the way
continue;
}
// Check if we can move into the room
if self.rooms[room]
.iter()
.copied()
.flatten()
.any(|other| other != pod)
{
// Scared of other pods
continue;
}
let room_pos = if let Some(pos) = self.rooms[room].iter().rposition(Option::is_none) {
pos
} else {
continue;
};
let new_cost = (abs_delta(pos, new_hallway_pos) + room_pos + 1) as u32 * pod.cost();
let mut new_state = self.clone();
swap(
&mut new_state.hallway[pos],
&mut new_state.rooms[room][room_pos],
);
new_state.add_to_queue(cost + new_cost, todo, visited);
}
}
pub fn solve(&self) -> u32 {
let mut todo = Todo::new();
let mut visited = HashMap::new();
visited.insert(self.clone(), 0);
todo.push(Reverse((self.estimate(), self.clone())));
while let Some(Reverse((_, state))) = todo.pop() {
let cost = *visited.get(&state).unwrap_or(&0);
if state.is_done() {
return cost;
}
state.generate_next(cost, &mut todo, &mut visited);
}
panic!("No route found!")
}
}
impl<const S: usize> Display for State<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let helper = |opt_pod| match opt_pod {
Some(Pod::A) => 'A',
Some(Pod::B) => 'B',
Some(Pod::C) => 'C',
Some(Pod::D) => 'D',
None => '.',
};
writeln!(f, "#############")?;
write!(f, "#")?;
for entry in self.hallway {
write!(f, "{}", helper(entry))?;
}
writeln!(f, "#")?;
for i in 0..S {
writeln!(
f,
" #{}#{}#{}#{}#",
helper(self.rooms[0][i]),
helper(self.rooms[1][i]),
helper(self.rooms[2][i]),
helper(self.rooms[3][i])
)?;
}
write!(f, " #########")
}
}
fn read_input(input: &mut dyn Read) -> State<2> {
let mut reader = LineIter::new(input);
let mut state = State {
hallway: Default::default(),
rooms: Default::default(),
};
let _ = reader.next();
let _ = reader.next();
let mut helper = |idx: usize| {
reader
.next()
.unwrap()
.chars()
.filter_map(|c| Pod::try_from(c).ok())
.zip(&mut state.rooms)
.for_each(|(pod, room)| room[idx] = Some(pod))
};
helper(0);
helper(1);
state
}
pub fn part1(input: &mut dyn Read) -> String {
let state = read_input(input);
state.solve().to_string()
}
pub fn part2(input: &mut dyn Read) -> String {
let state2 = read_input(input);
let state4 = State {
hallway: Default::default(),
rooms: [
[
state2.rooms[0][0],
Some(Pod::D),
Some(Pod::D),
state2.rooms[0][1],
],
[
state2.rooms[1][0],
Some(Pod::C),
Some(Pod::B),
state2.rooms[1][1],
],
[
state2.rooms[2][0],
Some(Pod::B),
Some(Pod::A),
state2.rooms[2][1],
],
[
state2.rooms[3][0],
Some(Pod::A),
Some(Pod::C),
state2.rooms[3][1],
],
],
};
state4.solve().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_implementation;
const SAMPLE: &[u8] = include_bytes!("samples/23.txt");
#[test]
fn test_is_done() {
let state = State {
hallway: Default::default(),
rooms: [
[Some(Pod::A); 2],
[Some(Pod::B); 2],
[Some(Pod::C); 2],
[Some(Pod::D); 2],
],
};
assert!(state.is_done());
}
#[test]
fn sample_part1() {
test_implementation(part1, SAMPLE, 12521);
}
#[test]
fn sample_part2() {
test_implementation(part2, SAMPLE, 44169);
}
}

View File

@@ -1,9 +1,159 @@
//! Very input-specific reverse-engineered solution
//!
//! # General implementation
//!
//! The code in the examples is a series of 14 times this:
//!
//! ```txt
//! inp w -> read digit
//! mul x 0
//! add x z
//! mod x 26 -> x = z % 26
//! div z $A -> pop Z (see below)
//! add x $B
//! eql x w -> x = ((z + $B) == w)
//! eql x 0 -> x = ((z + $B) != w)
//! mul y 0
//! add y 25
//! mul y x
//! add y 1 -> if x { 26 } else { 1 }
//! mul z y -> if x { z *= 26 } (push z, see below)
//! mul y 0
//! add y w
//! add y $C -> y = w + $C
//! mul y x
//! add z y -> if x { z += w + $C }
//! ```
//!
//! `$A` is either `1` or `26` which we can translate to a bool `$A == 26` for convenience. This
//! simplifies to the following rust.
//!
//! ```
//! fn validate<const A: bool, const B: i32, const C: i32>(mut z: i32, digit: i32) -> i32 {
//! let x = (z % 26 + B) != digit;
//! if A {
//! z /= 26;
//! }
//!
//! if x {
//! z = 26 * z + digit + C;
//! }
//!
//! z
//! }
//! ```
//!
//! In human terms, `z` is used to hold a base 26 number. When `$A` is `true`, we pop off the least
//! significant digit by dividing by 26. Then, depending on whether `(z + $B) % 26` is equal to our
//! digit, we push `digit + $C`. Ideally, we should pop as often as we push in order to arrive at `z
//! == 0` in the end. The input contains 7 pops, so we want each of those to not push.
//!
//! To solve this problem, we use a backtracking memoizing algorithm, where we cancel every sequence
//! that fails to pop at some point. A pop is failed whenever we execute a validation pop where we
//! can pop if `x` happened to be set to `0` at the time of the check. We can memoize this over the
//! running value of `z`.
//!
//! This implementation probably doesn't work on other people's input; to fix it, you'll want to
//! update the `parse_step` function. Good luck with that.
use std::collections::HashMap;
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::newline;
use nom::combinator::map;
use nom::multi::separated_list1;
use nom::sequence::delimited;
use nom::sequence::preceded;
use nom::sequence::tuple;
use nom::IResult;
use crate::common::read_input;
type Cache = HashMap<(usize, i32), Option<i64>>;
#[derive(Debug)]
struct Step(bool, i32, i32);
impl Step {
fn evaluate(&self, digit: i32, z: i32) -> Option<i32> {
if self.0 {
(z % 26 + self.1 == digit).then(|| z / 26)
} else {
Some(z * 26 + digit + self.2)
}
}
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
fn parse_step(input: &[u8]) -> IResult<&[u8], Step> {
use nom::character::complete::i32;
let parse_pop = preceded(
tag("inp w\nmul x 0\nadd x z\nmod x 26\ndiv z "),
alt((map(tag("1"), |_| false), map(tag("26"), |_| true))),
);
let parse_a = preceded(tag("\nadd x "), i32);
let parse_b = delimited(
tag("\neql x w\neql x 0\nmul y 0\nadd y 25\nmul y x\nadd y 1\nmul z y\nmul y 0\nadd y w\nadd y "),
i32,
tag("\nmul y x\nadd z y"),
);
map(tuple((parse_pop, parse_a, parse_b)), |(pop, a, b)| {
Step(pop, a, b)
})(input)
}
fn parse_input(input: &[u8]) -> IResult<&[u8], Vec<Step>> {
separated_list1(newline, parse_step)(input)
}
fn optimize(
current: usize,
steps: &[Step],
digits: &[i32],
z: i32,
cache: &mut Cache,
) -> Option<i64> {
if current >= steps.len() {
return (z == 0).then(|| 0);
}
if let Some(&memoized) = cache.get(&(current, z)) {
return memoized;
}
let result = digits.iter().find_map(|&digit| {
let z = steps[current].evaluate(digit, z)?;
let result = optimize(current + 1, steps, digits, z, cache)?;
Some(result + digit as i64 * 10i64.pow(13 - current as u32))
});
cache.insert((current, z), result);
result
}
fn parts_common(input: &mut dyn Read, digits: &[i32]) -> String {
let steps = read_input(input, parse_input);
let mut cache = Cache::new();
optimize(0, &steps, digits, 0, &mut cache)
.unwrap()
.to_string()
}
pub fn part1(input: &mut dyn Read) -> String {
let digits = [9, 8, 7, 6, 5, 4, 3, 2, 1];
parts_common(input, &digits)
}
pub fn part2(input: &mut dyn Read) -> String {
let digits = [1, 2, 3, 4, 5, 6, 7, 8, 9];
parts_common(input, &digits)
}

View File

@@ -92,7 +92,7 @@ pub fn get_implementation(day: usize, part2: bool) -> Solution {
#[cfg(test)]
fn test_implementation(solution: Solution, data: &[u8], answer: impl ToString) {
let result = solution(&mut &data[..]);
let result = solution(&mut &*data);
assert_eq!(answer.to_string(), result);
}

5
2021/src/samples/23.txt Normal file
View File

@@ -0,0 +1,5 @@
#############
#...........#
###B#C#B#D###
#A#D#C#A#
#########