Add day17 part2

This commit is contained in:
Adrian Groh 2024-12-17 21:15:23 +01:00
parent b569484664
commit f6fa43766a
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771

View File

@ -1,4 +1,4 @@
#[derive(Debug)] #[derive(Debug, Clone)]
struct State { struct State {
reg_a: usize, reg_a: usize,
reg_b: usize, reg_b: usize,
@ -67,19 +67,61 @@ fn parse(input: &str) -> State {
} }
} }
fn part1(state: &mut State) -> String { fn get_output(state: &mut State) -> Vec<u8> {
while state.pc < state.prog.len() - 1 { while state.pc < state.prog.len() - 1 {
state.exec_instruction(); state.exec_instruction();
} }
state state.out.clone()
.out }
fn part1(state: &mut State) -> String {
get_output(state)
.iter() .iter()
.map(|n| n.to_string()) .map(|n| n.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(",") .join(",")
} }
fn main() { // B <- A % 8
let mut input = parse(include_str!("../input.txt")); // B <- B ^ 3
println!("{}", part1(&mut input)); // C <- A >> B
// B <- B ^ C
// A <- A >> 3
// B <- B ^ 5
// print B & 7
// jmp 0 if A
fn find_input(state: &State, instructions: &[u8], curr_a: usize) -> Vec<usize> {
let mut candidates = vec![];
if instructions.is_empty() {
return vec![curr_a];
}
let curr = instructions.last().unwrap();
for i in 0..8 {
let mut new_state = state.clone();
let new_a = (curr_a << 3) + i;
new_state.reg_a = new_a;
if get_output(&mut new_state)[0] == *curr {
candidates.push(new_a);
}
}
let mut results = vec![];
for candidate in candidates {
results.extend(find_input(
state,
&instructions[0..instructions.len() - 1],
candidate,
));
}
results
}
fn part2(state: &State) -> usize {
*find_input(state, &state.prog, 0).iter().min().unwrap()
}
fn main() {
let input = parse(include_str!("../input.txt"));
println!("{}", part1(&mut input.clone()));
println!("{}", part2(&input));
} }