Day 2
This commit is contained in:
parent
03ebc5a5cf
commit
176bef0c0c
|
@ -1,3 +1,4 @@
|
||||||
bin/
|
bin/
|
||||||
zig-out/
|
zig-out/
|
||||||
zig-cache/
|
zig-cache/
|
||||||
|
target/
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1 @@
|
||||||
|
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,9,19,23,1,13,23,27,1,5,27,31,2,31,6,35,1,35,5,39,1,9,39,43,1,43,5,47,1,47,5,51,2,10,51,55,1,5,55,59,1,59,5,63,2,63,9,67,1,67,5,71,2,9,71,75,1,75,5,79,1,10,79,83,1,83,10,87,1,10,87,91,1,6,91,95,2,95,6,99,2,99,9,103,1,103,6,107,1,13,107,111,1,13,111,115,2,115,9,119,1,119,6,123,2,9,123,127,1,127,5,131,1,131,5,135,1,135,5,139,2,10,139,143,2,143,10,147,1,147,5,151,1,151,2,155,1,155,13,0,99,2,14,0,0
|
|
@ -0,0 +1,105 @@
|
||||||
|
fn main() {
|
||||||
|
let contents = std::fs::read_to_string("input")
|
||||||
|
.expect("Failed to read file 'input'");
|
||||||
|
let it = contents.split(",");
|
||||||
|
let mut program = Vec::new();
|
||||||
|
for val in it {
|
||||||
|
if val.ends_with('\n') {
|
||||||
|
let mut s = String::from(val);
|
||||||
|
s.pop();
|
||||||
|
program.push(s.parse::<i32>().unwrap());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
program.push(val.parse::<i32>().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a copy of the memory to refresh from
|
||||||
|
let backup = program.clone();
|
||||||
|
|
||||||
|
// Modify state for part 1
|
||||||
|
program[1] = 12;
|
||||||
|
program[2] = 2;
|
||||||
|
simulate(&mut program);
|
||||||
|
println!("[Part 1] The value at position 0 is: {}", program[0]);
|
||||||
|
|
||||||
|
let mut noun = 0;
|
||||||
|
while noun < 100 {
|
||||||
|
let mut verb = 0;
|
||||||
|
while verb < 100 {
|
||||||
|
program = backup.clone();
|
||||||
|
program[1] = noun;
|
||||||
|
program[2] = verb;
|
||||||
|
simulate(&mut program);
|
||||||
|
//println!("Noun {}, verb {}: {}", noun, verb, program[0]);
|
||||||
|
if program[0] == 19690720 {
|
||||||
|
println!("[Part 2] noun {}, verb {}, sum {}", noun, verb, 100*noun+verb);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
verb += 1;
|
||||||
|
}
|
||||||
|
noun += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simulate(v: &mut Vec<i32>) {
|
||||||
|
let mut pos = 0;
|
||||||
|
loop {
|
||||||
|
let code = v[pos];
|
||||||
|
if code == 1 {
|
||||||
|
let i1 = v[pos+1] as usize;
|
||||||
|
let i2 = v[pos+2] as usize;
|
||||||
|
let idest = v[pos+3] as usize;
|
||||||
|
v[idest] = v[i1] + v[i2];
|
||||||
|
pos += 4;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if code == 2 {
|
||||||
|
let i1 = v[pos+1] as usize;
|
||||||
|
let i2 = v[pos+2] as usize;
|
||||||
|
let idest = v[pos+3] as usize;
|
||||||
|
v[idest] = v[i1] * v[i2];
|
||||||
|
pos += 4;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if code == 99 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
panic!("Unknown opcode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_op_1() {
|
||||||
|
let mut program = vec![1, 0, 0, 0, 99];
|
||||||
|
simulate(&mut program);
|
||||||
|
assert_eq!(program[0], 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_op_2() {
|
||||||
|
let mut program = vec![2, 3, 0, 3, 99];
|
||||||
|
simulate(&mut program);
|
||||||
|
assert_eq!(program[3], 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_case_3() {
|
||||||
|
let mut program = vec![2,4,4,5,99,0];
|
||||||
|
simulate(&mut program);
|
||||||
|
assert_eq!(program[program.len() - 1], 9801);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_case_4() {
|
||||||
|
let mut program = vec![1,1,1,4,99,5,6,0,99];
|
||||||
|
simulate(&mut program);
|
||||||
|
assert_eq!(program[0], 30);
|
||||||
|
assert_eq!(program[4], 2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue