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

1
input/day11.txt Normal file
View File

@@ -0,0 +1 @@
3,8,1005,8,361,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,28,2,1104,18,10,1006,0,65,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,57,1,1101,5,10,2,108,15,10,2,102,12,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,91,2,1005,4,10,2,1107,10,10,1006,0,16,2,109,19,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,129,1,104,3,10,1,1008,9,10,1006,0,65,1,104,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,165,1,1106,11,10,1,1106,18,10,1,8,11,10,1,4,11,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,203,2,1003,11,10,1,1105,13,10,1,101,13,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,237,2,7,4,10,1006,0,73,1,1003,7,10,1006,0,44,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,273,2,108,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,299,1,1107,6,10,1006,0,85,1,1107,20,10,1,1008,18,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,337,2,107,18,10,101,1,9,9,1007,9,951,10,1005,10,15,99,109,683,104,0,104,1,21102,1,825594852248,1,21101,378,0,0,1105,1,482,21101,0,387240006552,1,21101,0,389,0,1106,0,482,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,29032025091,1,21101,436,0,0,1106,0,482,21101,29033143299,0,1,21102,1,447,0,1105,1,482,3,10,104,0,104,0,3,10,104,0,104,0,21101,988669698916,0,1,21101,0,470,0,1106,0,482,21101,0,709052072804,1,21102,1,481,0,1106,0,482,99,109,2,21202,-1,1,1,21101,0,40,2,21101,0,513,3,21101,503,0,0,1106,0,546,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,508,509,524,4,0,1001,508,1,508,108,4,508,10,1006,10,540,1101,0,0,508,109,-2,2105,1,0,0,109,4,1202,-1,1,545,1207,-3,0,10,1006,10,563,21102,0,1,-3,21202,-3,1,1,22101,0,-2,2,21102,1,1,3,21101,582,0,0,1105,1,587,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,610,2207,-4,-2,10,1006,10,610,21202,-4,1,-4,1106,0,678,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,629,1,0,1106,0,587,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,648,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,670,21202,-1,1,1,21101,670,0,0,105,1,545,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

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;