advent-of-code/aoc2019/src/day05.rs

51 lines
1,021 B
Rust
Raw Normal View History

use std::fmt::Write;
2019-12-05 15:45:19 +01:00
use aoc::err;
use aoc::Result;
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");
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
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()?;
intcode
2019-12-07 18:21:36 +01:00
.get_day05_output()
2019-12-05 15:45:19 +01:00
.ok_or_else(|| err!("intcode gave no output"))
}
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()?;
intcode
2019-12-07 18:21:36 +01:00
.get_day05_output()
2019-12-05 16:12:50 +01:00
.ok_or_else(|| err!("intcode gave no output"))
}
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
}