day11 task 1

This commit is contained in:
Johannes
2019-12-12 11:32:38 +01:00
parent f28caa727d
commit 009d1b8f86
4 changed files with 57 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
mod tasks;
fn main() {
tasks::day10::run();
tasks::day11::run();
}

54
src/tasks/day11.rs Normal file
View File

@@ -0,0 +1,54 @@
use super::day05::{IntCodeComputer, RAM};
use std::collections::{HashMap, HashSet};
#[allow(dead_code)]
pub fn run() {
let input = std::fs::read_to_string("input/day11.txt").unwrap();
let ram: RAM = input
.split(",")
.enumerate()
.map(|(i, s)| (i, s.parse::<i128>().unwrap()))
.collect();
task1(ram.clone());
}
fn task1(ram: RAM) {
let directions: HashMap<_, _> =
vec![('^', (0, -1)), ('v', (0, 1)), ('<', (-1, 0)), ('>', (1, 0))]
.into_iter()
.collect();
let turns: HashMap<_, _> = vec![
(('^', 0), '<'),
(('^', 1), '>'),
(('v', 0), '>'),
(('v', 1), '<'),
(('<', 0), 'v'),
(('<', 1), '^'),
(('>', 0), '^'),
(('>', 1), 'v'),
]
.into_iter()
.collect();
let mut colors: HashMap<(i32, i32), i128> = HashMap::new();
let mut painted_positions: HashSet<(i32, i32)> = HashSet::new();
let get = |colors: &HashMap<_, _>, (x, y)| **colors.get(&(x, y)).get_or_insert(&0);
let turn = |(c, d)| turns.get(&(c, d)).unwrap();
let mov = |(x0, y0), (x1, y1)| (x0 + x1, y0 + y1);
let mut done = false;
let mut computer = IntCodeComputer::new(vec![], ram);
let mut pos = (0, 0);
let mut dir = '^';
while !done {
computer.set_input(&[get(&colors, pos)]);
computer.clear_output();
done = computer.run_until_input_empty();
let color = computer.get_output()[0];
let turn_indicator = computer.get_output()[1];
colors.insert(pos, color);
painted_positions.insert(pos);
dir = *turn((dir, turn_indicator));
pos = mov(pos, *directions.get(&dir).unwrap());
}
println!("Task 1: {} positions were painted", painted_positions.len());
}

View File

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