Solve day 13 (more pain)
This commit is contained in:
parent
5ec16bfa91
commit
ab69bdc0ad
3 changed files with 1373 additions and 0 deletions
15
input/13-0.in
Normal file
15
input/13-0.in
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279
|
||||
1279
input/13-1.in
Normal file
1279
input/13-1.in
Normal file
File diff suppressed because it is too large
Load diff
79
src/bin/13.rs
Normal file
79
src/bin/13.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#![feature(iter_array_chunks)]
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
|
||||
fn main() {
|
||||
let text = std::fs::read_to_string("input/13-1.in").unwrap();
|
||||
|
||||
let button_regex = Regex::new(r"^Button [AB]: X\+(\d+), Y\+(\d+)$").unwrap();
|
||||
|
||||
let prize_regex = Regex::new(r"^Prize: X=(\d+), Y=(\d+)$").unwrap();
|
||||
|
||||
let t = text
|
||||
.lines()
|
||||
.chain([""])
|
||||
.array_chunks::<4>()
|
||||
.map(|[a, b, c, _]| {
|
||||
let ra = button_regex.captures(a).unwrap();
|
||||
let ax = ra[1].parse::<u64>().unwrap();
|
||||
let ay = ra[2].parse::<u64>().unwrap();
|
||||
let rb = button_regex.captures(b).unwrap();
|
||||
let bx = rb[1].parse::<u64>().unwrap();
|
||||
let by = rb[2].parse::<u64>().unwrap();
|
||||
|
||||
let rp = prize_regex.captures(c).unwrap();
|
||||
let px = rp[1].parse::<u64>().unwrap();
|
||||
let py = rp[2].parse::<u64>().unwrap();
|
||||
|
||||
let amax = u64::max(px.div_ceil(ax), py.div_ceil(ay));
|
||||
let bmax = u64::max(px.div_ceil(bx), py.div_ceil(by));
|
||||
(0..amax)
|
||||
.cartesian_product(0..bmax)
|
||||
.filter(|(a, b)| (a * ax + b * bx == px) && (a * ay + b * by == py))
|
||||
.map(|(a, b)| 3 * a + b)
|
||||
.min()
|
||||
.unwrap_or(0)
|
||||
})
|
||||
.sum::<u64>();
|
||||
|
||||
println!("{t}");
|
||||
|
||||
let t = text
|
||||
.lines()
|
||||
.chain([""])
|
||||
.array_chunks::<4>()
|
||||
.filter_map(|[a, b, c, _]| {
|
||||
let ra = button_regex.captures(a).unwrap();
|
||||
let ax = ra[1].parse::<i64>().unwrap();
|
||||
let ay = ra[2].parse::<i64>().unwrap();
|
||||
let rb = button_regex.captures(b).unwrap();
|
||||
let bx = rb[1].parse::<i64>().unwrap();
|
||||
let by = rb[2].parse::<i64>().unwrap();
|
||||
|
||||
let rp = prize_regex.captures(c).unwrap();
|
||||
let offset = 10000000000000;
|
||||
let px = rp[1].parse::<i64>().unwrap() + offset;
|
||||
let py = rp[2].parse::<i64>().unwrap() + offset;
|
||||
|
||||
// dbg!(ax, ay, bx, by, px, py);
|
||||
|
||||
let upper_b = (py * ax) - (ay * px);
|
||||
let lower_b = (by * ax) - (ay * bx);
|
||||
|
||||
if upper_b % lower_b == 0 {
|
||||
let b = upper_b / lower_b;
|
||||
if (px - bx * b) % ax == 0 {
|
||||
let a = (px - bx * b) / ax;
|
||||
// dbg!(a, b);
|
||||
Some(a * 3 + b)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.sum::<i64>();
|
||||
|
||||
println!("{t}");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue