2019-12-06 19:06:49 +01:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2020-12-14 19:24:43 +01:00
|
|
|
use anyhow::{Context, Result};
|
2019-12-05 15:45:19 +01:00
|
|
|
|
2019-12-07 18:21:36 +01:00
|
|
|
use crate::intcode::Intcode;
|
|
|
|
|
2019-12-05 15:45:19 +01:00
|
|
|
const INPUT: &str = include_str!("../input/day05.txt");
|
|
|
|
|
2019-12-06 19:06:49 +01:00
|
|
|
pub fn run() -> Result<String> {
|
|
|
|
let mut res = String::with_capacity(128);
|
|
|
|
|
|
|
|
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
|
|
|
writeln!(res, "part 2: {}", part2(INPUT)?)?;
|
2019-12-05 16:12:50 +01:00
|
|
|
|
2019-12-06 19:06:49 +01:00
|
|
|
Ok(res)
|
2019-12-05 15:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn part1(input: &str) -> Result<i64> {
|
|
|
|
let mut intcode = Intcode::new(input)?;
|
|
|
|
intcode.add_input(1);
|
|
|
|
intcode.run()?;
|
2020-12-14 19:24:43 +01:00
|
|
|
intcode.get_last_output().context("intcode gave no output")
|
2019-12-05 15:45:19 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:12:50 +01:00
|
|
|
fn part2(input: &str) -> Result<i64> {
|
|
|
|
let mut intcode = Intcode::new(input)?;
|
|
|
|
intcode.add_input(5);
|
|
|
|
intcode.run()?;
|
2020-12-14 19:24:43 +01:00
|
|
|
intcode.get_last_output().context("intcode gave no output")
|
2019-12-05 16:12:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:45:19 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part1_real() {
|
|
|
|
assert_eq!(part1(INPUT).unwrap(), 16225258);
|
|
|
|
}
|
2019-12-05 16:12:50 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_real() {
|
|
|
|
assert_eq!(part2(INPUT).unwrap(), 2808771);
|
|
|
|
}
|
2019-12-05 15:45:19 +01:00
|
|
|
}
|