-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.rs
More file actions
158 lines (137 loc) · 3.84 KB
/
day13.rs
File metadata and controls
158 lines (137 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use regex::Regex;
const OFFSET: usize = 10000000000000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
x: usize,
y: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Claw {
button_a: Point,
button_b: Point,
win_position: Point,
cost_a: usize,
cost_b: usize,
}
impl Claw {
pub fn det(&self) -> isize {
self.button_a.x as isize * self.button_b.y as isize
- self.button_a.y as isize * self.button_b.x as isize
}
pub fn solve_equation(&self) -> Option<(usize, usize)> {
let det = self.det();
if det == 0 {
return None;
}
let det_x = self.win_position.x as isize * self.button_b.y as isize
- self.win_position.y as isize * self.button_b.x as isize;
let det_y = self.button_a.x as isize * self.win_position.y as isize
- self.button_a.y as isize * self.win_position.x as isize;
if det_x % det != 0 || det_y % det != 0 {
return None;
}
let (a, b) = (det_x / det, det_y / det);
if a < 0 || b < 0 {
return None;
}
Some((a as usize, b as usize))
}
pub fn cost(&self) -> Option<usize> {
let (x, y) = self.solve_equation()?;
Some(x * self.cost_a + y * self.cost_b)
}
}
fn parse_line(line: &str) -> Point {
let re = Regex::new(r"(Button .|Prize): X(\+|=)(?<x>\d+), Y(\+|=)(?<y>\d+)").unwrap();
let caps = re.captures(line).unwrap();
Point {
x: caps.name("x").unwrap().as_str().parse::<usize>().unwrap(),
y: caps.name("y").unwrap().as_str().parse::<usize>().unwrap(),
}
}
fn parse_input(input: &str, task02: bool) -> Vec<Claw> {
let mut point_a = Point { x: 0, y: 0 };
let mut point_b = Point { x: 0, y: 0 };
let mut win_position = Point { x: 0, y: 0 };
let mut claws = Vec::new();
let cost_a: usize = 3;
let cost_b: usize = 1;
for (i, line) in input.lines().enumerate() {
match i % 4 {
0 => {
point_a = parse_line(line);
}
1 => {
point_b = parse_line(line);
}
2 => {
win_position = parse_line(line);
if task02 {
win_position = Point {
x: win_position.x + OFFSET,
y: win_position.y + OFFSET,
};
}
}
3 => {
claws.push(Claw {
button_a: point_a,
button_b: point_b,
win_position,
cost_a,
cost_b,
});
}
_ => unreachable!(),
}
}
claws.push(Claw {
button_a: point_a,
button_b: point_b,
win_position,
cost_a,
cost_b,
});
claws
}
pub fn task01(input: &str) -> String {
let claws = parse_input(input, false);
claws
.iter()
.filter_map(|claw| claw.cost())
.sum::<usize>()
.to_string()
}
pub fn task02(input: &str) -> String {
let claws = parse_input(input, true);
claws
.iter()
.filter_map(|claw| claw.cost())
.sum::<usize>()
.to_string()
}
#[cfg(test)]
mod tests {
use super::super::fs_utils::{read_example, read_input};
use super::*;
#[test]
fn test_task01() {
let input = read_example(13, 1);
assert_eq!(task01(&input), "480");
}
#[test]
fn run_task01() {
let input = read_input(13);
assert_eq!(task01(&input), "35255");
}
#[test]
fn test_task02() {
let input = read_example(13, 1);
assert_eq!(task02(&input), "416082282239");
}
#[test]
fn run_task02() {
let input = read_input(13);
assert_eq!(task02(&input), "87582154060429");
}
}