d13
This commit is contained in:
parent
eaa9952820
commit
cc9475f4d6
3 changed files with 1359 additions and 2 deletions
15
res/13/example
Normal file
15
res/13/example
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
res/13/input
Normal file
1279
res/13/input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,16 +1,79 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
use scan_fmt::scan_fmt;
|
||||||
|
|
||||||
pub fn solve() {
|
pub fn solve() {
|
||||||
let path = "res/13/input";
|
let path = "res/13/input";
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
|
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
|
||||||
|
|
||||||
let result: usize = 0;
|
let machines: Vec<((isize, isize), (isize, isize), (isize, isize))> = contents
|
||||||
|
.split("\n\n")
|
||||||
|
.map(|block| {
|
||||||
|
let (x1, y1, x2, y2, xt, yt) = scan_fmt!(
|
||||||
|
block,
|
||||||
|
"Button A: X+{}, Y+{}\nButton B: X+{}, Y+{}\nPrize: X={}, Y={}",
|
||||||
|
isize,
|
||||||
|
isize,
|
||||||
|
isize,
|
||||||
|
isize,
|
||||||
|
isize,
|
||||||
|
isize
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
((x1, y1), (x2, y2), (xt, yt))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let result: isize = machines
|
||||||
|
.iter()
|
||||||
|
.map(|m| {
|
||||||
|
let ((ax, ay), (bx, by), (xt, yt)) = *m;
|
||||||
|
let (mut a, mut b) = (0, 0);
|
||||||
|
while b * bx < xt && b * by < yt {
|
||||||
|
b += 1;
|
||||||
|
}
|
||||||
|
while (b * bx + a * ax != xt || b * by + a * ay != yt) && b > 0 {
|
||||||
|
b -= 1;
|
||||||
|
while b * bx + a * ax < xt && b * by + a * ay < yt {
|
||||||
|
a += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if b * bx + a * ax == xt && b * by + a * ay == yt {
|
||||||
|
return b + a * 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
println!("Result 1: {}", result);
|
println!("Result 1: {}", result);
|
||||||
|
|
||||||
let result: usize = 0;
|
let result: isize = machines
|
||||||
|
.iter()
|
||||||
|
.map(|m| {
|
||||||
|
let ((ax, ay), (bx, by), (xt, yt)) = *m;
|
||||||
|
let xt = xt + 10000000000000;
|
||||||
|
let yt = yt + 10000000000000;
|
||||||
|
|
||||||
|
let b_left = by * ax - bx * ay;
|
||||||
|
let b_right = yt * ax - xt * ay;
|
||||||
|
let b = b_right / b_left;
|
||||||
|
if b_left * b != b_right || b < 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let a_left = ax;
|
||||||
|
let a_right = xt - b * bx;
|
||||||
|
let a = a_right / a_left;
|
||||||
|
if a_left * a != a_right || a < 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
a * 3 + b
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
println!("Result 2: {}", result);
|
println!("Result 2: {}", result);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue