2019: intcode: split to own module
This commit is contained in:
parent
e468ec2952
commit
72444351a8
5 changed files with 346 additions and 341 deletions
|
|
@ -3,67 +3,50 @@ use std::fmt::Write;
|
|||
use aoc::err;
|
||||
use aoc::Result;
|
||||
|
||||
const INPUT: &str = include_str!("../input/day02.txt");
|
||||
const PART2_EXPECTED: usize = 19_690_720;
|
||||
use crate::intcode::{parse_memory, Intcode};
|
||||
|
||||
fn parse_intcode(input: &str) -> Result<Vec<usize>> {
|
||||
input
|
||||
.trim_end()
|
||||
.split(',')
|
||||
.map(|x| x.parse().map_err(|e| err!("couldn't parse int: {}", e)))
|
||||
.collect()
|
||||
}
|
||||
const INPUT: &str = include_str!("../input/day02.txt");
|
||||
const PART2_EXPECTED: i64 = 19_690_720;
|
||||
|
||||
pub fn run() -> Result<String> {
|
||||
let mut res = String::with_capacity(128);
|
||||
|
||||
let intcode = parse_intcode(INPUT)?;
|
||||
let memory = parse_memory(INPUT)?;
|
||||
|
||||
writeln!(res, "part 1: {}", part1(&mut intcode.clone())?)?;
|
||||
writeln!(res, "part 2: {}", part2(&intcode, PART2_EXPECTED)?)?;
|
||||
writeln!(res, "part 1: {}", part1(memory.clone())?)?;
|
||||
writeln!(res, "part 2: {}", part2(&memory, PART2_EXPECTED)?)?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn eval(intcode: &mut [usize]) -> Result<()> {
|
||||
let mut pc = 0;
|
||||
|
||||
while intcode[pc] != 99 {
|
||||
let op1 = intcode[pc + 1];
|
||||
let op2 = intcode[pc + 2];
|
||||
let res = intcode[pc + 3];
|
||||
|
||||
match intcode[pc] {
|
||||
1 => intcode[res] = intcode[op1] + intcode[op2],
|
||||
2 => intcode[res] = intcode[op1] * intcode[op2],
|
||||
_ => return Err(err!("unknown opcode: {}", intcode[pc])),
|
||||
};
|
||||
|
||||
pc += 4;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn part1(input: &mut Vec<usize>) -> Result<usize> {
|
||||
fn part1(mut input: Vec<i64>) -> Result<i64> {
|
||||
input[1] = 12;
|
||||
input[2] = 2;
|
||||
|
||||
eval(input)?;
|
||||
let mut intcode = Intcode::with_memory(input);
|
||||
intcode.run()?;
|
||||
|
||||
Ok(input[0])
|
||||
intcode
|
||||
.get_day02_output()
|
||||
.ok_or_else(|| err!("intcode memory was empty!"))
|
||||
}
|
||||
|
||||
fn part2(input: &[usize], res: usize) -> Result<usize> {
|
||||
fn part2(input: &[i64], res: i64) -> Result<i64> {
|
||||
for (noun, verb) in (0..=99).flat_map(|noun| (0..=99).map(move |verb| (noun, verb))) {
|
||||
let mut test_input = input.to_vec();
|
||||
test_input[1] = noun;
|
||||
test_input[2] = verb;
|
||||
|
||||
eval(&mut test_input)?;
|
||||
let mut intcode = Intcode::with_memory(test_input);
|
||||
intcode.run()?;
|
||||
|
||||
if test_input[0] == res {
|
||||
return Ok(noun * 100 + verb);
|
||||
match intcode.get_day02_output() {
|
||||
Some(val) => {
|
||||
if val == res {
|
||||
return Ok(noun * 100 + verb);
|
||||
}
|
||||
}
|
||||
None => return Err(err!("intcode memory was empty!")),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,36 +62,39 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn part1_provided() {
|
||||
let mut intcode = parse_intcode("1,9,10,3,2,3,11,0,99,30,40,50").unwrap();
|
||||
eval(&mut intcode).unwrap();
|
||||
assert_eq!(intcode, &[3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]);
|
||||
let mut intcode = Intcode::new("1,9,10,3,2,3,11,0,99,30,40,50").unwrap();
|
||||
intcode.run().unwrap();
|
||||
assert_eq!(
|
||||
&intcode.memory,
|
||||
&[3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]
|
||||
);
|
||||
|
||||
let mut intcode = parse_intcode("1,0,0,0,99").unwrap();
|
||||
eval(&mut intcode).unwrap();
|
||||
assert_eq!(intcode, &[2, 0, 0, 0, 99]);
|
||||
let mut intcode = Intcode::new("1,0,0,0,99").unwrap();
|
||||
intcode.run().unwrap();
|
||||
assert_eq!(&intcode.memory, &[2, 0, 0, 0, 99]);
|
||||
|
||||
let mut intcode = parse_intcode("2,3,0,3,99").unwrap();
|
||||
eval(&mut intcode).unwrap();
|
||||
assert_eq!(intcode, &[2, 3, 0, 6, 99]);
|
||||
let mut intcode = Intcode::new("2,3,0,3,99").unwrap();
|
||||
intcode.run().unwrap();
|
||||
assert_eq!(&intcode.memory, &[2, 3, 0, 6, 99]);
|
||||
|
||||
let mut intcode = parse_intcode("2,4,4,5,99,0").unwrap();
|
||||
eval(&mut intcode).unwrap();
|
||||
assert_eq!(intcode, &[2, 4, 4, 5, 99, 9801]);
|
||||
let mut intcode = Intcode::new("2,4,4,5,99,0").unwrap();
|
||||
intcode.run().unwrap();
|
||||
assert_eq!(&intcode.memory, &[2, 4, 4, 5, 99, 9801]);
|
||||
|
||||
let mut intcode = parse_intcode("1,1,1,4,99,5,6,0,99").unwrap();
|
||||
eval(&mut intcode).unwrap();
|
||||
assert_eq!(intcode, &[30, 1, 1, 4, 2, 5, 6, 0, 99]);
|
||||
let mut intcode = Intcode::new("1,1,1,4,99,5,6,0,99").unwrap();
|
||||
intcode.run().unwrap();
|
||||
assert_eq!(&intcode.memory, &[30, 1, 1, 4, 2, 5, 6, 0, 99]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_real() {
|
||||
let mut intcode = parse_intcode(INPUT).unwrap();
|
||||
assert_eq!(part1(&mut intcode).unwrap(), 6568671);
|
||||
let memory = parse_memory(INPUT).unwrap();
|
||||
assert_eq!(part1(memory).unwrap(), 6568671);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_real() {
|
||||
let intcode = parse_intcode(INPUT).unwrap();
|
||||
assert_eq!(part2(&intcode, PART2_EXPECTED).unwrap(), 3951);
|
||||
let memory = parse_memory(INPUT).unwrap();
|
||||
assert_eq!(part2(&memory, PART2_EXPECTED).unwrap(), 3951);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue