y2024d17p2 needed u64 also, kinda cheating as I didn't really understand the puzzle

This commit is contained in:
Fabian Schmidt 2024-12-17 15:55:24 +01:00
parent 085a02b2d5
commit 00ae1c3f7d

View File

@ -2,7 +2,7 @@ use std::{collections::HashMap, error::Error};
use itertools::Itertools; use itertools::Itertools;
pub fn process_part1(input: &str) -> (String, HashMap<&str, u32>) { pub fn process_part1(input: &str) -> (String, HashMap<&str, u64>) {
let (registers, program) = input.split_once("\n\n").unwrap(); let (registers, program) = input.split_once("\n\n").unwrap();
let mut registers = parse_registers(registers); let mut registers = parse_registers(registers);
let (_, instructions) = program.split_once(": ").unwrap(); let (_, instructions) = program.split_once(": ").unwrap();
@ -19,7 +19,7 @@ pub fn process_part1(input: &str) -> (String, HashMap<&str, u32>) {
.collect_vec() .collect_vec()
.concat(); .concat();
let mut instruction_pointer: u32 = 0; let mut instruction_pointer: u64 = 0;
while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) { while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) {
let opcode = OpCodes::try_from(*opcode).unwrap(); let opcode = OpCodes::try_from(*opcode).unwrap();
@ -37,7 +37,7 @@ pub fn process_part1(input: &str) -> (String, HashMap<&str, u32>) {
(out.into_iter().join(","), registers) (out.into_iter().join(","), registers)
} }
pub fn process_part2(input: &str) -> u32 { pub fn process_part2(input: &str) -> u64 {
let (registers, program) = input.split_once("\n\n").unwrap(); let (registers, program) = input.split_once("\n\n").unwrap();
let mut registers = parse_registers(registers); let mut registers = parse_registers(registers);
let (_, instructions) = program.split_once(": ").unwrap(); let (_, instructions) = program.split_once(": ").unwrap();
@ -56,8 +56,8 @@ pub fn process_part2(input: &str) -> u32 {
.iter() .iter()
.map(|(opcode, operand)| { .map(|(opcode, operand)| {
[ [
opcode.parse::<u32>().unwrap(), opcode.parse::<u64>().unwrap(),
operand.parse::<u32>().unwrap(), operand.parse::<u64>().unwrap(),
] ]
}) })
.collect_vec() .collect_vec()
@ -70,8 +70,8 @@ pub fn process_part2(input: &str) -> u32 {
loop { loop {
registers.insert("A", new_a); registers.insert("A", new_a);
let out = exec_program(instructions.clone(), &mut registers); let out = exec_program(instructions.clone(), &mut registers);
println!("target {target:?}"); //println!("target {target:?}");
println!("output {out:?}"); //println!("output {out:?}");
if out == target { if out == target {
a = new_a; a = new_a;
break; break;
@ -82,9 +82,9 @@ pub fn process_part2(input: &str) -> u32 {
a a
} }
fn exec_program(instructions: Vec<(&str, &str)>, registers: &mut HashMap<&str, u32>) -> Vec<u32> { fn exec_program(instructions: Vec<(&str, &str)>, registers: &mut HashMap<&str, u64>) -> Vec<u64> {
let mut out = Vec::new(); let mut out = Vec::new();
let mut instruction_pointer: u32 = 0; let mut instruction_pointer: u64 = 0;
while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) { while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) {
let opcode = OpCodes::try_from(*opcode).unwrap(); let opcode = OpCodes::try_from(*opcode).unwrap();
@ -102,7 +102,7 @@ fn exec_program(instructions: Vec<(&str, &str)>, registers: &mut HashMap<&str, u
out out
} }
fn parse_registers(input: &str) -> HashMap<&str, u32> { fn parse_registers(input: &str) -> HashMap<&str, u64> {
let mut registers = HashMap::new(); let mut registers = HashMap::new();
input.lines().for_each(|line| { input.lines().for_each(|line| {
let (register, value) = line.split_once(": ").unwrap(); let (register, value) = line.split_once(": ").unwrap();
@ -126,11 +126,11 @@ enum OpCodes {
} }
impl OpCodes { impl OpCodes {
fn exec(&self, operand: &str, registers: &mut HashMap<&str, u32>) -> Option<(u32, bool)> { fn exec(&self, operand: &str, registers: &mut HashMap<&str, u64>) -> Option<(u64, bool)> {
let a = *registers.get("A").unwrap(); let a = *registers.get("A").unwrap();
let b = *registers.get("B").unwrap(); let b = *registers.get("B").unwrap();
let c = *registers.get("C").unwrap(); let c = *registers.get("C").unwrap();
let operand: u32 = operand.parse().unwrap(); let operand: u64 = operand.parse().unwrap();
let combo = if operand <= 3 { let combo = if operand <= 3 {
operand operand
} else if operand == 4 { } else if operand == 4 {
@ -146,9 +146,9 @@ impl OpCodes {
OpCodes::Adv => { OpCodes::Adv => {
//println!( //println!(
// "a = a / 2 ** combo = {a} / 2 ** {combo} = {}", // "a = a / 2 ** combo = {a} / 2 ** {combo} = {}",
// a / 2_u32.pow(combo) // a / 2_u64.pow(combo)
//); //);
registers.insert("A", a / 2_u32.pow(combo)); registers.insert("A", a / 2_u64.pow(combo as u32));
None None
} }
OpCodes::Bxl => { OpCodes::Bxl => {
@ -182,17 +182,17 @@ impl OpCodes {
OpCodes::Bdv => { OpCodes::Bdv => {
//println!( //println!(
// "b = a / 2 ** combo = {a} / 2 ** {combo} = {}", // "b = a / 2 ** combo = {a} / 2 ** {combo} = {}",
// a / 2_u32.pow(combo) // a / 2_u64.pow(combo)
//); //);
registers.insert("B", a / 2_u32.pow(combo)); registers.insert("B", a / 2_u64.pow(combo as u32));
None None
} }
OpCodes::Cdv => { OpCodes::Cdv => {
//println!( //println!(
// "c = a / 2 ** combo = {a} / 2 ** {combo} = {}", // "c = a / 2 ** combo = {a} / 2 ** {combo} = {}",
// a / 2_u32.pow(combo) // a / 2_u64.pow(combo)
//); //);
registers.insert("C", a / 2_u32.pow(combo)); registers.insert("C", a / 2_u64.pow(combo as u32));
None None
} }
} }