Day 21 part 2. Shitty bitty.
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
use std::ops::{BitAnd, BitOr};
|
use std::ops::{BitAnd, BitOr};
|
||||||
|
|
||||||
pub fn task1() {
|
pub fn task1() {
|
||||||
let input = include_str!("../../input/day21.txt");
|
let input = include_str!("../../input/day21.txt");
|
||||||
let result = run1(input, 0);
|
let result = run1(input, 3007673);
|
||||||
println!("{result:?}");
|
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 registers: Registers = [start_value, 0, 0, 0, 0, 0];
|
||||||
let mut ip = 0;
|
let mut ip = 0;
|
||||||
let mut count = 0usize;
|
let mut count = 0usize;
|
||||||
|
let mut seen: HashSet<_> = HashSet::new();
|
||||||
|
let mut last = 0;
|
||||||
loop {
|
loop {
|
||||||
//print!("ip={ip}, {registers:?}");
|
//print!("ip={ip}, {registers:?}");
|
||||||
|
|
||||||
@@ -31,14 +34,29 @@ fn run1(input: &str, start_value: i64) -> (i64, usize) {
|
|||||||
ip = registers[ip_register] as usize;
|
ip = registers[ip_register] as usize;
|
||||||
ip += 1;
|
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
|
// 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 {
|
if ip == 28 {
|
||||||
println!("{registers:?}");
|
if seen.contains(®isters[2]) {
|
||||||
|
println!("First double: {registers:?}");
|
||||||
|
println!("Last was {last}");
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
|
seen.insert(registers[2]);
|
||||||
|
last = registers[2];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if program.get(ip).is_none() {
|
if program.get(ip).is_none() {
|
||||||
|
println!("exit by leaving instruction space");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user