day 8 + 9

This commit is contained in:
Stefan Bühler 2019-12-09 09:04:37 +01:00
parent 19b70128d3
commit e4e5d1c092
9 changed files with 175 additions and 10 deletions

11
Cargo.lock generated
View File

@ -68,6 +68,17 @@ 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"

View File

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

9
day8/Cargo.toml Normal file
View File

@ -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 Normal file

File diff suppressed because one or more lines are too long

38
day8/src/main.rs Normal file
View File

@ -0,0 +1,38 @@
fn main() {
let input = include_str!("input.txt").as_bytes();
const LAYER_SIZE: usize = 6*25;
assert_eq!(input.len() % LAYER_SIZE, 0);
let layers = input.chunks(LAYER_SIZE).collect::<Vec<_>>();
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 mut image = [2u8; LAYER_SIZE];
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;
}
}
for row in image.chunks(25) {
for cell in row {
match cell {
1 => print!("#"),
_ => print!(" "),
}
}
println!("");
}
}

10
day9/Cargo.toml Normal file
View File

@ -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 Normal file
View File

@ -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 Normal file
View File

@ -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
}
}

View File

@ -5,6 +5,7 @@ use failure::Error;
pub enum ParameterMode {
Position = 0,
Immediate = 1,
Relative = 2,
}
impl ParameterMode {
@ -13,6 +14,7 @@ impl ParameterMode {
let mode = match code % 10 {
0 => Self::Position,
1 => Self::Immediate,
2 => Self::Relative,
m => failure::bail!("Unknown parameter mode: {}", m),
};
Ok((mode, rem))
@ -30,6 +32,7 @@ pub enum Instruction {
JumpIfFalse = 6,
LessThan = 7,
Equal = 8,
AdjustBase = 9,
Halt = 99,
}
@ -45,6 +48,7 @@ impl Instruction {
6 => Self::JumpIfFalse,
7 => Self::LessThan,
8 => Self::Equal,
9 => Self::AdjustBase,
99 => Self::Halt,
n => failure::bail!("Unknown instruction code {}", n),
};
@ -106,6 +110,8 @@ impl SimulateStep {
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)
@ -113,15 +119,30 @@ pub struct IntCode {
}
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, modes: &mut &[ParameterMode]) -> CELL {
let mode = modes[0];
*modes = &modes[1..];
let imm = self.data[self.ip];
let imm = self.read(self.ip as CELL);
self.ip += 1;
match mode {
ParameterMode::Position => self.data[imm as usize],
ParameterMode::Position => self.read(imm),
ParameterMode::Immediate => imm,
ParameterMode::Relative => self.read(self.base + imm),
}
}
@ -129,15 +150,14 @@ impl IntCode {
fn store(&mut self, modes: &mut &[ParameterMode], value: CELL) {
let mode = modes[0];
*modes = &modes[1..];
let addr = self.ip;
let addr = self.ip as CELL;
self.ip += 1;
match mode {
ParameterMode::Immediate => self.data[addr] = value,
ParameterMode::Position => {
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: &mut &[ParameterMode])
@ -156,6 +176,29 @@ impl IntCode {
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
@ -200,6 +243,9 @@ impl IntCode {
self.ip += 1; // skip parameter for target
}
},
Instruction::AdjustBase => {
self.base += self.fetch(modes);
}
}
}
}
@ -211,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<_, _>>()?,
})