-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
58 lines (49 loc) · 1.49 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> u32 {
let (v1, v2) = get_special_numbers(data);
factorial(7) + v1 * v2
}
fn part_two(data: &str) -> u32 {
let (v1, v2) = get_special_numbers(data);
factorial(12) + v1 * v2
}
fn get_special_numbers(input: &str) -> (u32, u32) {
let mut data = input.lines();
let mut first = data.nth(19).unwrap().split_whitespace();
let mut second = data.next().unwrap().split_whitespace();
let (Some("cpy"), Some(v1), Some("c"), None) = (first.next(), first.next(), first.next(), first.next()) else {
unreachable!();
};
let (Some("jnz"), Some(v2), Some("d"), None) = (second.next(), second.next(), second.next(), second.next()) else {
unreachable!();
};
let v1 = v1
.parse()
.unwrap_or_else(|_| panic!("Could not convert v1: {v1} to a number"));
let v2 = v2
.parse()
.unwrap_or_else(|_| panic!("Could not convert v2: {v2} to a number"));
(v1, v2)
}
fn factorial(num: u32) -> u32 {
(1..=num).into_iter().product()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(0, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(0, part_two(data));
}
}