Commit 46adf681 authored by Marcel Behrmann's avatar Marcel Behrmann

create(day_11): Initial creation of day 11

parent 6c29f53e
target/
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day_11"
version = "0.1.0"
[package]
name = "day_11"
version = "0.1.0"
authors = ["Marcel Behrmann <[email protected]>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
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
use crate::utils;
use std::collections::VecDeque;
#[allow(dead_code)]
pub struct IntVM {
pub rom: Vec<i64>,
pub input: VecDeque<i64>,
pub output: VecDeque<i64>,
pub memory: Vec<i64>,
pub instr_ptr: usize,
pub is_halted: bool,
pub is_waiting_input: bool,
pub print_output: bool,
pub relative_offset: i64,
update_pointer: bool,
tick_length: usize,
}
impl IntVM {
#[allow(dead_code)]
pub fn reset(&mut self) {
self.memory = self.rom.clone();
self.instr_ptr = 0;
self.is_halted = false;
}
#[allow(dead_code)]
pub fn set_mem(&mut self, address: usize, value: i64) {
self.memory[address] = value;
}
pub fn get_input(&mut self, arg_count: usize) -> i64 {
let instruction = self.memory[self.instr_ptr];
let mode = (instruction / (10 as i64).pow((arg_count + 2) as u32)) % 10;
if arg_count + 2 > self.tick_length {
self.tick_length = arg_count + 2;
}
match mode {
0 => self.memory[self.memory[self.instr_ptr + 1 + arg_count] as usize],
1 => self.memory[self.instr_ptr + 1 + arg_count],
2 => self.memory[(self.relative_offset + self.memory[self.instr_ptr+1+arg_count]) as usize],
_ => self.memory[self.memory[self.instr_ptr + 1 + arg_count] as usize],
}
}
pub fn get_target(&mut self, arg_count: usize) -> usize {
if arg_count + 2 > self.tick_length {
self.tick_length = arg_count + 2;
}
let instruction = self.memory[self.instr_ptr];
let mode = (instruction / (10 as i64).pow((arg_count + 2) as u32)) % 10;
if arg_count + 2 > self.tick_length {
self.tick_length = arg_count + 2;
}
match mode {
2 => (self.relative_offset + self.memory[self.instr_ptr+1+arg_count]) as usize,
_ => self.memory[self.instr_ptr + arg_count + 1] as usize,
}
}
pub fn jump(&mut self, addr: usize) {
self.instr_ptr = addr;
self.update_pointer = false;
}
fn pre_tick(&mut self) {
self.update_pointer = true;
self.tick_length = 0;
}
fn post_tick(&mut self) {
if self.update_pointer {
self.instr_ptr = self.instr_ptr + self.tick_length;
}
}
#[allow(dead_code)]
pub fn tick(&mut self) {
if self.is_halted || self.is_waiting_input {
return;
}
self.pre_tick();
let op: i64 = self.memory[self.instr_ptr] % 100;
match op {
1 => {
let a = self.get_input(0);
let b = self.get_input(1);
let target = self.get_target(2);
self.set_mem(target, a + b);
},
2 => {
let a = self.get_input(0);
let b = self.get_input(1);
let target = self.get_target(2);
self.set_mem(target, a * b);
},
3 => {
let target = self.get_target(0);
if self.input.len() == 0 {
self.is_waiting_input = true;
return;
}
self.memory[target] = self.input.pop_front().expect("No input provieded?!");
},
4 => {
let target = self.get_input(0);
self.output.push_back(target as i64);
if self.print_output {
println!("OUT: {}", target);
}
},
5 => {
let a = self.get_input(0);
let b = self.get_input(1);
if a != 0 {
self.jump(b as usize);
}
},
6 => {
let a = self.get_input(0);
let b = self.get_input(1);
if a == 0 {
self.jump(b as usize);
}
},
7 => {
let a = self.get_input(0);
let b = self.get_input(1);
let target = self.get_target(2);
if a < b {
self.memory[target] = 1;
} else {
self.memory[target] = 0;
}
},
8 => {
let a = self.get_input(0);
let b = self.get_input(1);
let target = self.get_target(2);
if a == b {
self.memory[target] = 1;
} else {
self.memory[target] = 0;
}
},
9 => {
let a = self.get_input(0);
self.relative_offset += a;
},
99 => {
self.is_halted = true;
},
_ => {
println!("Unknown OPCode ({}, {}, {}, {}) update IntVM", op, self.memory[self.instr_ptr + 1], self.memory[self.instr_ptr + 2], self.memory[self.instr_ptr + 3]);
self.is_halted = true;
},
}
self.post_tick();
}
#[allow(dead_code)]
pub fn get_output(&mut self) -> i64 {
return self.memory[0];
}
#[allow(dead_code)]
pub fn run(&mut self) -> i64 {
self.is_waiting_input = false;
while !self.is_halted && !self.is_waiting_input {
self.tick()
};
self.get_output()
}
}
#[allow(dead_code)]
pub fn create_vm(input: &Vec<&str>) -> IntVM {
let mut vm = IntVM {
rom: utils::vec_to_int(input.clone()),
print_output: false,
instr_ptr: 0,
is_halted: false,
is_waiting_input: false,
memory: utils::vec_to_int(input.clone()),
input: VecDeque::new(),
output: VecDeque::new(),
relative_offset: 0,
update_pointer: true,
tick_length: 0,
};
for _ in 0..1000 {
vm.memory.push(0);
}
vm
}
\ No newline at end of file
mod utils;
mod intvm;
use std::fs;
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>>();
part_01(input.clone());
part_02(input.clone());
}
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
println!("Result of Part 1");
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
println!("Result of Part 2");
}
\ No newline at end of file
#[allow(dead_code)]
pub fn vec_to_int(input: Vec<&str>) -> Vec<i64> {
input
.into_iter()
.map(|line| { line.parse().expect("erro reading input") })
.collect()
}
#[allow(dead_code)]
pub fn vec_to_float(input: Vec<&str>) -> Vec<f64> {
input
.into_iter()
.map(|line| { line.parse().expect("erro reading input") })
.collect()
}
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