Day 19 part 2 (manually reverse engineered the assembler instructions).

This commit is contained in:
2023-11-12 23:12:30 +01:00
parent 2792304040
commit caf307bf2d
2 changed files with 46 additions and 5 deletions

View File

@@ -1,3 +1,3 @@
fn main() {
aoc_2018::tasks::day19::task1();
aoc_2018::tasks::day19::task2();
}

View File

@@ -2,25 +2,64 @@ use std::ops::{BitAnd, BitOr};
pub fn task1() {
let input = include_str!("../../input/day19.txt");
let result = run1(input);
let result = run1(input, 1);
println!("Day 19 part 1: {result}");
}
fn run1(input: &str) -> i32 {
pub fn task2() {
let mut a = 1i64;
let mut d = 948;
if a == 1 {
d += 10550400;
a = 0;
}
let mut f = 1;
'outer: loop {
let mut c = 1;
'inner: loop {
let b = f * c;
if b == d {
a = a + f;
} else if b > d {
// this is the important part: break the loop because otherwise we count and count
// and count ... but cannot reach the b == d condition without resetting c.
break 'inner;
}
c += 1;
if c > d {
break 'inner;
}
}
f += 1;
if f > d {
break 'outer;
}
}
println!("Day 19 part 2: {a}");
}
fn run1(input: &str, start_value: i32) -> i32 {
let (config, program) = input.split_once("\n").unwrap();
let ip_register: usize = config.split_once(" ").unwrap().1.parse().unwrap();
let program: Vec<Instruction> = program.lines().map(parse_instruction).collect();
let mut registers: Registers = [0i32; 6];
let mut registers: Registers = [start_value, 0, 0, 0, 0, 0];
let mut ip = 0;
let mut count = 0;
loop {
//print!("ip={ip}, {registers:?}");
registers[ip_register] = ip as i32;
let instruction = program.get(ip).unwrap();
if ip == 2 {
println!("{registers:?}");
}
//print!("{instruction:?}");
let op: Op = instruction.opcode;
@@ -34,7 +73,9 @@ fn run1(input: &str) -> i32 {
if program.get(ip).is_none() {
break;
}
count += 1;
}
println!("executed instructions: {count}");
registers[0]
}
@@ -197,6 +238,6 @@ mod test {
setr 1 0 0\n\
seti 8 0 4\n\
seti 9 0 5";
assert_eq!(run1(input), 6);
assert_eq!(run1(input, 0), 6);
}
}