2019: day11: finish

This commit is contained in:
Antoine Martin 2019-12-11 23:58:25 +01:00
parent bbbde2836d
commit 4e1fecf3ee
6 changed files with 191 additions and 0 deletions

View file

@ -10,6 +10,7 @@ use aoc2019::day07;
use aoc2019::day08; use aoc2019::day08;
use aoc2019::day09; use aoc2019::day09;
use aoc2019::day10; use aoc2019::day10;
use aoc2019::day11;
fn aoc2019_all(c: &mut Criterion) { fn aoc2019_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
@ -22,6 +23,7 @@ fn aoc2019_all(c: &mut Criterion) {
c.bench_function("day08", |b| b.iter(|| day08::run().unwrap())); c.bench_function("day08", |b| b.iter(|| day08::run().unwrap()));
c.bench_function("day09", |b| b.iter(|| day09::run().unwrap())); c.bench_function("day09", |b| b.iter(|| day09::run().unwrap()));
c.bench_function("day10", |b| b.iter(|| day10::run().unwrap())); c.bench_function("day10", |b| b.iter(|| day10::run().unwrap()));
c.bench_function("day11", |b| b.iter(|| day11::run().unwrap()));
} }
criterion_group! { criterion_group! {

1
aoc2019/input/day11.txt Normal file
View file

@ -0,0 +1 @@
3,8,1005,8,301,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,28,1006,0,98,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,54,2,1001,6,10,1,108,1,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,84,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,105,1006,0,94,2,7,20,10,2,5,7,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,139,1006,0,58,2,1003,16,10,1,6,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,172,2,107,12,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,197,1006,0,34,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,223,1006,0,62,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,248,1,7,7,10,1006,0,64,2,1008,5,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,280,101,1,9,9,1007,9,997,10,1005,10,15,99,109,623,104,0,104,1,21102,1,387508351636,1,21101,318,0,0,1106,0,422,21102,1,838480007948,1,21101,0,329,0,1106,0,422,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,21101,0,235190525123,1,21101,0,376,0,1105,1,422,21101,0,106505084123,1,21101,0,387,0,1106,0,422,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,838324605292,1,21102,1,410,0,1105,1,422,21102,709496668940,1,1,21102,421,1,0,1105,1,422,99,109,2,22101,0,-1,1,21102,1,40,2,21101,0,453,3,21102,443,1,0,1106,0,486,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,448,449,464,4,0,1001,448,1,448,108,4,448,10,1006,10,480,1102,1,0,448,109,-2,2106,0,0,0,109,4,2101,0,-1,485,1207,-3,0,10,1006,10,503,21102,0,1,-3,22102,1,-3,1,21201,-2,0,2,21101,1,0,3,21102,1,522,0,1106,0,527,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,550,2207,-4,-2,10,1006,10,550,21202,-4,1,-4,1106,0,618,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,569,1,0,1106,0,527,21202,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,588,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,610,22101,0,-1,1,21101,0,610,0,106,0,485,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

View file

@ -0,0 +1,6 @@
██ ███ █ █ ██ █ █ ███ ████ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ ███ ████
████ ███ █ █ █ ██ █ █ ███ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ ██ ███ ██ █ █ █ █ █

179
aoc2019/src/day11.rs Normal file
View file

@ -0,0 +1,179 @@
use std::collections::HashMap;
use std::fmt::Write;
use aoc::err;
use aoc::Result;
use crate::intcode::Intcode;
const INPUT: &str = include_str!("../input/day11.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2:")?;
part2(INPUT, &mut res)?;
Ok(res)
}
fn part1(input: &str) -> Result<usize> {
let mut robot = Robot::new(input)?;
let mut board = HashMap::new();
robot.run(&mut board)?;
Ok(board.len())
}
fn part2(input: &str, res: &mut String) -> Result<()> {
let mut robot = Robot::new(input)?;
let mut board = HashMap::new();
board.insert(robot.pos, true);
robot.run(&mut board)?;
write_board(res, board)
}
fn write_board(res: &mut String, board: HashMap<Position, bool>) -> Result<()> {
if board.is_empty() {
return Err(err!("board was empty"));
}
let min_x = board.keys().map(|p| p.x).min().unwrap();
let max_x = board.keys().map(|p| p.x).max().unwrap();
let min_y = board.keys().map(|p| p.y).min().unwrap();
let max_y = board.keys().map(|p| p.y).max().unwrap();
let width = max_x - min_x + 1;
let height = max_y - min_y + 1;
for i in 0..height {
for j in 0..width {
match board.get(&Position {
x: j + min_x,
y: i + min_y,
}) {
Some(true) => write!(res, "")?,
_ => write!(res, " ")?,
};
}
writeln!(res)?;
}
Ok(())
}
enum Direction {
Up,
Down,
Left,
Right,
}
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
struct Position {
x: i64,
y: i64,
}
struct Robot {
pos: Position,
dir: Direction,
brain: Intcode,
}
impl Robot {
fn new(program: &str) -> Result<Self> {
let intcode = Intcode::new(program)?;
Ok(Robot {
pos: Position { x: 0, y: 0 },
dir: Direction::Up,
brain: intcode,
})
}
fn run(&mut self, board: &mut HashMap<Position, bool>) -> Result<()> {
while !self.brain.run_and_wait()? {
if !self.brain.output.is_empty() {
let color = self.brain.output[0];
let direction = self.brain.output[1];
match color {
0 => board.insert(self.pos, false),
1 => board.insert(self.pos, true),
_ => return Err(err!("robot brain output different from 0 or 1")),
};
match direction {
0 => self.turn_left(),
1 => self.turn_right(),
_ => return Err(err!("robot brain output different from 0 or 1")),
};
self.move_forward();
self.brain.output.clear();
}
let paint = match board.get(&self.pos) {
Some(true) => 1,
_ => 0,
};
self.brain.add_input(paint);
}
Ok(())
}
fn move_forward(&mut self) {
match self.dir {
Direction::Up => self.pos.y -= 1,
Direction::Down => self.pos.y += 1,
Direction::Left => self.pos.x -= 1,
Direction::Right => self.pos.x += 1,
}
}
fn turn_right(&mut self) {
match self.dir {
Direction::Up => self.dir = Direction::Right,
Direction::Right => self.dir = Direction::Down,
Direction::Down => self.dir = Direction::Left,
Direction::Left => self.dir = Direction::Up,
}
}
fn turn_left(&mut self) {
match self.dir {
Direction::Up => self.dir = Direction::Left,
Direction::Right => self.dir = Direction::Up,
Direction::Down => self.dir = Direction::Right,
Direction::Left => self.dir = Direction::Down,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const RES2: &str = include_str!("../input/day11_res.txt");
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 1883);
}
#[test]
fn part2_real() {
let mut res = String::with_capacity(RES2.len());
part2(INPUT, &mut res).unwrap();
assert_eq!(res.len(), RES2.len());
assert_eq!(res, RES2);
}
}

View file

@ -10,3 +10,4 @@ pub mod day07;
pub mod day08; pub mod day08;
pub mod day09; pub mod day09;
pub mod day10; pub mod day10;
pub mod day11;

View file

@ -11,6 +11,7 @@ use aoc2019::day07;
use aoc2019::day08; use aoc2019::day08;
use aoc2019::day09; use aoc2019::day09;
use aoc2019::day10; use aoc2019::day10;
use aoc2019::day11;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[ let days: &[DayFunc] = &[
@ -24,6 +25,7 @@ fn main() -> Result<()> {
day08::run, day08::run,
day09::run, day09::run,
day10::run, day10::run,
day11::run,
]; ];
aoc::run(days) aoc::run(days)