Partial solve day 17
This commit is contained in:
parent
407b054779
commit
f3c1072d08
6 changed files with 183 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -15,6 +15,7 @@ dependencies = [
|
||||||
"image",
|
"image",
|
||||||
"itertools 0.13.0",
|
"itertools 0.13.0",
|
||||||
"memoize",
|
"memoize",
|
||||||
|
"rayon",
|
||||||
"regex",
|
"regex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,5 @@ edition = "2021"
|
||||||
image = "0.25.5"
|
image = "0.25.5"
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
memoize = "0.4.2"
|
memoize = "0.4.2"
|
||||||
|
rayon = "1.10.0"
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
|
|
|
||||||
5
input/17-0a.in
Normal file
5
input/17-0a.in
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Register A: 729
|
||||||
|
Register B: 0
|
||||||
|
Register C: 0
|
||||||
|
|
||||||
|
Program: 0,1,5,4,3,0
|
||||||
5
input/17-0b.in
Normal file
5
input/17-0b.in
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Register A: 2024
|
||||||
|
Register B: 0
|
||||||
|
Register C: 0
|
||||||
|
|
||||||
|
Program: 0,3,5,4,3,0
|
||||||
5
input/17-1.in
Normal file
5
input/17-1.in
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Register A: 27575648
|
||||||
|
Register B: 0
|
||||||
|
Register C: 0
|
||||||
|
|
||||||
|
Program: 2,4,1,2,7,5,4,1,1,3,5,5,0,3,3,0
|
||||||
166
src/bin/17.rs
Normal file
166
src/bin/17.rs
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
use rayon::prelude::*;
|
||||||
|
|
||||||
|
fn combo_operand(operand: u8, a: u64, b: u64, c: u64) -> u64 {
|
||||||
|
match operand {
|
||||||
|
0..4 => operand as u64,
|
||||||
|
4 => a,
|
||||||
|
5 => b,
|
||||||
|
6 => c,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_program(program: &[u8], mut a: u64, mut b: u64, mut c: u64) -> Vec<u8> {
|
||||||
|
let mut i = 0;
|
||||||
|
|
||||||
|
let mut output = Vec::new();
|
||||||
|
|
||||||
|
while let Some((&opcode, &operand)) = program.get(i).zip(program.get(i + 1)) {
|
||||||
|
// dbg!(i, a, b, c, opcode, operand);
|
||||||
|
match opcode {
|
||||||
|
0 => {
|
||||||
|
a = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
b ^= operand as u64;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
b = combo_operand(operand, a, b, c) % 8;
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
3 => {
|
||||||
|
if a != 0 {
|
||||||
|
i = operand as usize
|
||||||
|
} else {
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4 => {
|
||||||
|
b ^= c;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
5 => {
|
||||||
|
output.push((combo_operand(operand, a, b, c) % 8) as u8);
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
6 => {
|
||||||
|
b = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
7 => {
|
||||||
|
c = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_program_output(program: &[u8], mut a: u64, mut b: u64, mut c: u64) -> bool {
|
||||||
|
let mut i = 0;
|
||||||
|
|
||||||
|
let mut o = 0;
|
||||||
|
|
||||||
|
while let Some((&opcode, &operand)) = program.get(i).zip(program.get(i + 1)) {
|
||||||
|
// dbg!(i, a, b, c, opcode, operand);
|
||||||
|
match opcode {
|
||||||
|
0 => {
|
||||||
|
a = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
b ^= operand as u64;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
b = combo_operand(operand, a, b, c) % 8;
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
3 => {
|
||||||
|
if a != 0 {
|
||||||
|
i = operand as usize
|
||||||
|
} else {
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4 => {
|
||||||
|
b ^= c;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
5 => {
|
||||||
|
if program.len() >= o && program[o] != (combo_operand(operand, a, b, c) % 8) as u8 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
o += 1;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
6 => {
|
||||||
|
b = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
7 => {
|
||||||
|
c = a / (2u64.pow(combo_operand(operand, a, b, c) as u32));
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
o == program.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let text = std::fs::read_to_string("input/17-1.in").unwrap();
|
||||||
|
let mut lines = text.lines();
|
||||||
|
let start_a = lines
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.split_once(": ")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap();
|
||||||
|
let start_b = lines
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.split_once(": ")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap();
|
||||||
|
let start_c = lines
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.split_once(": ")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let program = lines
|
||||||
|
.nth(1)
|
||||||
|
.unwrap()
|
||||||
|
.split_once(": ")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.split(',')
|
||||||
|
.map(|c| c.parse::<u8>().unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
println!("{:?}", run_program(&program, start_a, start_b, start_c));
|
||||||
|
|
||||||
|
let start = 1_000_000_000_000u64;
|
||||||
|
|
||||||
|
let t = (start..start * 10)
|
||||||
|
.into_par_iter()
|
||||||
|
.find_first(|&i| run_program_output(&program, i, 0, 0))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("{t}");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue