y2024d17p2 needed u64 also, kinda cheating as I didn't really understand the puzzle
This commit is contained in:
		| @@ -2,7 +2,7 @@ use std::{collections::HashMap, error::Error}; | ||||
|  | ||||
| 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 mut registers = parse_registers(registers); | ||||
|     let (_, instructions) = program.split_once(": ").unwrap(); | ||||
| @@ -19,7 +19,7 @@ pub fn process_part1(input: &str) -> (String, HashMap<&str, u32>) { | ||||
|         .collect_vec() | ||||
|         .concat(); | ||||
|  | ||||
|     let mut instruction_pointer: u32 = 0; | ||||
|     let mut instruction_pointer: u64 = 0; | ||||
|  | ||||
|     while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) { | ||||
|         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) | ||||
| } | ||||
|  | ||||
| pub fn process_part2(input: &str) -> u32 { | ||||
| pub fn process_part2(input: &str) -> u64 { | ||||
|     let (registers, program) = input.split_once("\n\n").unwrap(); | ||||
|     let mut registers = parse_registers(registers); | ||||
|     let (_, instructions) = program.split_once(": ").unwrap(); | ||||
| @@ -56,8 +56,8 @@ pub fn process_part2(input: &str) -> u32 { | ||||
|         .iter() | ||||
|         .map(|(opcode, operand)| { | ||||
|             [ | ||||
|                 opcode.parse::<u32>().unwrap(), | ||||
|                 operand.parse::<u32>().unwrap(), | ||||
|                 opcode.parse::<u64>().unwrap(), | ||||
|                 operand.parse::<u64>().unwrap(), | ||||
|             ] | ||||
|         }) | ||||
|         .collect_vec() | ||||
| @@ -70,8 +70,8 @@ pub fn process_part2(input: &str) -> u32 { | ||||
|         loop { | ||||
|             registers.insert("A", new_a); | ||||
|             let out = exec_program(instructions.clone(), &mut registers); | ||||
|             println!("target {target:?}"); | ||||
|             println!("output {out:?}"); | ||||
|             //println!("target {target:?}"); | ||||
|             //println!("output {out:?}"); | ||||
|             if out == target { | ||||
|                 a = new_a; | ||||
|                 break; | ||||
| @@ -82,9 +82,9 @@ pub fn process_part2(input: &str) -> u32 { | ||||
|     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 instruction_pointer: u32 = 0; | ||||
|     let mut instruction_pointer: u64 = 0; | ||||
|  | ||||
|     while let Some((opcode, operand)) = instructions.get(instruction_pointer as usize) { | ||||
|         let opcode = OpCodes::try_from(*opcode).unwrap(); | ||||
| @@ -102,7 +102,7 @@ fn exec_program(instructions: Vec<(&str, &str)>, registers: &mut HashMap<&str, u | ||||
|     out | ||||
| } | ||||
|  | ||||
| fn parse_registers(input: &str) -> HashMap<&str, u32> { | ||||
| fn parse_registers(input: &str) -> HashMap<&str, u64> { | ||||
|     let mut registers = HashMap::new(); | ||||
|     input.lines().for_each(|line| { | ||||
|         let (register, value) = line.split_once(": ").unwrap(); | ||||
| @@ -126,11 +126,11 @@ enum 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 b = *registers.get("B").unwrap(); | ||||
|         let c = *registers.get("C").unwrap(); | ||||
|         let operand: u32 = operand.parse().unwrap(); | ||||
|         let operand: u64 = operand.parse().unwrap(); | ||||
|         let combo = if operand <= 3 { | ||||
|             operand | ||||
|         } else if operand == 4 { | ||||
| @@ -146,9 +146,9 @@ impl OpCodes { | ||||
|             OpCodes::Adv => { | ||||
|                 //println!( | ||||
|                 //    "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 | ||||
|             } | ||||
|             OpCodes::Bxl => { | ||||
| @@ -182,17 +182,17 @@ impl OpCodes { | ||||
|             OpCodes::Bdv => { | ||||
|                 //println!( | ||||
|                 //    "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 | ||||
|             } | ||||
|             OpCodes::Cdv => { | ||||
|                 //println!( | ||||
|                 //    "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 | ||||
|             } | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user