Skip to content

Commit

Permalink
2019: Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvw committed Apr 30, 2024
1 parent 2c55891 commit 14bb9a0
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2019/input_11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3,8,1005,8,320,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,29,2,101,10,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,54,2,3,16,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,81,1006,0,75,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,105,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,128,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,149,1,105,5,10,1,105,20,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,179,1,101,1,10,2,109,8,10,1006,0,74,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,213,1006,0,60,2,1105,9,10,1,1005,11,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,245,1,6,20,10,1,1103,11,10,2,6,11,10,2,1103,0,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,284,2,1103,12,10,2,1104,14,10,2,1004,12,10,2,1009,4,10,101,1,9,9,1007,9,968,10,1005,10,15,99,109,642,104,0,104,1,21102,1,48063419288,1,21102,1,337,0,1105,1,441,21101,0,846927340300,1,21101,0,348,0,1105,1,441,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,235245104151,1,21102,395,1,0,1105,1,441,21102,29032123584,1,1,21101,0,406,0,1105,1,441,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,709047878500,1,21101,429,0,0,1106,0,441,21101,868402070284,0,1,21102,1,440,0,1105,1,441,99,109,2,22102,1,-1,1,21101,40,0,2,21101,0,472,3,21102,462,1,0,1105,1,505,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,467,468,483,4,0,1001,467,1,467,108,4,467,10,1006,10,499,1102,1,0,467,109,-2,2106,0,0,0,109,4,2101,0,-1,504,1207,-3,0,10,1006,10,522,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21101,1,0,3,21102,541,1,0,1106,0,546,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,569,2207,-4,-2,10,1006,10,569,21202,-4,1,-4,1105,1,637,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,588,0,0,1105,1,546,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,607,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,629,21201,-1,0,1,21102,629,1,0,106,0,504,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
162 changes: 162 additions & 0 deletions 2019/src/bin/puzzle_11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use std::cmp;
use std::collections::HashMap;
use std::io;

use aoc2019::grid::Point;
use aoc2019::intcode;

#[derive(Copy, Clone)]
enum PanelColor {
Black = 0,
White = 1,
}

impl From<i64> for PanelColor {
fn from(v: i64) -> Self {
match v {
0 => PanelColor::Black,
1 => PanelColor::White,
_ => unreachable!(),
}
}
}

enum TurnDirection {
Counterclockwise,
Clockwise,
}

impl From<i64> for TurnDirection {
fn from(v: i64) -> Self {
match v {
0 => TurnDirection::Counterclockwise,
1 => TurnDirection::Clockwise,
_ => unreachable!(),
}
}
}

#[derive(Copy, Clone)]
enum Direction {
Up,
Right,
Down,
Left,
}

impl From<i64> for Direction {
fn from(v: i64) -> Self {
match v {
0 => Direction::Up,
1 => Direction::Right,
2 => Direction::Down,
3 => Direction::Left,
_ => unreachable!(),
}
}
}

struct Robot {
direction: Direction,
loc: Point,
}

impl Robot {
fn rotate(&mut self, turn: TurnDirection) {
self.direction = Direction::from(
(self.direction as i64
+ match turn {
TurnDirection::Counterclockwise => -1,
TurnDirection::Clockwise => 1,
})
.rem_euclid(4),
);
}

fn move_forward(&mut self) {
self.loc += match self.direction {
Direction::Up => Point { x: -1, y: 0 },
Direction::Right => Point { x: 0, y: 1 },
Direction::Down => Point { x: 1, y: 0 },
Direction::Left => Point { x: 0, y: -1 },
}
}
}

fn paint_hull(prog: &[i64], start_color: PanelColor) -> HashMap<Point, PanelColor> {
let mut comp = intcode::Computer::new(prog, &[start_color as i64]);

let mut robot = Robot {
direction: Direction::Up,
loc: Default::default(),
};

let mut panels: HashMap<Point, PanelColor> = HashMap::new();
while let intcode::State::Output(paint_color) = comp.run() {
panels.insert(robot.loc, PanelColor::from(paint_color));

if let intcode::State::Output(turn_direction) = comp.run() {
robot.rotate(TurnDirection::from(turn_direction));
} else {
break;
}

robot.move_forward();

let panel_color_over = panels.get(&robot.loc).unwrap_or(&PanelColor::Black);
comp.input.push_back(*panel_color_over as i64);
}

panels
}

fn render_registration_identifier(panels: &HashMap<Point, PanelColor>) -> String {
let white_panel_locs: Vec<_> = panels
.iter()
.filter_map(|(&k, &v)| match v {
PanelColor::White => Some(k),
_ => None,
})
.collect();

let delta_x = cmp::min(white_panel_locs.iter().min_by_key(|p| p.x).unwrap().x, 0).abs();
let delta_y = cmp::min(white_panel_locs.iter().min_by_key(|p| p.y).unwrap().y, 0).abs();

let white_panel_locs: Vec<_> = white_panel_locs
.into_iter()
.map(move |p| Point {
x: p.x + delta_x,
y: p.y + delta_y,
})
.collect();

let max_x = white_panel_locs.iter().max_by_key(|p| p.x).unwrap().x;
let max_y = white_panel_locs.iter().max_by_key(|p| p.y).unwrap().y;

let mut hull =
vec![vec![b' '; usize::try_from(max_y).unwrap() + 1]; usize::try_from(max_x).unwrap() + 1];

for p in white_panel_locs {
hull[usize::try_from(p.x).unwrap()][usize::try_from(p.y).unwrap()] = b'#';
}

let hull: Vec<_> = hull
.into_iter()
.map(|row| String::from_utf8(row).unwrap())
.collect();
hull.join("\n")
}

fn main() {
let paint_program = intcode::parse_program(io::stdin());

println!(
"Part 1: {}",
paint_hull(&paint_program, PanelColor::Black).len()
);

print!(
"Part 2:\n{}",
render_registration_identifier(&paint_hull(&paint_program, PanelColor::White))
);
}

0 comments on commit 14bb9a0

Please sign in to comment.