Completed day 5 part 1

Also refactored the code
This commit is contained in:
Tyler Hallada 2019-12-07 14:05:18 -05:00
parent 7845628111
commit 3b0fe76e68
5 changed files with 408 additions and 0 deletions

137
day5/Cargo.lock generated Normal file
View File

@ -0,0 +1,137 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day5"
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
day5/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "day5"
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
day5/input/input.txt Normal file
View File

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1102,9,19,225,1,136,139,224,101,-17,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,2,218,213,224,1001,224,-4560,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1102,25,63,224,101,-1575,224,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1102,55,31,225,1101,38,15,225,1001,13,88,224,1001,224,-97,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,1002,87,88,224,101,-3344,224,224,4,224,102,8,223,223,1001,224,7,224,1,224,223,223,1102,39,10,225,1102,7,70,225,1101,19,47,224,101,-66,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1102,49,72,225,102,77,166,224,101,-5544,224,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,101,32,83,224,101,-87,224,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1101,80,5,225,1101,47,57,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,226,224,1002,223,2,223,1005,224,329,1001,223,1,223,107,226,677,224,1002,223,2,223,1006,224,344,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,359,1001,223,1,223,8,677,226,224,102,2,223,223,1005,224,374,101,1,223,223,108,226,677,224,102,2,223,223,1006,224,389,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,404,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,419,1001,223,1,223,1008,226,226,224,102,2,223,223,1005,224,434,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,449,101,1,223,223,1007,677,226,224,102,2,223,223,1005,224,464,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,1107,226,677,224,1002,223,2,223,1005,224,494,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,509,101,1,223,223,1007,226,226,224,1002,223,2,223,1005,224,524,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,539,101,1,223,223,8,226,226,224,1002,223,2,223,1006,224,554,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,569,101,1,223,223,1108,677,226,224,1002,223,2,223,1005,224,584,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,599,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,614,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,644,101,1,223,223,108,226,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,674,1001,223,1,223,4,223,99,226

1
day5/input/test.txt Normal file
View File

@ -0,0 +1 @@
1,9,10,3,2,3,11,0,99,30,40,50

259
day5/src/main.rs Normal file
View File

@ -0,0 +1,259 @@
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;
const INPUT: &str = "input/input.txt";
type Result<T> = result::Result<T, Box<dyn Error>>;
#[derive(Debug, PartialEq)]
struct Intcode {
integers: Vec<i32>,
}
#[derive(Debug, PartialEq)]
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)]
enum Opcode {
Add = 1,
Mult = 2,
Input = 3,
Output = 4,
Halt = 99,
}
impl Opcode {
fn parameter_count(&self) -> u32 {
match self {
Opcode::Add => 3,
Opcode::Mult => 3,
Opcode::Input => 1,
Opcode::Output => 1,
Opcode::Halt => 0,
}
}
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::Halt => None,
}
}
}
#[derive(Debug, PartialEq, TryFromPrimitive)]
#[repr(u8)]
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();
Ok(Intcode {
integers: intcode_string
.split(',')
.map(|code| code.parse().unwrap())
.collect(),
})
}
}
impl Intcode {
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()
}
fn execute(&mut self, input: i32) -> Result<Vec<i32>> {
let mut pointer = 0;
let mut output = vec![];
loop {
let instruction = Instruction::try_from(self.integers[pointer])?;
let parameters = self.load_parameters(pointer, &instruction);
match instruction.opcode {
Opcode::Add => {
self.integers[parameters[2] as usize] = parameters[0] + parameters[1];
}
Opcode::Mult => {
self.integers[parameters[2] as usize] = parameters[0] * parameters[1];
}
Opcode::Input => {
self.integers[parameters[0] as usize] = input;
}
Opcode::Output => {
output.push(parameters[0]);
}
Opcode::Halt => {
break;
}
}
pointer += 1 + instruction.opcode.parameter_count() as usize;
}
Ok(output)
}
}
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()?)
}
fn solve_part1() -> Result<i32> {
let mut intcode = read_intcode(INPUT)?;
Ok(intcode.execute(1)?.into_iter().last().ok_or("No output")?)
}
// fn solve_part2() -> io::Result<i32> {
// }
fn main() -> Result<()> {
println!("Part 1: {:?}", solve_part1()?);
// println!("Part 2: {}", solve_part2()?);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_INPUT: &str = "input/test.txt";
#[test]
fn reads_intcode() {
assert_eq!(
read_intcode(TEST_INPUT).unwrap(),
Intcode {
integers: vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]
},
);
}
#[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 {
integers: vec![1, 0, 0, 0, 99],
};
intcode.execute(0).unwrap();
assert_eq!(intcode.integers, vec![2, 0, 0, 0, 99]);
let mut intcode = Intcode {
integers: vec![2, 3, 0, 3, 99],
};
intcode.execute(0).unwrap();
assert_eq!(intcode.integers, vec![2, 3, 0, 6, 99]);
let mut intcode = Intcode {
integers: vec![2, 4, 4, 5, 99, 0],
};
intcode.execute(0).unwrap();
assert_eq!(intcode.integers, vec![2, 4, 4, 5, 99, 9801]);
let mut intcode = Intcode {
integers: vec![1, 1, 1, 4, 99, 5, 6, 0, 99],
};
intcode.execute(0).unwrap();
assert_eq!(intcode.integers, vec![30, 1, 1, 4, 2, 5, 6, 0, 99]);
let mut intcode = Intcode {
integers: vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50],
};
intcode.execute(0).unwrap();
assert_eq!(
intcode.integers,
vec![3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]
);
}
}