diff --git a/day13/Cargo.lock b/day13/Cargo.lock new file mode 100644 index 0000000..07a2d33 --- /dev/null +++ b/day13/Cargo.lock @@ -0,0 +1,137 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day13" +version = "0.1.0" +dependencies = [ + "num_enum 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "derivative" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_enum" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "num_enum_derive 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_enum_derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-crate" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "toml" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "942ca430eef7a3806595a6737bc388bf51adb888d3fc0dd1b50f1c170167ee3a" +"checksum num_enum 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "be601e38e20a6f3d01049d85801cb9b7a34a8da7a0da70df507bbde7735058c8" +"checksum num_enum_derive 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b59f30f6a043f2606adbd0addbf1eef6f2e28e8c4968918b63b7ff97ac0db2a7" +"checksum proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e10d4b51f154c8a7fb96fd6dad097cb74b863943ec010ac94b9fd1be8861fe1e" +"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +"checksum proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0319972dcae462681daf4da1adeeaa066e3ebd29c69be96c6abb1259d2ee2bcc" +"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" +"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +"checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" +"checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/day13/Cargo.toml b/day13/Cargo.toml new file mode 100644 index 0000000..f905214 --- /dev/null +++ b/day13/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day13" +version = "0.1.0" +authors = ["Tyler Hallada "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num_enum = "0.4.2" diff --git a/day13/input/input.txt b/day13/input/input.txt new file mode 100644 index 0000000..eb1b0f9 --- /dev/null +++ b/day13/input/input.txt @@ -0,0 +1 @@ +2,380,379,385,1008,2311,194223,381,1005,381,12,99,109,2312,1101,0,0,383,1101,0,0,382,20102,1,382,1,21002,383,1,2,21101,0,37,0,1106,0,578,4,382,4,383,204,1,1001,382,1,382,1007,382,38,381,1005,381,22,1001,383,1,383,1007,383,22,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1101,-1,0,384,1106,0,119,1007,392,36,381,1006,381,161,1101,0,1,384,21002,392,1,1,21101,0,20,2,21101,0,0,3,21102,138,1,0,1106,0,549,1,392,384,392,21002,392,1,1,21102,20,1,2,21102,1,3,3,21102,1,161,0,1106,0,549,1101,0,0,384,20001,388,390,1,20102,1,389,2,21102,180,1,0,1106,0,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21002,389,1,2,21101,205,0,0,1105,1,393,1002,390,-1,390,1101,0,1,384,20101,0,388,1,20001,389,391,2,21102,228,1,0,1105,1,578,1206,1,261,1208,1,2,381,1006,381,253,21002,388,1,1,20001,389,391,2,21101,0,253,0,1106,0,393,1002,391,-1,391,1102,1,1,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,0,279,0,1106,0,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21102,304,1,0,1105,1,393,1002,390,-1,390,1002,391,-1,391,1101,1,0,384,1005,384,161,20101,0,388,1,21002,389,1,2,21101,0,0,3,21102,1,338,0,1106,0,549,1,388,390,388,1,389,391,389,20102,1,388,1,21001,389,0,2,21101,0,4,3,21101,365,0,0,1106,0,549,1007,389,21,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,255,17,17,1,1,19,109,3,22101,0,-2,1,21201,-1,0,2,21102,1,0,3,21101,414,0,0,1105,1,549,21201,-2,0,1,21202,-1,1,2,21101,429,0,0,1105,1,601,2101,0,1,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2106,0,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,22102,1,-3,-7,109,-8,2105,1,0,109,4,1202,-2,38,566,201,-3,566,566,101,639,566,566,1202,-1,1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,38,593,201,-2,593,593,101,639,593,593,21001,0,0,-2,109,-3,2105,1,0,109,3,22102,22,-2,1,22201,1,-1,1,21101,0,421,2,21102,594,1,3,21101,0,836,4,21101,630,0,0,1106,0,456,21201,1,1475,-2,109,-3,2105,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,0,2,0,0,2,0,0,0,2,2,0,2,2,2,0,2,0,0,2,0,2,2,0,0,0,0,0,2,0,2,2,0,0,0,1,1,0,2,0,0,2,0,0,0,2,0,0,0,2,0,2,0,2,0,2,0,2,2,2,0,2,0,2,0,0,2,0,2,2,0,0,0,1,1,0,2,2,0,2,0,2,2,0,0,0,2,2,0,2,0,0,0,0,2,2,0,2,2,2,0,2,2,0,2,0,2,2,0,0,0,1,1,0,2,2,2,2,2,2,0,2,2,2,2,0,0,2,2,0,0,2,2,2,2,2,0,0,0,0,2,2,2,2,2,2,2,0,0,1,1,0,2,2,0,2,0,2,2,2,2,2,2,0,0,2,2,2,2,2,2,0,0,2,0,0,0,2,2,2,2,0,2,0,0,0,0,1,1,0,2,0,0,0,2,0,2,0,2,2,2,0,0,2,2,0,0,2,2,2,0,0,0,0,0,2,2,2,0,0,2,0,2,0,0,1,1,0,2,2,0,0,2,2,2,0,0,0,0,0,2,0,0,2,2,2,2,0,2,0,0,2,0,0,2,2,0,0,0,0,2,2,0,1,1,0,0,0,2,0,2,0,2,0,2,2,0,2,2,2,0,2,2,0,2,2,0,0,0,2,0,0,0,2,2,2,0,0,0,0,0,1,1,0,0,0,2,0,0,2,2,0,2,0,2,0,2,0,0,2,0,2,0,0,0,2,2,2,2,2,2,0,2,2,2,2,2,2,0,1,1,0,0,2,0,2,2,2,0,0,2,0,2,0,2,2,2,0,0,2,2,0,2,0,2,2,2,2,2,2,2,0,2,0,2,0,0,1,1,0,2,0,2,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,2,0,2,2,0,2,0,2,2,2,2,2,2,0,1,1,0,2,2,0,0,0,2,2,2,0,0,2,2,0,2,0,2,2,0,2,2,2,0,0,2,2,0,2,2,0,0,2,2,2,2,0,1,1,0,0,2,0,0,2,0,0,0,0,0,0,2,2,0,2,2,2,0,0,0,0,0,0,0,2,0,0,2,0,0,2,2,0,2,0,1,1,0,2,0,0,2,2,2,0,2,2,2,2,0,0,2,2,0,2,0,2,2,2,2,2,2,2,0,2,2,2,2,2,2,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,64,86,97,12,60,6,31,61,53,79,45,82,7,4,9,5,60,42,15,39,54,90,43,57,68,25,79,48,37,54,2,55,31,6,48,97,78,53,17,72,15,58,88,20,60,98,39,43,49,7,32,59,11,32,67,3,96,42,4,6,34,58,24,37,15,87,72,83,76,30,89,70,86,55,42,15,62,21,65,88,35,52,79,16,20,94,18,98,68,58,83,25,72,39,92,66,41,50,92,25,90,17,86,53,89,64,12,54,98,97,49,31,43,59,33,55,58,11,27,62,30,23,20,4,24,13,37,16,82,66,57,58,28,97,35,84,89,13,1,57,1,55,90,98,17,22,43,70,33,6,77,9,17,6,70,72,15,22,72,75,35,84,93,74,10,69,8,6,85,39,7,98,55,39,66,39,52,60,63,3,49,59,28,37,8,84,75,6,98,37,8,19,91,74,54,42,70,94,18,10,1,34,67,10,5,75,98,59,35,77,54,59,88,14,28,10,72,11,30,85,35,88,94,44,3,71,2,91,63,71,4,50,23,38,75,95,17,20,28,88,34,30,93,79,63,61,75,40,75,47,72,25,15,49,62,64,91,72,5,36,90,45,52,80,48,19,70,45,7,72,44,5,39,11,27,32,97,98,73,51,33,56,9,54,33,36,4,10,84,82,20,28,9,41,26,78,96,5,61,20,44,70,59,69,59,48,24,91,88,46,2,67,14,89,44,82,40,25,53,91,11,61,55,88,95,55,92,9,81,76,59,76,94,2,34,3,57,61,11,87,18,23,77,94,72,88,1,95,77,64,3,77,2,6,42,52,79,27,69,59,33,36,20,44,6,45,36,9,10,51,12,64,11,62,83,36,50,61,85,20,16,81,36,94,54,17,72,28,26,53,47,42,38,72,87,59,17,63,8,12,48,22,77,45,42,33,6,29,87,53,66,35,32,32,24,72,31,96,17,83,62,1,66,54,96,1,37,74,53,26,55,9,22,69,66,46,40,97,9,85,10,51,38,70,44,5,59,59,87,25,90,73,11,74,63,33,33,25,65,69,80,20,30,32,10,86,29,18,67,77,76,89,9,55,89,70,95,49,38,89,58,45,52,44,35,66,19,48,82,67,60,92,66,38,21,54,6,6,86,29,45,2,24,13,35,51,20,5,61,1,47,88,50,45,78,80,46,81,17,26,7,34,28,41,14,15,79,23,8,57,69,58,92,66,92,70,59,40,74,28,21,33,77,27,95,93,67,7,14,68,29,44,98,14,51,25,64,10,60,67,9,6,69,25,41,78,81,32,35,96,89,29,69,35,93,61,25,35,71,61,97,40,67,36,29,77,42,34,31,59,47,63,22,19,39,6,42,33,79,4,76,38,75,5,1,29,31,38,3,64,35,33,19,90,43,47,30,43,86,33,76,4,85,66,26,98,91,33,59,93,6,78,27,31,22,89,78,86,70,49,83,81,15,20,8,2,13,30,18,22,73,53,37,48,66,93,46,27,62,72,55,65,9,83,20,32,41,12,63,20,16,55,98,31,20,46,27,17,93,84,59,15,90,29,72,13,83,88,21,49,29,67,47,7,7,12,38,36,25,16,20,80,63,55,46,27,51,72,79,94,68,75,34,41,24,91,72,64,90,81,82,93,96,47,1,57,75,81,56,14,57,58,54,24,40,40,71,46,16,3,34,79,46,28,42,9,55,87,85,23,14,11,98,15,31,28,44,81,96,94,10,51,44,57,11,55,31,15,9,93,76,92,69,12,25,27,82,43,80,54,18,58,6,82,59,81,65,96,38,69,2,28,86,70,22,66,10,5,88,56,79,31,77,48,61,34,87,7,17,21,37,16,26,68,64,72,30,3,6,88,26,24,3,77,50,34,67,79,31,3,77,26,72,51,23,25,194223 diff --git a/day13/src/intcode.rs b/day13/src/intcode.rs new file mode 100644 index 0000000..20fbfa8 --- /dev/null +++ b/day13/src/intcode.rs @@ -0,0 +1,461 @@ +use std::collections::HashMap; +use std::convert::TryFrom; +use std::error::Error; +use std::fs::File; +use std::io::prelude::*; +use std::result; +use std::str::FromStr; + +use num_enum::TryFromPrimitive; + +type Result = result::Result>; + +#[derive(Debug, Clone, PartialEq)] +pub struct Intcode { + pub integers: HashMap, + pub pointer: usize, + pub halted: bool, + pub relative_base: i64, +} + +#[derive(Debug, PartialEq)] +pub struct Instruction { + opcode: Opcode, + parameter_modes: Vec, +} + +impl TryFrom for Instruction { + type Error = Box; + + fn try_from(integer: i64) -> Result { + let opcode: Opcode = Opcode::try_from((integer % 100) as u8)?; + let modes_integer = integer / 100; + let mut parameter_modes = vec![]; + for parameter_index in 0..opcode.parameter_count() { + parameter_modes.push(ParameterMode::try_from( + (modes_integer % (10_i64.pow(parameter_index + 1)) / 10_i64.pow(parameter_index)) + as u8, + )?) + } + Ok(Instruction { + opcode, + parameter_modes, + }) + } +} + +#[derive(Debug, PartialEq, TryFromPrimitive)] +#[repr(u8)] +pub enum Opcode { + Add = 1, + Mult = 2, + Input = 3, + Output = 4, + JumpIfTrue = 5, + JumpIfFalse = 6, + LessThan = 7, + Equals = 8, + RelativeBaseOffset = 9, + Halt = 99, +} + +impl Opcode { + pub fn parameter_count(&self) -> u32 { + match self { + Opcode::Add => 3, + Opcode::Mult => 3, + Opcode::Input => 1, + Opcode::Output => 1, + Opcode::JumpIfTrue => 2, + Opcode::JumpIfFalse => 2, + Opcode::LessThan => 3, + Opcode::Equals => 3, + Opcode::RelativeBaseOffset => 1, + Opcode::Halt => 0, + } + } + + pub fn target_parameter_index(&self) -> Option { + match self { + Opcode::Add => Some(2), + Opcode::Mult => Some(2), + Opcode::Input => Some(0), + Opcode::Output => None, + Opcode::JumpIfTrue => None, + Opcode::JumpIfFalse => None, + Opcode::LessThan => Some(2), + Opcode::Equals => Some(2), + Opcode::RelativeBaseOffset => None, + Opcode::Halt => None, + } + } +} + +#[derive(Debug, PartialEq, TryFromPrimitive)] +#[repr(u8)] +pub enum ParameterMode { + Position = 0, + Immediate = 1, + Relative = 2, +} + +impl FromStr for Intcode { + type Err = Box; + + fn from_str(s: &str) -> Result { + let intcode_string = s.trim().to_string(); + let mut integers = HashMap::new(); + for (index, code) in intcode_string.split(',').enumerate() { + integers.insert(index, code.parse().unwrap()); + } + + Ok(Intcode::new(integers)) + } +} + +impl Intcode { + fn new(integers: HashMap) -> Intcode { + Intcode { + integers, + pointer: 0, + halted: false, + relative_base: 0, + } + } + + fn load_parameters(&mut self, pointer: usize, instruction: &Instruction) -> Vec { + (0..instruction.opcode.parameter_count() as usize) + .map(|parameter_index| { + let mut integer = *self + .integers + .entry(pointer + parameter_index + 1) + .or_insert(0); + match instruction.parameter_modes[parameter_index] { + ParameterMode::Position => match instruction.opcode.target_parameter_index() { + Some(target_parameter_index) + if target_parameter_index == parameter_index => {} + _ => { + integer = *self.integers.entry(integer as usize).or_insert(0); + } + }, + ParameterMode::Relative => match instruction.opcode.target_parameter_index() { + Some(target_parameter_index) + if target_parameter_index == parameter_index => + { + integer += self.relative_base; + } + _ => { + integer = *self + .integers + .entry((self.relative_base + integer) as usize) + .or_insert(0); + } + }, + _ => {} + } + integer + }) + .collect() + } + + pub fn execute(&mut self, inputs: &[i64]) -> Result> { + let mut input_index = 0; + let mut output = vec![]; + + loop { + let instruction = + Instruction::try_from(*self.integers.entry(self.pointer).or_insert(0))?; + let parameters = self.load_parameters(self.pointer, &instruction); + let mut jump_pointer: Option = None; + + match instruction.opcode { + Opcode::Add => { + self.integers + .insert(parameters[2] as usize, parameters[0] + parameters[1]); + } + Opcode::Mult => { + self.integers + .insert(parameters[2] as usize, parameters[0] * parameters[1]); + } + Opcode::Input => { + if input_index >= inputs.len() { + break; // pause execution to wait for more input + } + self.integers + .insert(parameters[0] as usize, inputs[input_index]); + input_index += 1; + } + Opcode::Output => { + output.push(parameters[0]); + } + Opcode::JumpIfTrue => { + if parameters[0] != 0 { + jump_pointer = Some(parameters[1] as usize); + } + } + Opcode::JumpIfFalse => { + if parameters[0] == 0 { + jump_pointer = Some(parameters[1] as usize); + } + } + Opcode::LessThan => { + if parameters[0] < parameters[1] { + self.integers.insert(parameters[2] as usize, 1); + } else { + self.integers.insert(parameters[2] as usize, 0); + } + } + Opcode::Equals => { + if parameters[0] == parameters[1] { + self.integers.insert(parameters[2] as usize, 1); + } else { + self.integers.insert(parameters[2] as usize, 0); + } + } + Opcode::RelativeBaseOffset => { + self.relative_base += parameters[0]; + } + Opcode::Halt => { + self.halted = true; + break; + } + } + + match jump_pointer { + Some(jump_pointer) => self.pointer = jump_pointer, + None => self.pointer += 1 + instruction.opcode.parameter_count() as usize, + } + } + + Ok(output) + } +} + +pub fn read_intcode(filename: &str) -> Result { + let mut file = File::open(filename)?; + let mut intcode_string = String::new(); + file.read_to_string(&mut intcode_string)?; + + Ok(intcode_string.parse()?) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "input/test1.txt"; + + #[test] + fn reads_intcode() { + assert_eq!( + read_intcode(TEST_INPUT).unwrap(), + Intcode::new( + vec![3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0] + .into_iter() + .enumerate() + .collect() + ), + ); + } + + #[test] + fn converts_integer_to_instruction() { + assert_eq!( + Instruction::try_from(1002).unwrap(), + Instruction { + opcode: Opcode::Mult, + parameter_modes: vec![ + ParameterMode::Position, + ParameterMode::Immediate, + ParameterMode::Position + ], + } + ); + + assert_eq!( + Instruction::try_from(101).unwrap(), + Instruction { + opcode: Opcode::Add, + parameter_modes: vec![ + ParameterMode::Immediate, + ParameterMode::Position, + ParameterMode::Position + ], + } + ); + } + + #[test] + fn executes_intcodes() { + let mut intcode = Intcode::new(vec![1, 0, 0, 0, 99].into_iter().enumerate().collect()); + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![2, 0, 0, 0, 99].into_iter().enumerate().collect() + ); + + let mut intcode = Intcode::new(vec![2, 3, 0, 3, 99].into_iter().enumerate().collect()); + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![2, 3, 0, 6, 99].into_iter().enumerate().collect() + ); + + let mut intcode = Intcode::new(vec![2, 4, 4, 5, 99, 0].into_iter().enumerate().collect()); + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![2, 4, 4, 5, 99, 9801].into_iter().enumerate().collect() + ); + + let mut intcode = Intcode::new( + vec![1, 1, 1, 4, 99, 5, 6, 0, 99] + .into_iter() + .enumerate() + .collect(), + ); + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![30, 1, 1, 4, 2, 5, 6, 0, 99] + .into_iter() + .enumerate() + .collect() + ); + + let mut intcode = Intcode::new( + vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] + .into_iter() + .enumerate() + .collect(), + ); + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] + .into_iter() + .enumerate() + .collect() + ); + } + + #[test] + fn less_and_equal_outputs() { + let intcode = Intcode::new( + vec![3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + + let intcode = Intcode::new( + vec![3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![0]); + + let intcode = Intcode::new( + vec![3, 3, 1108, -1, 8, 3, 4, 3, 99] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + + let intcode = Intcode::new( + vec![3, 3, 1107, -1, 8, 3, 4, 3, 99] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![0]); + } + + #[test] + fn jump_outputs() { + let intcode = Intcode::new( + vec![3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + assert_eq!(intcode.clone().execute(&[1]).unwrap(), vec![1]); + + let intcode = Intcode::new( + vec![3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + assert_eq!(intcode.clone().execute(&[1]).unwrap(), vec![1]); + } + + #[test] + fn larger_part2_intcode() { + let intcode = Intcode::new( + vec![ + 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, + 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, + 1, 20, 4, 20, 1105, 1, 46, 98, 99, + ] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![999]); + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1000]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![1001]); + } + + #[test] + fn multiple_input_intcode() { + let intcode = Intcode::new( + vec![ + 3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0, + ] + .into_iter() + .enumerate() + .collect(), + ); + assert_eq!(intcode.clone().execute(&[1, 1]).unwrap(), vec![11]); + } + + #[test] + fn relative_base_offset_quine() { + let code = vec![ + 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99, + ]; + let intcode = Intcode::new(code.clone().into_iter().enumerate().collect()); + assert_eq!(intcode.clone().execute(&[]).unwrap(), code); + } + + #[test] + fn sixteen_digit_output() { + let code = vec![1102, 34915192, 34915192, 7, 4, 7, 99, 0]; + let intcode = Intcode::new(code.into_iter().enumerate().collect()); + assert_eq!(intcode.clone().execute(&[]).unwrap(), [1219070632396864]); + } + + #[test] + fn large_output() { + let code = vec![104, 1125899906842624, 99]; + let intcode = Intcode::new(code.into_iter().enumerate().collect()); + assert_eq!(intcode.clone().execute(&[]).unwrap(), [1125899906842624]); + } + + #[test] + fn relative_target_parameters() { + let code = vec![109, 1, 203, 2, 204, 2, 99]; + let intcode = Intcode::new(code.into_iter().enumerate().collect()); + assert_eq!(intcode.clone().execute(&[123]).unwrap(), [123]); + } +} diff --git a/day13/src/main.rs b/day13/src/main.rs new file mode 100644 index 0000000..e8083be --- /dev/null +++ b/day13/src/main.rs @@ -0,0 +1,146 @@ +use std::collections::HashMap; +use std::convert::TryFrom; +use std::error::Error; +use std::result; +use std::fmt; + +use num_enum::TryFromPrimitive; + +mod intcode; + +use intcode::{read_intcode, Intcode}; + +const INPUT: &str = "input/input.txt"; + +type Result = result::Result>; + +#[derive(Debug, PartialEq, TryFromPrimitive)] +#[repr(u8)] +enum Tile { + Empty = 0, + Wall = 1, + Block = 2, + HorizontalPaddle = 3, + Ball = 4, +} + +#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] +struct Coordinate { + x: i64, + y: i64, +} + +#[derive(Debug)] +struct Game { + tiles: HashMap, + score: i64, +} + +impl Game { + fn new() -> Game { + Game { + tiles: HashMap::new(), + score: 0, + } + } +} + +impl fmt::Display for Game { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!(f, "Score: {}", self.score)?; + let start_coord = Coordinate { x: 0, y: 0 }; + let up_left_corner = Coordinate { + x: self + .tiles + .keys() + .min_by_key(|coord| coord.x) + .unwrap_or(&start_coord) + .x, + y: self + .tiles + .keys() + .min_by_key(|coord| coord.y) + .unwrap_or(&start_coord) + .y, + }; + let down_right_corner = Coordinate { + x: self + .tiles + .keys() + .max_by_key(|coord| coord.x) + .unwrap_or(&start_coord) + .x, + y: self + .tiles + .keys() + .max_by_key(|coord| coord.y) + .unwrap_or(&start_coord) + .y, + }; + for y in up_left_corner.y..=down_right_corner.y { + let mut row_string = String::new(); + for x in up_left_corner.x..=down_right_corner.x { + row_string += match self + .tiles + .get(&Coordinate { x, y }) + .unwrap_or(&Tile::Empty) + { + Tile::Empty => " ", + Tile::Wall => "|", + Tile::Block => "#", + Tile::HorizontalPaddle => "=", + Tile::Ball => "o", + }; + } + write!(f, "{}\n", row_string)?; + } + Ok(()) + } +} + +fn solve_part1() -> Result { + let mut intcode = read_intcode(INPUT)?; + let output = intcode.execute(&[0]).expect("Failed to execute intcode"); + let mut game = Game::new(); + for index in (0..output.len()).step_by(3) { + let x = output[index]; + let y = output[index + 1]; + let tile = Tile::try_from(output[index + 2] as u8)?; + game.tiles.insert(Coordinate { x, y }, tile); + } + Ok(game.tiles.values().fold(0, |acc, tile| { + if *tile == Tile::Block { + return acc + 1 + } + acc + })) +} + +fn solve_part2() -> Result { + let mut intcode = read_intcode(INPUT)?; + let mut output = intcode.execute(&[]).expect("Failed to execute intcode"); + let mut game = Game::new(); + while !intcode.halted { + for index in (0..output.len()).step_by(3) { + if output[index] == -1 { + game.score = output[index + 2]; + } else { + let x = output[index]; + let y = output[index + 1]; + let tile = Tile::try_from(output[index + 2] as u8)?; + game.tiles.insert(Coordinate { x, y}, tile); + } + } + print!("{}", game); + // TODO: move the joystick correctly + output = intcode.execute(&[0]).expect("Failed to execute intcode"); + } + Ok(game.score) +} + +fn main() -> Result<()> { + println!("Part 1: {}", solve_part1()?); + println!("Part 2: {}", solve_part2()?); + + Ok(()) +}