2019: day07: part 1

This commit is contained in:
Antoine Martin 2019-12-07 21:32:06 +01:00
parent 72444351a8
commit 27f6a072da
5 changed files with 96 additions and 0 deletions

View file

@ -6,6 +6,7 @@ use aoc2019::day03;
use aoc2019::day04; use aoc2019::day04;
use aoc2019::day05; use aoc2019::day05;
use aoc2019::day06; use aoc2019::day06;
use aoc2019::day07;
fn aoc2019_all(c: &mut Criterion) { fn aoc2019_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
@ -14,6 +15,7 @@ fn aoc2019_all(c: &mut Criterion) {
c.bench_function("day04", |b| b.iter(|| day04::run().unwrap())); c.bench_function("day04", |b| b.iter(|| day04::run().unwrap()));
c.bench_function("day05", |b| b.iter(|| day05::run().unwrap())); c.bench_function("day05", |b| b.iter(|| day05::run().unwrap()));
c.bench_function("day06", |b| b.iter(|| day06::run().unwrap())); c.bench_function("day06", |b| b.iter(|| day06::run().unwrap()));
c.bench_function("day07", |b| b.iter(|| day07::run().unwrap()));
} }
criterion_group! { criterion_group! {

1
aoc2019/input/day07.txt Normal file
View file

@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,46,67,88,101,126,207,288,369,450,99999,3,9,1001,9,5,9,1002,9,5,9,1001,9,5,9,102,3,9,9,101,2,9,9,4,9,99,3,9,102,4,9,9,101,5,9,9,102,5,9,9,101,3,9,9,4,9,99,3,9,1001,9,3,9,102,2,9,9,1001,9,5,9,102,4,9,9,4,9,99,3,9,102,3,9,9,1001,9,4,9,4,9,99,3,9,102,3,9,9,1001,9,3,9,1002,9,2,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,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,99,3,9,101,1,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,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,101,2,9,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,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,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,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99

90
aoc2019/src/day07.rs Normal file
View file

@ -0,0 +1,90 @@
use std::collections::VecDeque;
use std::fmt::Write;
use aoc::err;
use aoc::Result;
use crate::intcode::{parse_memory, Intcode};
const INPUT: &str = include_str!("../input/day07.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
Ok(res)
}
fn permutations_rec(used: &mut Vec<i64>, available: &mut VecDeque<i64>, res: &mut Vec<Vec<i64>>) {
if available.is_empty() {
res.push(used.clone());
} else {
for _ in 0..available.len() {
used.push(available.pop_front().unwrap());
permutations_rec(used, available, res);
available.push_back(used.pop().unwrap());
}
}
}
fn permutations(array: &[i64]) -> Vec<Vec<i64>> {
let mut res = Vec::new();
permutations_rec(
&mut Vec::new(),
&mut VecDeque::from(array.to_vec()),
&mut res,
);
res
}
fn part1(input: &str) -> Result<i64> {
let memory = parse_memory(input)?;
let combinations = permutations(&[0, 1, 2, 3, 4]);
let mut res = 0;
for combination in combinations {
let mut output = 0;
for phase in combination.iter() {
let mut intcode = Intcode::with_memory(memory.clone());
intcode.add_input(*phase);
intcode.add_input(output);
intcode.run()?;
output = intcode
.get_day05_output()
.ok_or_else(|| err!("no output at end of pipeline!"))?;
}
res = std::cmp::max(res, output);
}
Ok(res)
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDED1: &str = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0";
const PROVIDED2: &str =
"3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0";
const PROVIDED3: &str = "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0";
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED1).unwrap(), 43210);
assert_eq!(part1(PROVIDED2).unwrap(), 54321);
assert_eq!(part1(PROVIDED3).unwrap(), 65210);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 844468);
}
}

View file

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

View file

@ -7,6 +7,7 @@ use aoc2019::day03;
use aoc2019::day04; use aoc2019::day04;
use aoc2019::day05; use aoc2019::day05;
use aoc2019::day06; use aoc2019::day06;
use aoc2019::day07;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[ let days: &[DayFunc] = &[
@ -16,6 +17,7 @@ fn main() -> Result<()> {
day04::run, day04::run,
day05::run, day05::run,
day06::run, day06::run,
day07::run,
]; ];
aoc::run(days) aoc::run(days)