diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2019/day1/.gitignore b/2019/day1/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/2019/day1/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/2019/day2/Cargo.lock b/2019/day2/Cargo.lock new file mode 100644 index 0000000..e459401 --- /dev/null +++ b/2019/day2/Cargo.lock @@ -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" diff --git a/2019/day2/Cargo.toml b/2019/day2/Cargo.toml new file mode 100644 index 0000000..c11170b --- /dev/null +++ b/2019/day2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day2" +version = "0.1.0" +authors = ["genofire "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2019/day2/src/main.rs b/2019/day2/src/main.rs new file mode 100644 index 0000000..9071f2c --- /dev/null +++ b/2019/day2/src/main.rs @@ -0,0 +1,58 @@ +fn run_intcode(intcode: &mut std::vec::Vec) { + 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); +}