Day 21 part 2. Shitty bitty.

This commit is contained in:
2023-11-13 23:22:33 +01:00
parent 48ad7e7a96
commit a4c33ae3d2

View File

@@ -1,8 +1,9 @@
use std::collections::HashSet;
use std::ops::{BitAnd, BitOr};
pub fn task1() {
let input = include_str!("../../input/day21.txt");
let result = run1(input, 0);
let result = run1(input, 3007673);
println!("{result:?}");
}
@@ -15,6 +16,8 @@ fn run1(input: &str, start_value: i64) -> (i64, usize) {
let mut registers: Registers = [start_value, 0, 0, 0, 0, 0];
let mut ip = 0;
let mut count = 0usize;
let mut seen: HashSet<_> = HashSet::new();
let mut last = 0;
loop {
//print!("ip={ip}, {registers:?}");
@@ -31,14 +34,29 @@ fn run1(input: &str, start_value: i64) -> (i64, usize) {
ip = registers[ip_register] as usize;
ip += 1;
// uncomment this and print look at register 2 to find the value for register 0
// Part 1: uncomment this and print look at register 2 to find the value for register 0
// that would cause the program to halt
// if ip == 28 {
// println!("Value in register 2: {}", registers[2]);
// }
// Part 2: 9969507 too high
// 16774755 highest value
// Store all values of register 2 at the possible exit point. Once you see one you already
// saw: you've seen all (there is a loop). The one before that was the one it took the longest
// to reach without seeing anything twice.
if ip == 28 {
println!("{registers:?}");
break;
if seen.contains(&registers[2]) {
println!("First double: {registers:?}");
println!("Last was {last}");
break;
} else {
seen.insert(registers[2]);
last = registers[2];
}
}
if program.get(ip).is_none() {
println!("exit by leaving instruction space");
break;
}
count += 1;