2019: day02: part 1
This commit is contained in:
parent
9903508129
commit
c59cd75be3
1
aoc2019/input/day02.txt
Normal file
1
aoc2019/input/day02.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,19,5,23,1,9,23,27,2,27,6,31,1,5,31,35,2,9,35,39,2,6,39,43,2,43,13,47,2,13,47,51,1,10,51,55,1,9,55,59,1,6,59,63,2,63,9,67,1,67,6,71,1,71,13,75,1,6,75,79,1,9,79,83,2,9,83,87,1,87,6,91,1,91,13,95,2,6,95,99,1,10,99,103,2,103,9,107,1,6,107,111,1,10,111,115,2,6,115,119,1,5,119,123,1,123,13,127,1,127,5,131,1,6,131,135,2,135,13,139,1,139,2,143,1,143,10,0,99,2,0,14,0
|
82
aoc2019/src/day02.rs
Normal file
82
aoc2019/src/day02.rs
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
use aoc::err;
|
||||||
|
use aoc::Result;
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../input/day02.txt");
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run() -> Result<()> {
|
||||||
|
let intcode = parse_intcode(INPUT)?;
|
||||||
|
println!("part 1: {}", part1(&mut intcode.clone())?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
input[1] = 12;
|
||||||
|
input[2] = 2;
|
||||||
|
|
||||||
|
eval(input)?;
|
||||||
|
|
||||||
|
Ok(input[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[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 = parse_intcode("1,0,0,0,99").unwrap();
|
||||||
|
eval(&mut intcode).unwrap();
|
||||||
|
assert_eq!(intcode, &[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 = 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 = 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_real() {
|
||||||
|
let mut intcode = parse_intcode(INPUT).unwrap();
|
||||||
|
assert_eq!(part1(&mut intcode).unwrap(), 6568671);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod day01;
|
pub mod day01;
|
||||||
|
pub mod day02;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use aoc2019::day01;
|
use aoc2019::day01;
|
||||||
|
use aoc2019::day02;
|
||||||
|
|
||||||
use aoc2019::Result;
|
use aoc2019::Result;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[fn() -> Result<()>] = &[day01::run];
|
let days: &[fn() -> Result<()>] = &[day01::run, day02::run];
|
||||||
|
|
||||||
let mut args = env::args();
|
let mut args = env::args();
|
||||||
args.next();
|
args.next();
|
||||||
|
|
Loading…
Reference in a new issue