-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.rs
More file actions
67 lines (60 loc) · 1.75 KB
/
13.rs
File metadata and controls
67 lines (60 loc) · 1.75 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
advent_of_code::solution!(13);
use itertools::Itertools;
fn solve(x1: i64, x2: i64, y1: i64, y2: i64, z1: i64, z2: i64) -> i64 {
let b = (z2 * x1 - z1 * x2) / (y2 * x1 - y1 * x2);
let a = (z1 - b * y1) / x1;
if (x1 * a + y1 * b, x2 * a + y2 * b) != (z1, z2) {
return 0;
}
a * 3 + b
}
pub fn part_one(input: &str) -> Option<usize> {
let mut result = 0;
// \n\r -- Win OS
// \n\n -- Unix OS
for line in input.split("\n\n") {
let (x1, x2, y1, y2, z1, z2) = match line
.split(|c: char| !c.is_ascii_digit())
.filter(|w| !w.is_empty())
.map(|w| w.parse().unwrap())
.collect_tuple()
{
Some(x) => x,
None => continue,
};
result += solve(x1, x2, y1, y2, z1, z2);
}
Some(result as usize)
}
pub fn part_two(input: &str) -> Option<usize> {
let mut result = 0;
// \n\r -- Win OS
// \n\n -- Unix OS
for line in input.split("\n\n") {
let (x1, x2, y1, y2, z1, z2) = match line
.split(|c: char| !c.is_ascii_digit())
.filter(|w| !w.is_empty())
.map(|w| w.parse().unwrap())
.collect_tuple()
{
Some(x) => x,
None => continue,
};
result += solve(x1, x2, y1, y2, z1 + 10000000000000, z2 + 10000000000000);
}
Some(result as usize)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(480));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(875318608908));
}
}