From 5bcead2691c541a6a59962ca905cde5bd73b1804 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Tue, 17 Dec 2024 00:46:08 -0800 Subject: [PATCH] day17: part 2 solution --- .aoc_tiles/tiles/2024/17.png | Bin 6327 -> 7929 bytes README.md | 2 +- src/day17.rs | 68 +++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.aoc_tiles/tiles/2024/17.png b/.aoc_tiles/tiles/2024/17.png index 353a003c333530c095e03398a108b33d1af35f97..7606912fcc461caada16fa0489f85ca8d003da24 100644 GIT binary patch literal 7929 zcmXAu1ymH@+s8+w8wmpmQRxyvLPENmrNaeQQc!w9LR~5io&mfl6A zo25f|_xFF#d1mIGGjr~_bMO5=&*yn=gr1Hn75M{l005}e)szhI&p!NRPfCElYb~!^ z0sw29x{|z+U*=ArnHTd^+Nuo&`K3-AdwU?Ax_+XXGs3>C&J~rM;B=LSMMmI+` zZKYsjLsG^QHS)37@w%Gekju@@b7Ajg#Lzr1E-zO zA1x2*oAOP}sx}e*9a1*soBCg?J5&{Qaur&VukQzgIHptfT>Z-X{c>4J4mHX*`~P1& z9k>O2vaRn4aiQe3FdSTaD0j@dtYJxsUhSVdSsiKpm(uz-_yO8$#;2Jd2~+P%Glk)X zdKOMb)pVtowOh)x9A3ElYdDqN27h?0KiW{YU{kv!S-sGX4s=m!-$b4oue7idWSWBV-Q%py0E)E z!1jFR2|N7iD8y@$>?-+XfHXogiuoj6>NLF9zqj>63QnSgrl}nM_V381^PC2H&mfgg zPpbqQU+z%M7mCh5`%D74uVSTNA)Iue`Y@$s|H%3qvnHITNwD$e6Y4&nIq2aT#_H_( z(!E;(?ygQv2A6si?M`A43B!q#E5RxYKBrj~W??bN zA_w!r(%aax77?P zzL&qX+kpK#ZO`o<_ce2?(L=!x?YCYz z+5%&JPtSLH#}2RM>AYg-lKK;bY&J@&We(r0cm{)xmx7HFCrM*TcCBm@FDV{@g;2sR z*@q{8TMZK@r>U=%CN@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k8*V@bH*YV7JZPT8{$}T1~!pcYtA0*{)#c(+B@bCrspY3e(J_~-Y*Jo zt6IAEjmi)ApA4-SBHoLnzH>Hwn%i(gZ#Xr*Ofw7@pMpby_&1+7McI6Q)Q|pV>1Ahn zQKIZ~wrk=aEac9p1zOQrWmPs~nqwjPHa_vG-JiApu#h|!ho5g_U!-*G(7&}uF>dBR zzF-D;A?;>RFswtvaOecR+@V^lhG!LM349@*1d%JiLH37xNmOf9zuGA`SkNv_B++#x zW9Z1n&AD_(wp<>ABHjWU3IMx{@O*bvlz~C2mLN<+8yBC$GT{HMww2P28SNyVIirKT z9ViF%`b`RmP>Pzae2$y0lK1`l?eVBWER1SeS;G{U=Klbiz@$ekILdI@`(qQX+GidZ z6_p-lp`r{H{jb9z)SDK0P19()L>F9_Elm6&0npJfRIgTh@#_BoP

- 2024 - 33 ⭐ - Rust + 2024 - 34 ⭐ - Rust

diff --git a/src/day17.rs b/src/day17.rs index 8fe87a9..b95c045 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -33,8 +33,8 @@ enum Opcode { cdv = 7, } -impl From for Opcode { - fn from(value: usize) -> Self { +impl From for Opcode { + fn from(value: i64) -> Self { match value { 0 => Opcode::adv, 1 => Opcode::bxl, @@ -97,13 +97,16 @@ struct RegisterFile { file: [T; SIZE], } -impl RegisterFile { +impl, const SIZE: usize> RegisterFile { fn load(&self, reg: Register) -> &T { &self.file[reg as usize] } fn store(&mut self, reg: Register, val: T) { self.file[reg as usize] = val; } + fn reset(&mut self) { + self.file.fill(0.into()); + } } #[derive(Debug, Copy, Clone)] @@ -113,9 +116,6 @@ struct Instruction { } impl Instruction { - fn new(opcode: Opcode, operand: Operand) -> Self { - Self { opcode, operand } - } fn exec(&self, m: &mut Machine) { match self.opcode { Opcode::adv => self.adv(m), @@ -178,9 +178,10 @@ impl Instruction { } #[derive(Debug)] -struct Machine { +pub struct Machine { registers: RegisterFile<3, i64>, program: Vec, + program_raw: Vec, ip: usize, out_file: Vec, } @@ -202,6 +203,11 @@ impl Machine { fn jump(&mut self, addr: usize) { self.ip = addr; } + fn reset(&mut self) { + self.registers.reset(); + self.ip = 0; + self.out_file.clear(); + } } fn parse(input: &str) -> Machine { @@ -210,6 +216,7 @@ fn parse(input: &str) -> Machine { let mut registers: RegisterFile<3, i64> = RegisterFile { file: [0; 3] }; let mut program = Vec::new(); + let mut program_raw = Vec::new(); for line in input.lines() { if let Some(caps) = reg_re.captures(line) { let address = (caps[1].as_bytes()[0] - b'A') as usize; @@ -220,10 +227,13 @@ fn parse(input: &str) -> Machine { if let Some(caps) = prog_re.captures(line) { let instructions = caps[1].split(','); for (inst, operand) in instructions.tuples() { - let opcode: Opcode = inst.parse::().unwrap().into(); - let operand = operand.parse::().unwrap().into(); + let opcode = inst.parse::().unwrap(); + let operand = operand.parse::().unwrap(); + program_raw.push(opcode); + program_raw.push(operand); + let opcode: Opcode = opcode.into(); program.push(Instruction { - operand: opcode.interp_operand(operand), + operand: opcode.interp_operand(operand as i64), opcode, }); } @@ -233,6 +243,7 @@ fn parse(input: &str) -> Machine { Machine { registers, program, + program_raw, out_file: Vec::new(), ip: 0, } @@ -245,27 +256,56 @@ pub fn part1(input: &str) -> String { machine.out_file.iter().map(|n| n.to_string()).join(",") } +pub fn solve(m: &mut Machine, guess: i64, i: usize) -> Option { + if i as usize == m.program_raw.len() { + return Some(guess as i64); + } + let program_pos = m.program_raw.len() - 1 - i; + let goal_digit = m.program_raw[program_pos]; + + for digit in 0..8 { + let local_guess = (digit << (program_pos*3)) + guess; + m.reset(); + m.registers.store(Register::A, local_guess); + m.run(); + if m.out_file.len() == m.program_raw.len() && m.out_file[program_pos] == goal_digit { + if let Some(sol) = solve(m, local_guess, i+1) { + return Some(sol) + } + } + } + None +} + #[aoc(day17, part2)] pub fn part2(input: &str) -> i64 { - 0 + let mut machine = parse(input); + + return solve(&mut machine, 0, 0).expect("expected a solution"); } #[cfg(test)] mod tests { use super::*; - const EXAMPLE: &str = "Register A: 729 + const EXAMPLE1: &str = "Register A: 729 Register B: 0 Register C: 0 Program: 0,1,5,4,3,0"; + const EXAMPLE2: &str = "Register A: 2024 +Register B: 0 +Register C: 0 + +Program: 0,3,5,4,3,0"; + #[test] fn part1_example() { - assert_eq!(part1(EXAMPLE), "4,6,3,5,6,3,5,2,1,0"); + assert_eq!(part1(EXAMPLE1), "4,6,3,5,6,3,5,2,1,0"); } #[test] fn part2_example() { - assert_eq!(part2(EXAMPLE), 0); + assert_eq!(part2(EXAMPLE2), 117440); } }