day07 task 1

This commit is contained in:
Johannes
2019-12-08 20:26:31 +01:00
parent 864e4f7446
commit 0db2018217
5 changed files with 35 additions and 5 deletions

1
input/day07.txt Normal file
View File

@@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,42,63,76,101,114,195,276,357,438,99999,3,9,101,2,9,9,102,5,9,9,1001,9,3,9,1002,9,5,9,4,9,99,3,9,101,4,9,9,102,5,9,9,1001,9,5,9,102,2,9,9,4,9,99,3,9,1001,9,3,9,1002,9,5,9,4,9,99,3,9,1002,9,2,9,101,5,9,9,102,3,9,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,3,9,9,102,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99

View File

@@ -1,5 +1,5 @@
mod tasks;
fn main() {
tasks::day05::run();
tasks::day07::run();
}

View File

@@ -9,7 +9,7 @@ pub fn run() {
task2(ram.clone());
}
struct IntCodeComputer {
pub struct IntCodeComputer {
input_storage: Vec<i32>,
output_storage: Vec<i32>,
pc: usize,
@@ -140,7 +140,7 @@ impl Op {
}
impl IntCodeComputer {
fn new(input: Vec<i32>, memory: Vec<i32>) -> Self {
pub fn new(input: Vec<i32>, memory: Vec<i32>) -> Self {
IntCodeComputer {
input_storage: input,
output_storage: Vec::new(),
@@ -149,7 +149,7 @@ impl IntCodeComputer {
}
}
fn run_until_end(&mut self) {
pub fn run_until_end(&mut self) {
let mut op = Op::from(self.pc, &self.ram);
let mut inputs = self.input_storage.iter();
while op.opcode != OpCode::Terminate {
@@ -212,7 +212,7 @@ impl IntCodeComputer {
}
}
fn get_output(&self) -> &[i32] {
pub fn get_output(&self) -> &[i32] {
self.output_storage.as_slice()
}
}

28
src/tasks/day07.rs Normal file
View File

@@ -0,0 +1,28 @@
use crate::tasks::day05::IntCodeComputer;
use itertools::Itertools;
#[allow(dead_code)]
pub fn run() {
let prog_code: Vec<_> = std::fs::read_to_string("input/day07.txt")
.unwrap()
.split(",")
.map(|s| s.parse::<i32>().unwrap())
.collect();
task1(prog_code.clone());
}
fn task1(input: Vec<i32>) {
let phases = vec![0, 1, 2, 3, 4];
let result = phases
.iter()
.permutations(phases.len())
.map(|perm| {
perm.iter().fold(0, |signal, phase| {
let mut computer = IntCodeComputer::new(vec![**phase, signal], input.clone());
computer.run_until_end();
*computer.get_output().last().unwrap()
})
})
.max();
println!("Task 1: best signal is {}", result.unwrap());
}

View File

@@ -3,4 +3,5 @@ pub mod day03;
pub mod day04;
pub mod day05;
pub mod day06;
pub mod day07;
pub mod day08;