e84087dec40d1f7281f2d59c90301b697b2d0ca2
3 use nalgebra
::{stack
, Vector2
};
6 type Equations
= Vec
<(Vector2
<i64>, Vector2
<i64>, Vector2
<i64>)>;
8 pub fn read(reader
: &mut dyn BufRead
) -> Equations
{
9 let button_a
= Regex
::new(r
#"Button A: X\+(\d+), Y\+(\d+)"#).unwrap();
10 let button_b
= Regex
::new(r
#"Button B: X\+(\d+), Y\+(\d+)"#).unwrap();
11 let prize
= Regex
::new(r
#"Prize: X=(\d+), Y=(\d+)"#).unwrap();
13 let mut equations
= Vec
::new();
14 let mut iter
= reader
.lines();
16 if let Some(Ok(line1
)) = iter
.next() {
17 let a_cap
= button_a
.captures(&line1
).unwrap();
18 let a_factors
= Vector2
::new(
19 a_cap
[1].parse
::<i64>().unwrap(),
20 a_cap
[2].parse
::<i64>().unwrap(),
22 let line2
= iter
.next().unwrap().unwrap();
23 let b_cap
= button_b
.captures(&line2
).unwrap();
24 let b_factors
= Vector2
::new(
25 b_cap
[1].parse
::<i64>().unwrap(),
26 b_cap
[2].parse
::<i64>().unwrap(),
28 let line3
= iter
.next().unwrap().unwrap();
29 let prize_cap
= prize
.captures(&line3
).unwrap();
30 let prize_xy
= Vector2
::new(
31 prize_cap
[1].parse
::<i64>().unwrap(),
32 prize_cap
[2].parse
::<i64>().unwrap(),
35 equations
.push((a_factors
, b_factors
, prize_xy
));
43 pub fn add_10000000000000_to_xy(equations
: Equations
) -> Equations
{
46 .map(|(a_v
, b_v
, xy
)| (a_v
, b_v
, xy
.add_scalar(10000000000000)))
50 pub fn nb_tokens(equations
: &Equations
) -> i64 {
53 .filter_map(|(a_v
, b_v
, xy
)| {
54 let d
= stack
![a_v
.cast
::<f64>(), b_v
.cast
::<f64>()].determinant();
58 let a
= (stack
![xy
.cast
::<f64>(), b_v
.cast
::<f64>()].determinant() / d
) as i64;
59 let b
= (stack
![a_v
.cast
::<f64>(), xy
.cast
::<f64>()].determinant() / d
) as i64;
61 if a
* a_v
[0] + b
* b_v
[0] == xy
[0] && a
* a_v
[1] + b
* b_v
[1] == xy
[1] {
74 static BUTTON_BEHAVIORS
: &str = "Button A: X+94, Y+34
80 Prize: X=12748, Y=12176
88 Prize: X=18641, Y=10279";
92 let equations
= read(&mut BUTTON_BEHAVIORS
.as_bytes());
93 assert_eq!(nb_tokens(&equations
), 480);