2019 - day2 - part 1: dirty but works

This commit is contained in:
genofire 2020-09-09 11:03:26 +02:00
parent 8a201e8aa0
commit 9a5d4edc09
5 changed files with 73 additions and 1 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

View file

@ -1 +0,0 @@
/target

5
2019/day2/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day2"
version = "0.1.0"

9
2019/day2/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "day2"
version = "0.1.0"
authors = ["genofire <geno+dev@fireorbit.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

58
2019/day2/src/main.rs Normal file
View file

@ -0,0 +1,58 @@
fn run_intcode(intcode: &mut std::vec::Vec<usize>) {
let mut pos = 0;
println!("\n\nrun with intcode: {:?}", intcode);
while intcode[pos] != 99 {
print!("run code:");
pos = match intcode[pos] {
1 => {
let in1 = intcode[intcode[pos + 1]];
let in2 = intcode[intcode[pos + 2]];
let pos_out = intcode[pos + 3];
println!(
"add[{}]: value:{} + value:{} -> pos:{}",
pos, in1, in2, pos_out
);
intcode[pos_out] = in1 + in2;
pos + 4
}
2 => {
let in1 = intcode[intcode[pos + 1]];
let in2 = intcode[intcode[pos + 2]];
let pos_out = intcode[pos + 3];
println!(
"mult[{}]: value:{} * value:{} -> pos:{}",
pos, in1, in2, pos_out
);
intcode[pos_out] = in1 * in2;
pos + 4
}
99 => {
println!("end program");
pos
}
_ => {
println!("unknown code");
pos + 1
}
}
}
println!("memory dump: {:?}\n\nresult: {}", intcode, intcode[0]);
}
fn main() {
run_intcode(&mut vec![1, 0, 0, 0, 99]);
run_intcode(&mut vec![2, 3, 0, 3, 99]);
run_intcode(&mut vec![2, 4, 4, 5, 99, 0]);
run_intcode(&mut vec![1, 1, 1, 4, 99, 5, 6, 0, 99]);
let mut intcode = vec![
1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 9, 19, 23, 2, 23, 10, 27,
1, 27, 5, 31, 1, 31, 6, 35, 1, 6, 35, 39, 2, 39, 13, 43, 1, 9, 43, 47, 2, 9, 47, 51, 1, 51,
6, 55, 2, 55, 10, 59, 1, 59, 5, 63, 2, 10, 63, 67, 2, 9, 67, 71, 1, 71, 5, 75, 2, 10, 75,
79, 1, 79, 6, 83, 2, 10, 83, 87, 1, 5, 87, 91, 2, 9, 91, 95, 1, 95, 5, 99, 1, 99, 2, 103,
1, 103, 13, 0, 99, 2, 14, 0, 0,
];
intcode[1] = 12;
intcode[2] = 2;
run_intcode(&mut intcode);
}