day 13 part 2
This commit is contained in:
73
src/day13.rs
73
src/day13.rs
@@ -1,4 +1,4 @@
|
|||||||
use std::{borrow::Borrow, fs::read_to_string};
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@@ -10,7 +10,7 @@ pub fn day_main() {
|
|||||||
println!(" part2: {}", part2(input));
|
println!(" part2: {}", part2(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
type RiddleResult = u64;
|
type RiddleResult = i64;
|
||||||
|
|
||||||
fn part1(input: &str) -> RiddleResult {
|
fn part1(input: &str) -> RiddleResult {
|
||||||
// Button A: X+30, Y+84
|
// Button A: X+30, Y+84
|
||||||
@@ -27,46 +27,73 @@ Prize: X=(\d+), Y=(\d+)",
|
|||||||
let (ax, ay, bx, by, px, py) = block
|
let (ax, ay, bx, by, px, py) = block
|
||||||
.iter()
|
.iter()
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|it| it.unwrap().as_str().parse::<u64>().unwrap())
|
.map(|it| it.unwrap().as_str().parse::<i64>().unwrap())
|
||||||
.collect_tuple()
|
.collect_tuple()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let mut solution = None;
|
solve((ax, ay, bx, by, px, py)).unwrap_or(0)
|
||||||
for a in 0..=100 {
|
})
|
||||||
for b in 0..=100 {
|
.sum()
|
||||||
if a * ax + b * bx == px && a * ay + b * by == py {
|
}
|
||||||
let s = 3 * a + b;
|
|
||||||
if let Some(old) = solution {
|
fn part2(input: &str) -> RiddleResult {
|
||||||
if old > s {
|
let r = Regex::new(
|
||||||
solution = Some(s);
|
r"Button A: X\+(\d+), Y\+(\d+)
|
||||||
}
|
Button B: X\+(\d+), Y\+(\d+)
|
||||||
} else {
|
Prize: X=(\d+), Y=(\d+)",
|
||||||
solution = Some(s);
|
)
|
||||||
}
|
.unwrap();
|
||||||
}
|
r.captures_iter(input)
|
||||||
}
|
.map(|block| {
|
||||||
}
|
let (ax, ay, bx, by, px, py) = block
|
||||||
|
.iter()
|
||||||
|
.skip(1)
|
||||||
|
.map(|it| it.unwrap().as_str().parse::<i64>().unwrap())
|
||||||
|
.collect_tuple()
|
||||||
|
.unwrap();
|
||||||
|
let solution = solve((ax, ay, bx, by, px + 10000000000000, py + 10000000000000));
|
||||||
solution.unwrap_or(0)
|
solution.unwrap_or(0)
|
||||||
})
|
})
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(_input: &str) -> RiddleResult {
|
fn solve((ax, ay, bx, by, px, py): (i64, i64, i64, i64, i64, i64)) -> Option<RiddleResult> {
|
||||||
0
|
let b = (ay * px - ax * py) / (ay * bx - ax * by);
|
||||||
|
let a = (px - b * bx) / ax;
|
||||||
|
if a >= 0 && b >= 0 && a * ax + b * bx == px && a * ay + b * by == py {
|
||||||
|
Some(3 * a + b)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::{part1, part2};
|
use super::{part1, part2};
|
||||||
|
|
||||||
const TEST_INPUT: &str = r"";
|
const TEST_INPUT: &str = r"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
|
||||||
|
";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test1() {
|
fn test1() {
|
||||||
assert_eq!(part1(TEST_INPUT), 0);
|
assert_eq!(part1(TEST_INPUT), 480);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test2() {
|
fn test2() {
|
||||||
assert_eq!(part2(TEST_INPUT), 0);
|
assert_eq!(part2(TEST_INPUT), 875318608908);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user