Starting day 9: use HashMap, not Vec for integers
This commit is contained in:
parent
a954425a9e
commit
ee946c4409
137
day9/Cargo.lock
generated
Normal file
137
day9/Cargo.lock
generated
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "day9"
|
||||||
|
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.6 (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.11 (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.6"
|
||||||
|
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.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.103"
|
||||||
|
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.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 1.0.6 (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.103 (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.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
|
||||||
|
"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.103 (registry+https://github.com/rust-lang/crates.io-index)" = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702"
|
||||||
|
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
|
||||||
|
"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
|
||||||
|
"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"
|
10
day9/Cargo.toml
Normal file
10
day9/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "day9"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num_enum = "0.4.2"
|
1
day9/input/input.txt
Normal file
1
day9/input/input.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1102,32,1,1016,1101,38,0,1012,1102,1,693,1022,1102,1,27,1007,1101,0,190,1025,1102,20,1,1019,1102,1,33,1008,1102,28,1,1013,1101,0,1,1021,1102,1,851,1026,1102,22,1,1018,1101,29,0,1005,1101,0,21,1004,1101,36,0,1009,1101,0,195,1024,1101,0,39,1002,1101,0,848,1027,1102,1,34,1003,1102,23,1,1015,1102,1,30,1010,1101,0,26,1017,1102,1,35,1001,1102,1,489,1028,1102,1,484,1029,1102,1,686,1023,1102,0,1,1020,1102,25,1,1006,1102,1,31,1014,1101,37,0,1000,1102,1,24,1011,109,23,2105,1,1,4,187,1105,1,199,1001,64,1,64,1002,64,2,64,109,-18,2102,1,-3,63,1008,63,41,63,1005,63,223,1001,64,1,64,1106,0,225,4,205,1002,64,2,64,109,2,2101,0,1,63,1008,63,33,63,1005,63,251,4,231,1001,64,1,64,1105,1,251,1002,64,2,64,109,13,21107,40,41,-8,1005,1012,269,4,257,1106,0,273,1001,64,1,64,1002,64,2,64,109,9,1205,-8,287,4,279,1105,1,291,1001,64,1,64,1002,64,2,64,109,-38,2101,0,9,63,1008,63,34,63,1005,63,315,1001,64,1,64,1106,0,317,4,297,1002,64,2,64,109,18,21108,41,38,1,1005,1010,337,1001,64,1,64,1106,0,339,4,323,1002,64,2,64,109,-5,2107,30,1,63,1005,63,359,1001,64,1,64,1106,0,361,4,345,1002,64,2,64,109,14,21101,42,0,-7,1008,1011,42,63,1005,63,387,4,367,1001,64,1,64,1106,0,387,1002,64,2,64,109,-18,1208,0,39,63,1005,63,403,1106,0,409,4,393,1001,64,1,64,1002,64,2,64,109,3,1207,-1,38,63,1005,63,425,1106,0,431,4,415,1001,64,1,64,1002,64,2,64,109,-8,1201,5,0,63,1008,63,35,63,1005,63,455,1001,64,1,64,1106,0,457,4,437,1002,64,2,64,109,30,1206,-4,469,1106,0,475,4,463,1001,64,1,64,1002,64,2,64,109,10,2106,0,-7,4,481,1106,0,493,1001,64,1,64,1002,64,2,64,109,-24,21102,43,1,3,1008,1014,40,63,1005,63,517,1001,64,1,64,1105,1,519,4,499,1002,64,2,64,109,-4,2108,41,-5,63,1005,63,539,1001,64,1,64,1106,0,541,4,525,1002,64,2,64,109,18,21101,44,0,-8,1008,1017,47,63,1005,63,561,1105,1,567,4,547,1001,64,1,64,1002,64,2,64,109,-24,1202,6,1,63,1008,63,27,63,1005,63,589,4,573,1106,0,593,1001,64,1,64,1002,64,2,64,109,7,1208,-5,34,63,1005,63,611,4,599,1106,0,615,1001,64,1,64,1002,64,2,64,109,-5,1207,6,37,63,1005,63,637,4,621,1001,64,1,64,1106,0,637,1002,64,2,64,109,23,1206,-6,655,4,643,1001,64,1,64,1105,1,655,1002,64,2,64,109,-10,2107,32,-8,63,1005,63,673,4,661,1105,1,677,1001,64,1,64,1002,64,2,64,109,5,2105,1,2,1001,64,1,64,1106,0,695,4,683,1002,64,2,64,109,-17,1202,0,1,63,1008,63,20,63,1005,63,715,1106,0,721,4,701,1001,64,1,64,1002,64,2,64,109,-4,1201,4,0,63,1008,63,21,63,1005,63,743,4,727,1106,0,747,1001,64,1,64,1002,64,2,64,109,10,1205,10,763,1001,64,1,64,1105,1,765,4,753,1002,64,2,64,109,1,21102,45,1,1,1008,1012,45,63,1005,63,787,4,771,1105,1,791,1001,64,1,64,1002,64,2,64,109,-4,2102,1,-2,63,1008,63,29,63,1005,63,813,4,797,1105,1,817,1001,64,1,64,1002,64,2,64,109,-4,2108,33,5,63,1005,63,835,4,823,1105,1,839,1001,64,1,64,1002,64,2,64,109,23,2106,0,1,1106,0,857,4,845,1001,64,1,64,1002,64,2,64,109,-12,21108,46,46,1,1005,1015,879,4,863,1001,64,1,64,1106,0,879,1002,64,2,64,109,10,21107,47,46,-5,1005,1019,899,1001,64,1,64,1105,1,901,4,885,4,64,99,21101,27,0,1,21101,915,0,0,1105,1,922,21201,1,52134,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,0,942,0,1105,1,922,22101,0,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,21201,-2,0,-2,109,-3,2106,0,0
|
1
day9/input/test1.txt
Normal file
1
day9/input/test1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
|
407
day9/src/intcode.rs
Normal file
407
day9/src/intcode.rs
Normal file
@ -0,0 +1,407 @@
|
|||||||
|
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<T> = result::Result<T, Box<dyn Error>>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct Intcode {
|
||||||
|
pub integers: HashMap<usize, i32>,
|
||||||
|
pub pointer: usize,
|
||||||
|
pub halted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct Instruction {
|
||||||
|
opcode: Opcode,
|
||||||
|
parameter_modes: Vec<ParameterMode>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<i32> for Instruction {
|
||||||
|
type Error = Box<dyn Error>;
|
||||||
|
|
||||||
|
fn try_from(integer: i32) -> Result<Self> {
|
||||||
|
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(match opcode.target_parameter_index() {
|
||||||
|
Some(target_parameter_index)
|
||||||
|
if target_parameter_index == parameter_index as usize =>
|
||||||
|
{
|
||||||
|
ParameterMode::Position
|
||||||
|
}
|
||||||
|
_ => ParameterMode::try_from(
|
||||||
|
(modes_integer % (10_i32.pow(parameter_index + 1))
|
||||||
|
/ 10_i32.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,
|
||||||
|
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::Halt => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn target_parameter_index(&self) -> Option<usize> {
|
||||||
|
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::Halt => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, TryFromPrimitive)]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum ParameterMode {
|
||||||
|
Position = 0,
|
||||||
|
Immediate = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Intcode {
|
||||||
|
type Err = Box<dyn Error>;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Intcode> {
|
||||||
|
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<usize, i32>) -> Intcode {
|
||||||
|
Intcode {
|
||||||
|
integers,
|
||||||
|
pointer: 0,
|
||||||
|
halted: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn load_parameters(&self, pointer: usize, instruction: &Instruction) -> Vec<i32> {
|
||||||
|
(0..instruction.opcode.parameter_count() as usize)
|
||||||
|
.map(|parameter_index| {
|
||||||
|
let mut integer = self.integers[&(pointer + parameter_index + 1)];
|
||||||
|
if let ParameterMode::Position = instruction.parameter_modes[parameter_index] {
|
||||||
|
match instruction.opcode.target_parameter_index() {
|
||||||
|
Some(target_parameter_index)
|
||||||
|
if target_parameter_index == parameter_index => {}
|
||||||
|
_ => {
|
||||||
|
integer = self.integers[&(integer as usize)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
integer
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute(&mut self, inputs: &[i32]) -> Result<Vec<i32>> {
|
||||||
|
let mut input_index = 0;
|
||||||
|
let mut output = vec![];
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let instruction = Instruction::try_from(self.integers[&self.pointer])?;
|
||||||
|
let parameters = self.load_parameters(self.pointer, &instruction);
|
||||||
|
let mut jump_pointer: Option<usize> = 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::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<Intcode> {
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
30
day9/src/main.rs
Normal file
30
day9/src/main.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::result;
|
||||||
|
|
||||||
|
mod intcode;
|
||||||
|
|
||||||
|
use intcode::{Intcode, read_intcode};
|
||||||
|
|
||||||
|
const INPUT: &str = "input/input.txt";
|
||||||
|
|
||||||
|
type Result<T> = result::Result<T, Box<dyn Error>>;
|
||||||
|
|
||||||
|
fn solve_part1() -> Result<u32> {
|
||||||
|
Ok(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_part2() -> Result<u32> {
|
||||||
|
Ok(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
println!("Part 1: {}", solve_part1()?);
|
||||||
|
println!("Part 2: {}", solve_part2()?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user