Commit 539476e6 authored by Marcel Behrmann's avatar Marcel Behrmann

day(11): solved both parts

parent 46adf681
3,8,1005,8,328,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,28,1,1003,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,54,2,1103,6,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,80,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,102,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,124,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,147,1006,0,35,1,7,3,10,2,106,13,10,2,1104,9,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,183,2,7,16,10,2,105,14,10,1,1002,12,10,1006,0,13,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,220,1006,0,78,2,5,3,10,1006,0,92,1006,0,92,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,255,1006,0,57,2,1001,11,10,1006,0,34,2,1007,18,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,292,2,109,3,10,1,1103,14,10,2,2,5,10,2,1006,3,10,101,1,9,9,1007,9,997,10,1005,10,15,99,109,650,104,0,104,1,21101,932700762920,0,1,21101,0,345,0,1105,1,449,21102,1,386577306516,1,21102,356,1,0,1106,0,449,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,179355975827,0,1,21101,403,0,0,1106,0,449,21102,1,46413220903,1,21102,1,414,0,1106,0,449,3,10,104,0,104,0,3,10,104,0,104,0,21101,988224959252,0,1,21102,1,437,0,1106,0,449,21101,717637968660,0,1,21101,0,448,0,1106,0,449,99,109,2,22101,0,-1,1,21102,40,1,2,21101,480,0,3,21101,470,0,0,1106,0,513,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,475,476,491,4,0,1001,475,1,475,108,4,475,10,1006,10,507,1102,1,0,475,109,-2,2105,1,0,0,109,4,2102,1,-1,512,1207,-3,0,10,1006,10,530,21102,1,0,-3,22102,1,-3,1,22101,0,-2,2,21102,1,1,3,21101,0,549,0,1105,1,554,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,577,2207,-4,-2,10,1006,10,577,21202,-4,1,-4,1106,0,645,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,596,0,1106,0,554,21201,1,0,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,615,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,637,21201,-1,0,1,21101,0,637,0,105,1,512,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
3,8,1005,8,328,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,28,1,1003,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,54,2,1103,6,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,80,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,102,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,124,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,147,1006,0,35,1,7,3,10,2,106,13,10,2,1104,9,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,183,2,7,16,10,2,105,14,10,1,1002,12,10,1006,0,13,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,220,1006,0,78,2,5,3,10,1006,0,92,1006,0,92,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,255,1006,0,57,2,1001,11,10,1006,0,34,2,1007,18,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,292,2,109,3,10,1,1103,14,10,2,2,5,10,2,1006,3,10,101,1,9,9,1007,9,997,10,1005,10,15,99,109,650,104,0,104,1,21101,932700762920,0,1,21101,0,345,0,1105,1,449,21102,1,386577306516,1,21102,356,1,0,1106,0,449,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,179355975827,0,1,21101,403,0,0,1106,0,449,21102,1,46413220903,1,21102,1,414,0,1106,0,449,3,10,104,0,104,0,3,10,104,0,104,0,21101,988224959252,0,1,21102,1,437,0,1106,0,449,21101,717637968660,0,1,21101,0,448,0,1106,0,449,99,109,2,22101,0,-1,1,21102,40,1,2,21101,480,0,3,21101,470,0,0,1106,0,513,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,475,476,491,4,0,1001,475,1,475,108,4,475,10,1006,10,507,1102,1,0,475,109,-2,2105,1,0,0,109,4,2102,1,-1,512,1207,-3,0,10,1006,10,530,21102,1,0,-3,22102,1,-3,1,22101,0,-2,2,21102,1,1,3,21101,0,549,0,1105,1,554,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,577,2207,-4,-2,10,1006,10,577,21202,-4,1,-4,1106,0,645,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,596,0,1106,0,554,21201,1,0,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,615,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,637,21201,-1,0,1,21101,0,637,0,105,1,512,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
\ No newline at end of file
mod utils;
mod intvm;
use std::fs;
use std::collections::HashMap;
const INPUT_FILE: &str = "input.txt";
const INPUT_FILE: &str = "input.txt";
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
let file = &fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input = get_input(file);
part_01(input.clone());
part_02(input.clone());
println!("Result Part 1: {}", part_01(&input));
part_02(&input);
}
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
println!("Result of Part 1");
fn get_input(input: &String) -> Vec<&str> {
let codes = input.split(",");
codes.collect::<Vec<&str>>()
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
println!("Result of Part 2");
fn part_01(input: &Vec<&str>) -> i64{
let mut vm = intvm::create_vm(input);
let mut panels: HashMap<String, i64> = HashMap::new();
let mut x = 0;
let mut y = 0;
let mut direction = 0;
while (!vm.is_halted) {
//for _ in 0..5 {
let mut panel_index = String::new();
panel_index.push_str(x.to_string().as_ref());
panel_index.push_str("|");
panel_index.push_str(y.to_string().as_ref());
let panel = panels.get(&panel_index).unwrap_or(&0);
vm.input.push_back(*panel);
vm.run();
let out = vm.output.pop_front().unwrap();
panels.entry(panel_index).and_modify(|e| *e = out).or_insert(out);
let turn = vm.output.pop_front().unwrap();
match turn {
0 => direction -= 1,
1 => direction += 1,
_ => panic!("unknown turn direction: {}", turn),
}
if direction < 0 {
direction += 4;
}
direction %= 4;
match direction {
0 => y += 1,
1 => x += 1,
2 => y -= 1,
3 => x -= 1,
_ => panic!("unknown direction: {}", direction),
}
}
panels.len() as i64
}
fn part_02(input: &Vec<&str>) {
let mut vm = intvm::create_vm(input);
let mut panels: HashMap<String, i64> = HashMap::new();
let mut x = 0;
let mut y = 0;
let mut direction = 0;
let mut panel_index = String::new();
panel_index.push_str(x.to_string().as_ref());
panel_index.push_str("|");
panel_index.push_str(y.to_string().as_ref());
panels.insert(panel_index, 1);
while !vm.is_halted {
//for _ in 0..5 {
let mut panel_index = String::new();
panel_index.push_str(x.to_string().as_ref());
panel_index.push_str("|");
panel_index.push_str(y.to_string().as_ref());
let panel = panels.get(&panel_index).unwrap_or(&0);
vm.input.push_back(*panel);
vm.run();
let out = vm.output.pop_front().unwrap();
panels.entry(panel_index).and_modify(|e| *e = out).or_insert(out);
let turn = vm.output.pop_front().unwrap();
match turn {
0 => direction -= 1,
1 => direction += 1,
_ => panic!("unknown turn direction: {}", turn),
}
if direction < 0 {
direction += 4;
}
direction %= 4;
match direction {
0 => y += 1,
1 => x += 1,
2 => y -= 1,
3 => x -= 1,
_ => panic!("unknown direction: {}", direction),
}
}
for y in (-50..50).rev() {
for x in -50..50 {
let mut panel_index = String::new();
panel_index.push_str(x.to_string().as_ref());
panel_index.push_str("|");
panel_index.push_str(y.to_string().as_ref());
let value = panels.get(&panel_index).or(Some(&0)).unwrap();
match value {
0 => print!("."),
1 => print!("#"),
_ => print!("."),
}
}
println!();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment