generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.rs
91 lines (76 loc) · 2.84 KB
/
13.rs
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
use itertools::Itertools;
use regex::Regex;
advent_of_code::solution!(13);
const PREFIX: i64 = 10000000000000;
fn parse(input: &str) -> Vec<((i64, i64, i64), (i64, i64, i64))> {
let pattern = Regex::new(r"\n\s*\n").unwrap();
pattern.split(input).map(parse_equation).collect()
}
fn parse_with_prefix(input: &str) -> Vec<((i64, i64, i64), (i64, i64, i64))> {
let pattern = Regex::new(r"\n\s*\n").unwrap();
pattern.split(input).map(parse_equation_with_prefix).collect()
}
fn parse_equation(input: &str) -> ((i64, i64, i64), (i64, i64, i64)) {
let equation: Vec<(i64, i64)> = input.lines()
.map(|line| {
let first = &line[(line.find('+').or(line.find('=')).unwrap() + 1)..line.find(',').unwrap()];
let second = &line[(line.rfind('+').or(line.rfind('=')).unwrap() + 1)..].trim_end();
let first: i64 = first.parse().unwrap();
let second: i64 = second.parse().unwrap();
(first, second)
})
.collect_vec();
((equation[0].0, equation[1].0, equation[2].0), (equation[0].1, equation[1].1, equation[2].1))
}
fn parse_equation_with_prefix(input: &str) -> ((i64, i64, i64), (i64, i64, i64)) {
let equation: Vec<(i64, i64)> = input.lines()
.map(|line| {
let first = &line[(line.find('+').or(line.find('=')).unwrap() + 1)..line.find(',').unwrap()];
let second = &line[(line.rfind('+').or(line.rfind('=')).unwrap() + 1)..].trim_end();
let first: i64 = first.parse().unwrap();
let second: i64 = second.parse().unwrap();
(first, second)
})
.collect_vec();
((equation[0].0, equation[1].0, PREFIX + equation[2].0), (equation[0].1, equation[1].1, PREFIX + equation[2].1))
}
fn solve(eq1: (i64, i64, i64), eq2: (i64, i64, i64)) -> Option<i64> {
let (a1, b1, c1) = eq1;
let (a2, b2, c2) = eq2;
#[allow(non_snake_case)]
let det_A = a1 * b2 - b1 * a2;
if det_A == 0 {
return None;
}
let x = (c1 * b2 - b1 * c2) / det_A;
let y = (a1 * c2 - c1 * a2) / det_A;
if a1 * x + b1 * y == c1 && a2 * x + b2 * y == c2 {
Some(x * 3 + y)
} else {
None
}
}
pub fn part_one(input: &str) -> Option<i64> {
Some(parse(input).iter()
.filter_map(|(eq1, eq2)| solve(*eq1, *eq2))
.sum::<i64>())
}
pub fn part_two(input: &str) -> Option<i64> {
Some(parse_with_prefix(input).iter()
.filter_map(|(eq1, eq2)| solve(*eq1, *eq2))
.sum::<i64>())
}
#[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));
}
}