From 0c3ebf803f93447dcefbc4dedf6609b31317c5a1 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 18 Dec 2024 17:57:49 +0100 Subject: [PATCH] day 17 part 2 --- src/day17.rs | 98 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/src/day17.rs b/src/day17.rs index 0a6aa30..36093f7 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -1,4 +1,4 @@ -use std::{fmt::Write, fs::read_to_string, io::BufRead, ops::BitXor}; +use std::{fs::read_to_string, ops::BitXor}; use itertools::Itertools; @@ -12,30 +12,21 @@ pub fn day_main() { type RiddleResult = String; fn part1(input: &str) -> RiddleResult { - let (first, second) = input.split_once("\n\n").unwrap(); - dbg!(first); - dbg!(second); - let mut registers = first.lines().map(|line| line.split_once(": ").unwrap().1); + let (a, b, c, program) = parse_input(input); - let mut a = registers.next().unwrap().parse::().unwrap(); - let mut b = registers.next().unwrap().parse::().unwrap(); - let mut c = registers.next().unwrap().parse::().unwrap(); - - let program = second - .split_once(": ") - .unwrap() - .1 - .split(",") - .map(|v| v.parse::().unwrap()) - .collect_vec(); + run(&program, a, b, c) + .into_iter() + .map(|it| it.to_string()) + .join(",") +} +fn run(program: &Vec, mut a: i64, mut b: i64, mut c: i64) -> Vec { let mut ip = 0; - let mut output = String::new(); + let mut output = vec![]; while ip < program.len() { let opcode = program[ip]; let operand = program[(ip + 1) % program.len()]; - // println!("a: {a}, b: {b}, c: {c}, op={opcode} {operand} (ip: {ip})"); match opcode { 0 => a = a / (2i64.pow(val(a, b, c, operand) as u32)), 1 => b = b.bitxor(operand), @@ -48,26 +39,32 @@ fn part1(input: &str) -> RiddleResult { } } 4 => b = b.bitxor(c), - 5 => output - .write_fmt(format_args!("{},", val(a, b, c, operand) % 8)) - .unwrap(), + 5 => output.push(val(a, b, c, operand) % 8), 6 => b = a / (2i64.pow(val(a, b, c, operand) as u32)), 7 => c = a / (2i64.pow(val(a, b, c, operand) as u32)), _ => panic!("illegal op code {}", opcode), } ip += 2; - // ip %= program.len(); - // wait(); - } - if output.ends_with(",") { - output.remove(output.len() - 1); } output } -fn wait() { - let stdin = std::io::stdin(); - let _line1 = stdin.lock().lines().next().unwrap().unwrap(); +fn parse_input(input: &str) -> (i64, i64, i64, Vec) { + let (first, second) = input.split_once("\n\n").unwrap(); + let mut registers = first.lines().map(|line| line.split_once(": ").unwrap().1); + + let a = registers.next().unwrap().parse::().unwrap(); + let b = registers.next().unwrap().parse::().unwrap(); + let c = registers.next().unwrap().parse::().unwrap(); + + let program = second + .split_once(": ") + .unwrap() + .1 + .split(",") + .map(|v| v.parse::().unwrap()) + .collect_vec(); + (a, b, c, program) } fn val(a: i64, b: i64, c: i64, operand: i64) -> i64 { @@ -80,8 +77,34 @@ fn val(a: i64, b: i64, c: i64, operand: i64) -> i64 { } } -fn part2(_input: &str) -> RiddleResult { - String::new() +fn part2(input: &str) -> RiddleResult { + let (_, _, _, program) = parse_input(input); + solve(&program, program.len() - 1, 0).unwrap().to_string() +} + +fn solve(program: &Vec, index: usize, a: i64) -> Option { + // if index == 0 { + // return if &run(program, a, 0, 0) == program { + // Some(a) + // } else { + // None + // }; + // } + for i in 0..8 { + let next_a = a * 8 + i; + let o = run(program, next_a, 0, 0); + if o[0] == program[index] { + if index == 0 { + return Some(next_a); + } else { + let m = solve(program, index - 1, next_a); + if m.is_some() { + return m; + } + } + } + } + None } #[cfg(test)] @@ -103,6 +126,17 @@ Program: 0,1,5,4,3,0 #[test] fn test2() { - assert_eq!(part2(TEST_INPUT.trim()), "".to_string()); + assert_eq!( + part2( + "Register A: 2024 +Register B: 0 +Register C: 0 + +Program: 0,3,5,4,3,0 +" + .trim() + ), + "117440".to_string() + ); } }