Skip to content

Commit

Permalink
2022: Start on day 22
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 28, 2023
1 parent 74ba5e0 commit d40296c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
68 changes: 58 additions & 10 deletions aoc_2022/src/day_22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ impl Solution for Day22 {

fn part_a(&self, input: &str) -> Answer {
let mut world = World::parse(input);
world.run(wrap_1);

world.run(wrap_2d);
world.password().into()
}

fn part_b(&self, _input: &str) -> Answer {
Answer::Unimplemented
fn part_b(&self, input: &str) -> Answer {
let mut world = World::parse(input);
world.run(wrap_3d);
world.password().into()
}
}

Expand All @@ -29,6 +30,7 @@ struct World {
// == World ==
walls: HashSet<Point>,
open: HashSet<Point>,
side_len: usize,

// == Player ==
pos: Point,
Expand All @@ -52,15 +54,20 @@ enum Direction {

impl World {
fn parse(raw: &str) -> Self {
let raw = raw.replace('\r', "");
let (map, instructions) = raw.split_once("\n\n").unwrap();

let mut walls = HashSet::new();
let mut open = HashSet::new();
let mut side_len = 0;

for (y, line) in map.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
let (x, y) = (x as isize, y as isize);

if y == 0 && c != ' ' {
side_len += 1;
}

match c {
'#' => walls.insert(Point::new(x, y)),
'.' => open.insert(Point::new(x, y)),
Expand All @@ -79,6 +86,7 @@ impl World {
Self {
walls,
pos: *start,
side_len,
open,
dir: Direction::Right,
instructions: VecDeque::from(Instruction::parse(instructions)),
Expand Down Expand Up @@ -127,22 +135,22 @@ impl Instruction {
match i {
i if i.is_ascii_digit() => working.push(i),
'L' => {
Self::_flush(&mut working, &mut out);
Self::flush(&mut working, &mut out);
out.push(Instruction::Turn(Direction::Left));
}
'R' => {
Self::_flush(&mut working, &mut out);
Self::flush(&mut working, &mut out);
out.push(Instruction::Turn(Direction::Right));
}
_ => continue,
}
}

Self::_flush(&mut working, &mut out);
Self::flush(&mut working, &mut out);
out
}

fn _flush(working: &mut String, out: &mut Vec<Self>) {
fn flush(working: &mut String, out: &mut Vec<Self>) {
if !working.is_empty() {
out.push(Instruction::Move(working.parse().unwrap()));
working.clear();
Expand Down Expand Up @@ -188,7 +196,7 @@ impl Direction {
}
}

fn wrap_1(world: &World, mut pos: Point) -> Option<(Point, Direction)> {
fn wrap_2d(world: &World, mut pos: Point) -> Option<(Point, Direction)> {
loop {
pos = world.dir.reverse().apply(pos);
if !world.walls.contains(&pos) && !world.open.contains(&pos) {
Expand All @@ -202,6 +210,46 @@ fn wrap_1(world: &World, mut pos: Point) -> Option<(Point, Direction)> {
}
}

fn wrap_3d(world: &World, mut _pos: Point) -> Option<(Point, Direction)> {
let _ = world.side_len;
todo!()
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day22;

const CASE: &str = indoc! {"
...#
.#..
#...
....
...#.......#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
......#.
10R5L5R10L4R5L5
"};

#[test]
fn part_a() {
assert_eq!(Day22.part_a(CASE), 6032.into());
}

#[test]
fn part_b() {
assert_eq!(Day22.part_b(CASE), 5031.into());
}
}

/*
1 1 1 1
1 1 1 1
Expand Down
16 changes: 0 additions & 16 deletions aoc_2022/src/day_23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@ impl World {
Self { elves, iter: 0 }
}

fn draw(&self) {
let (min, max) = self.bounds();

println!("\nITER: {}", self.iter);
for y in min.y..=max.y {
for x in min.x..=max.x {
if self.elves.contains(&Point::new(x, y)) {
print!("#");
continue;
}
print!(".");
}
println!()
}
}

fn tick(&mut self) -> bool {
let mut next = HashSet::new();
let mut next_to_cur = HashMap::new();
Expand Down

0 comments on commit d40296c

Please sign in to comment.