Browse Source

Add day 2 part 2

master
parent
commit
cc6ca01b72
  1. 33
      src/day2.rs
  2. 5
      src/main.rs

33
src/day2.rs

@ -1,13 +1,34 @@
use std::io::BufRead;
pub fn part1<F: BufRead> (mut input: F) {
pub fn part1<F: BufRead> (input: F) {
let intcode = parse_input(input);
println!("{}", compute_result(intcode, 12, 2));
}
pub fn part2<F: BufRead> (input: F) {
let intcode = parse_input(input);
for noun in 0..99 {
for verb in 0..99 {
let res = compute_result(intcode.clone(), noun, verb);
if res == 19690720 {
println!("{}", noun * 100 + verb);
return;
}
}
}
}
fn parse_input<F: BufRead> (mut input: F) -> Vec<usize> {
let mut buf = String::new();
let _num_bytes = input.read_line(&mut buf).expect("Unable to first read line of input file");
let mut intcode: Vec<_> = buf.trim().split(",").filter_map(|n| n.parse::<usize>().ok()).collect();
intcode[1] = 12;
intcode[2] = 2;
let output = run_intcode(intcode, 0);
println!("{}", output[0]);
buf.trim().split(",").filter_map(|n| n.parse().ok()).collect()
}
fn compute_result(mut src: Vec<usize>, param1: usize, param2: usize) -> usize {
src[1] = param1;
src[2] = param2;
let output = run_intcode(src, 0);
output[0]
}
fn compute_op(mut src: Vec<usize>, index: usize) -> Vec<usize> {

5
src/main.rs

@ -36,12 +36,13 @@ fn main() {
let challenges = [
day1::part1, day1::part2,
day2::part1,
day2::part1, day2::part2,
];
if let Some(f) = challenges.get(challenge) {
println!("Running day {}, part {}", challenge / 2 + 1, challenge % 2 +1);
f(reader);
} else {
println!("Challenge {}, part {} not unimplemented", challenge / 2 + 1, challenge % 2 +1)
println!("Challenge {}, part {} not unimplemented", challenge / 2 + 1, challenge % 2 + 1)
}
}

Loading…
Cancel
Save