3 Commits

Author SHA1 Message Date
Stefan Bühler 5abf4a2916 day8 tests 2 years ago
Stefan Bühler e4e5d1c092 day 8 + 9 2 years ago
Stefan Bühler 19b70128d3 intcode: decode opcode into proper structure/enums 2 years ago
  1. 129
      Cargo.lock
  2. 2
      Cargo.toml
  3. 9
      day8/Cargo.toml
  4. 1
      day8/src/input.txt
  5. 62
      day8/src/main.rs
  6. 10
      day9/Cargo.toml
  7. 1
      day9/src/input.txt
  8. 46
      day9/src/main.rs
  9. 1
      shared/Cargo.toml
  10. 229
      shared/src/intcode.rs

129
Cargo.lock

@ -1,5 +1,35 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "backtrace"
version = "0.3.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "backtrace-sys"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cc"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day1"
version = "0.1.0"
@ -38,11 +68,42 @@ dependencies = [
"shared 0.1.0",
]
[[package]]
name = "day8"
version = "0.1.0"
[[package]]
name = "day9"
version = "0.1.0"
dependencies = [
"shared 0.1.0",
]
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "failure"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)",
"failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "failure_derive"
version = "0.1.6"
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)",
"syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itertools"
version = "0.8.2"
@ -51,10 +112,78 @@ dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.66"
source = "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 = "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 = "rustc-demangle"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "shared"
version = "0.1.0"
dependencies = [
"failure 0.1.6 (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 = "synstructure"
version = "0.12.3"
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)",
"syn 1.0.11 (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 = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea"
"checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491"
"checksum cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "f52a465a666ca3d838ebbf08b241383421412fe7ebb463527bba275526d89f76"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9"
"checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08"
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
"checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"

2
Cargo.toml

@ -7,5 +7,7 @@ members = [
"day5",
"day6",
"day7",
"day8",
"day9",
"shared",
]

9
day8/Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "day8"
version = "0.1.0"
authors = ["Stefan Bühler <stbuehler@web.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
day8/src/input.txt
File diff suppressed because it is too large
View File

62
day8/src/main.rs

@ -0,0 +1,62 @@
fn merge_layers(layers: &[&[u8]]) -> Vec<u8> {
let mut image = Vec::new();
image.resize(layers[0].len(), 2);
for layer in layers {
for (ndx, b) in layer.into_iter().enumerate() {
let digit = *b - b'0';
if image[ndx] != 2 { continue; }
image[ndx] = digit;
}
}
image
}
fn split_layers(input: &[u8], image_size: usize) -> Vec<&[u8]> {
assert_eq!(input.len() % image_size, 0);
input.chunks(image_size).collect::<Vec<_>>()
}
fn main() {
let input = include_bytes!("input.txt");
let layers = split_layers(input, 6*25);
let min0_product12 = layers.iter().map(|layer| {
let mut count = [0, 0, 0];
for b in *layer {
let digit = *b - b'0';
if digit < 3 {
count[digit as usize] += 1;
}
}
(count[0], count[1]*count[2])
}).min_by_key(|(zeroes, _product12)| *zeroes).unwrap().1;
println!("Product of counts of 1 and 2 for the layer with min 0s: {}", min0_product12);
let image = merge_layers(&layers);
for row in image.chunks(25) {
for cell in row {
match cell {
1 => print!("#"),
_ => print!(" "),
}
}
println!("");
}
}
#[cfg(test)]
mod day8test {
use super::*;
#[test]
fn examples1() {
// no runnable examples
}
#[test]
fn examples2() {
let layers = split_layers(b"0222112222120000", 4);
assert_eq!(merge_layers(&layers), [0, 1, 1, 0]);
}
}

10
day9/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "day9"
version = "0.1.0"
authors = ["Stefan Bühler <stbuehler@web.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
shared = { path = "../shared" }

1
day9/src/input.txt

@ -0,0 +1 @@
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1102,1,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,902,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,39,1005,1102,1,1,1021,1101,0,212,1025,1101,0,24,1014,1102,22,1,1019,1101,0,35,1003,1101,38,0,1002,1101,0,571,1026,1102,32,1,1006,1102,31,1,1000,1102,25,1,1018,1102,1,37,1016,1101,0,820,1023,1102,1,29,1004,1101,564,0,1027,1101,0,375,1028,1101,26,0,1013,1102,1,370,1029,1101,21,0,1007,1101,0,0,1020,1102,1,30,1001,1102,36,1,1011,1102,1,27,1017,1101,0,28,1012,1101,0,217,1024,1101,823,0,1022,1102,1,20,1009,1101,0,23,1010,1101,34,0,1015,1101,33,0,1008,109,5,1208,0,39,63,1005,63,199,4,187,1106,0,203,1001,64,1,64,1002,64,2,64,109,13,2105,1,6,4,209,1105,1,221,1001,64,1,64,1002,64,2,64,109,-4,21108,40,39,-1,1005,1013,241,1001,64,1,64,1105,1,243,4,227,1002,64,2,64,109,5,21102,41,1,-1,1008,1018,40,63,1005,63,267,1001,64,1,64,1106,0,269,4,249,1002,64,2,64,109,-28,1202,10,1,63,1008,63,30,63,1005,63,291,4,275,1106,0,295,1001,64,1,64,1002,64,2,64,109,24,21107,42,43,-4,1005,1011,313,4,301,1106,0,317,1001,64,1,64,1002,64,2,64,109,-8,21108,43,43,3,1005,1010,335,4,323,1105,1,339,1001,64,1,64,1002,64,2,64,109,-8,1207,4,34,63,1005,63,359,1001,64,1,64,1106,0,361,4,345,1002,64,2,64,109,26,2106,0,3,4,367,1106,0,379,1001,64,1,64,1002,64,2,64,109,-21,2102,1,-2,63,1008,63,37,63,1005,63,399,1105,1,405,4,385,1001,64,1,64,1002,64,2,64,109,2,1207,-2,30,63,1005,63,427,4,411,1001,64,1,64,1105,1,427,1002,64,2,64,109,4,2108,36,-5,63,1005,63,447,1001,64,1,64,1106,0,449,4,433,1002,64,2,64,109,-13,1201,8,0,63,1008,63,41,63,1005,63,469,1106,0,475,4,455,1001,64,1,64,1002,64,2,64,109,14,21107,44,43,3,1005,1014,495,1001,64,1,64,1106,0,497,4,481,1002,64,2,64,109,2,1205,8,511,4,503,1106,0,515,1001,64,1,64,1002,64,2,64,109,14,1206,-6,527,1105,1,533,4,521,1001,64,1,64,1002,64,2,64,109,-29,2107,31,8,63,1005,63,551,4,539,1105,1,555,1001,64,1,64,1002,64,2,64,109,28,2106,0,1,1001,64,1,64,1106,0,573,4,561,1002,64,2,64,109,-3,21101,45,0,-4,1008,1019,45,63,1005,63,595,4,579,1105,1,599,1001,64,1,64,1002,64,2,64,109,-23,1208,2,39,63,1005,63,615,1105,1,621,4,605,1001,64,1,64,1002,64,2,64,109,15,2108,32,-9,63,1005,63,643,4,627,1001,64,1,64,1105,1,643,1002,64,2,64,109,-9,2107,33,0,63,1005,63,659,1106,0,665,4,649,1001,64,1,64,1002,64,2,64,109,7,21101,46,0,2,1008,1015,49,63,1005,63,689,1001,64,1,64,1106,0,691,4,671,1002,64,2,64,109,-8,2101,0,-3,63,1008,63,35,63,1005,63,711,1105,1,717,4,697,1001,64,1,64,1002,64,2,64,109,12,1202,-9,1,63,1008,63,31,63,1005,63,741,1001,64,1,64,1105,1,743,4,723,1002,64,2,64,109,-27,2102,1,10,63,1008,63,31,63,1005,63,769,4,749,1001,64,1,64,1105,1,769,1002,64,2,64,109,9,2101,0,1,63,1008,63,31,63,1005,63,791,4,775,1106,0,795,1001,64,1,64,1002,64,2,64,109,28,1206,-7,809,4,801,1105,1,813,1001,64,1,64,1002,64,2,64,2105,1,-4,1106,0,829,4,817,1001,64,1,64,1002,64,2,64,109,-15,21102,47,1,-2,1008,1010,47,63,1005,63,851,4,835,1106,0,855,1001,64,1,64,1002,64,2,64,109,5,1205,3,867,1106,0,873,4,861,1001,64,1,64,1002,64,2,64,109,-12,1201,0,0,63,1008,63,39,63,1005,63,895,4,879,1105,1,899,1001,64,1,64,4,64,99,21101,0,27,1,21102,913,1,0,1106,0,920,21201,1,47951,1,204,1,99,109,3,1207,-2,3,63,1005,63,962,21201,-2,-1,1,21101,0,940,0,1105,1,920,21201,1,0,-1,21201,-2,-3,1,21101,0,955,0,1106,0,920,22201,1,-1,-2,1105,1,966,21202,-2,1,-2,109,-3,2105,1,0

46
day9/src/main.rs

@ -0,0 +1,46 @@
use shared::intcode::{IntCode, SimulateStep};
fn run_checks(mut ic: IntCode) {
ic.next_input = Some(1);
loop {
match ic.simulate_step() {
SimulateStep::Finished => break,
SimulateStep::Output(o) => println!("Check output: {}", o),
SimulateStep::WaitForInput => panic!("No further input"),
}
}
}
fn main() {
let mut ic = include_str!("input.txt").parse::<IntCode>().unwrap();
run_checks(ic.clone());
ic.next_input = Some(2);
println!("BOOST: {}", ic.simulate_step().expect_output());
}
#[cfg(test)]
mod day9test {
use super::*;
#[test]
fn examples1() {
assert_eq!(
"109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99".parse::<IntCode>().unwrap().simulate_io(None),
[109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99],
);
assert_eq!(
"1102,34915192,34915192,7,4,7,99,0".parse::<IntCode>().unwrap().simulate_io(None),
[1219070632396864], // "16-digit number"
);
assert_eq!(
"104,1125899906842624,99".parse::<IntCode>().unwrap().simulate_io(None),
[1125899906842624],
);
}
#[test]
fn examples2() {
// no examples
}
}

1
shared/Cargo.toml

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
failure = "0.1.6"

229
shared/src/intcode.rs

@ -1,15 +1,84 @@
const OP_ADD: CELL = 1;
const OP_MUL: CELL = 2;
const OP_INPUT: CELL = 3;
const OP_OUTPUT: CELL = 4;
const OP_JUMP_IF_TRUE: CELL = 5;
const OP_JUMP_IF_FALSE: CELL = 6;
const OP_LESS_THAN: CELL = 7;
const OP_EQUAL: CELL = 8;
const OP_HALT: CELL = 99;
use failure::Error;
#[repr(u8)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
pub enum ParameterMode {
Position = 0,
Immediate = 1,
Relative = 2,
}
impl ParameterMode {
pub fn decode(code: CELL) -> Result<(Self, CELL), Error> {
let rem = code / 10;
let mode = match code % 10 {
0 => Self::Position,
1 => Self::Immediate,
2 => Self::Relative,
m => failure::bail!("Unknown parameter mode: {}", m),
};
Ok((mode, rem))
}
}
#[repr(u8)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
pub enum Instruction {
Add = 1,
Mul = 2,
In = 3,
Out = 4,
JumpIfTrue = 5,
JumpIfFalse = 6,
LessThan = 7,
Equal = 8,
AdjustBase = 9,
Halt = 99,
}
impl Instruction {
pub fn decode(opcode: CELL) -> Result<(Self, CELL), Error> {
let rem = opcode / 100;
let instr = match opcode % 100 {
1 => Self::Add,
2 => Self::Mul,
3 => Self::In,
4 => Self::Out,
5 => Self::JumpIfTrue,
6 => Self::JumpIfFalse,
7 => Self::LessThan,
8 => Self::Equal,
9 => Self::AdjustBase,
99 => Self::Halt,
n => failure::bail!("Unknown instruction code {}", n),
};
Ok((instr, rem))
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
pub struct OpCode {
pub instruction: Instruction,
pub modes: [ParameterMode; 3],
}
impl OpCode {
pub fn decode(opcode: CELL) -> Result<OpCode, Error> {
let (instruction, code) = Instruction::decode(opcode)?;
let (mode0, code) = ParameterMode::decode(code)?;
let (mode1, code) = ParameterMode::decode(code)?;
let (mode2, code) = ParameterMode::decode(code)?;
failure::ensure!(code == 0);
let modes = [mode0, mode1, mode2];
Ok(OpCode {
instruction,
modes,
})
}
}
pub type CELL = i64;
type Mode = u8;
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum SimulateStep {
@ -39,97 +108,144 @@ impl SimulateStep {
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct IntCode {
/// instruction pointer
pub ip: usize,
/// base for relative mode
pub base: CELL,
/// next value to use for an "IN" (3) instruction (which will clear it)
pub next_input: Option<CELL>,
/// memory of machine (both code and data)
pub data: Vec<CELL>,
}
impl IntCode {
fn read(&self, addr: CELL) -> CELL {
assert!(addr >= 0, "Tried reading negative address");
self.data.get(addr as usize).cloned().unwrap_or(0)
}
fn write(&mut self, addr: CELL, value: CELL) {
assert!(addr >= 0, "Tried reading negative address");
let addr = addr as usize;
if addr >= self.data.len() {
self.data.resize(addr + 1, 0);
}
self.data[addr] = value;
}
#[inline(always)]
fn fetch(&mut self, mode: Mode) -> CELL {
let imm = self.data[self.ip];
fn fetch(&mut self, modes: &mut &[ParameterMode]) -> CELL {
let mode = modes[0];
*modes = &modes[1..];
let imm = self.read(self.ip as CELL);
self.ip += 1;
match mode {
1 => imm,
_ => self.data[imm as usize],
ParameterMode::Position => self.read(imm),
ParameterMode::Immediate => imm,
ParameterMode::Relative => self.read(self.base + imm),
}
}
#[inline(always)]
fn store(&mut self, mode: Mode, value: CELL) {
let addr = self.ip;
fn store(&mut self, modes: &mut &[ParameterMode], value: CELL) {
let mode = modes[0];
*modes = &modes[1..];
let addr = self.ip as CELL;
self.ip += 1;
match mode {
1 => self.data[addr] = value,
_ => {
let addr = self.data[addr] as usize;
self.data[addr] = value;
},
}
let addr = match mode {
ParameterMode::Position => self.read(addr),
ParameterMode::Immediate => addr,
ParameterMode::Relative => self.base + self.read(addr),
};
self.write(addr, value);
}
fn binop<F>(&mut self, op: F, modes: [Mode; 3])
fn binop<F>(&mut self, op: F, modes: &mut &[ParameterMode])
where
F: FnOnce(CELL, CELL) -> CELL,
{
let src1 = self.fetch(modes[0]);
let src2 = self.fetch(modes[1]);
self.store(modes[2], op(src1, src2));
let src1 = self.fetch(modes);
let src2 = self.fetch(modes);
self.store(modes, op(src1, src2));
}
/// run simple machine without I/O to completion
pub fn simulate(&mut self) {
assert_eq!(self.ip, 0);
assert!(self.next_input.is_none());
self.simulate_step().expect_finished();
}
pub fn simulate_io<I>(&mut self, i: I) -> Vec<CELL>
where
I: IntoIterator<Item = CELL>,
{
assert_eq!(self.ip, 0);
assert!(self.next_input.is_none());
let mut i = i.into_iter().fuse();
let mut buf = Vec::new();
loop {
match self.simulate_step() {
SimulateStep::Finished => break,
SimulateStep::WaitForInput => {
self.next_input = Some(i.next().expect("no further input"));
},
SimulateStep::Output(n) => buf.push(n),
}
}
if i.next().is_some() {
panic!("Input not used completely");
}
buf
}
/// run a machine until:
/// - it needs input, but `next_input` is `None`
/// - it outputs a value
/// - it finishes
pub fn simulate_step(&mut self) -> SimulateStep {
loop {
let modes = self.data[self.ip];
let opcode = OpCode::decode(self.data[self.ip]).unwrap();
let modes = &mut &opcode.modes[..];
self.ip += 1;
let op = modes % 100;
let modes = modes / 100;
let mode1 = (modes % 10) as Mode;
let modes = modes / 10;
let mode2 = (modes % 10) as Mode;
let modes = modes / 10;
let mode3 = (modes % 10) as Mode;
let modes = [mode1, mode2, mode3];
match op {
OP_ADD => self.binop(|a, b| a + b, modes),
OP_MUL => self.binop(|a, b| a * b, modes),
OP_LESS_THAN => self.binop(|a, b| if a < b { 1 } else { 0 }, modes),
OP_EQUAL => self.binop(|a, b| if a == b { 1 } else { 0 }, modes),
OP_INPUT => {
match opcode.instruction {
Instruction::Add => self.binop(|a, b| a + b, modes),
Instruction::Mul => self.binop(|a, b| a * b, modes),
Instruction::LessThan => self.binop(|a, b| if a < b { 1 } else { 0 }, modes),
Instruction::Equal => self.binop(|a, b| if a == b { 1 } else { 0 }, modes),
Instruction::In => {
if let Some(v) = self.next_input.take() {
self.store(mode1, v);
self.store(modes, v);
} else {
self.ip -= 1; // go back to opcode
return SimulateStep::WaitForInput;
}
},
OP_OUTPUT => {
return SimulateStep::Output(self.fetch(mode1));
Instruction::Out => {
return SimulateStep::Output(self.fetch(modes));
},
OP_HALT => {
Instruction::Halt => {
self.ip -= 1; // make re-running this step end up here again
return SimulateStep::Finished;
},
OP_JUMP_IF_TRUE => {
if self.fetch(mode1) != 0 {
self.ip = self.fetch(mode2) as usize;
Instruction::JumpIfTrue => {
if self.fetch(modes) != 0 {
self.ip = self.fetch(modes) as usize;
} else {
self.ip += 1; // skip target
self.ip += 1; // skip parameter for target
}
},
OP_JUMP_IF_FALSE => {
if self.fetch(mode1) == 0 {
self.ip = self.fetch(mode2) as usize;
Instruction::JumpIfFalse => {
if self.fetch(modes) == 0 {
self.ip = self.fetch(modes) as usize;
} else {
self.ip += 1; // skip target
self.ip += 1; // skip parameter for target
}
},
_ => panic!("invalid opcode: {}", op),
Instruction::AdjustBase => {
self.base += self.fetch(modes);
}
}
}
}
@ -141,6 +257,7 @@ impl std::str::FromStr for IntCode {
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(IntCode {
ip: 0,
base: 0,
next_input: None,
data: s.split(',').map(|s| s.trim().parse::<CELL>()).collect::<Result<_, _>>()?,
})

Loading…
Cancel
Save