-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.rs
70 lines (60 loc) · 1.39 KB
/
day1.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
//! [Day 1: Report Repair](https://adventofcode.com/2020/day/1)
struct Puzzle {
expenses: Vec<u64>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
expenses: data.lines().map(|s| s.parse().unwrap()).collect(),
}
}
/// Solve part one.
fn part1(&self) -> u64 {
for i in &self.expenses {
for j in &self.expenses {
if i + j == 2020 {
return i * j;
}
}
}
0
}
/// Solve part two.
fn part2(&self) -> u64 {
for i in &self.expenses {
for j in &self.expenses {
for k in &self.expenses {
if i + j + k == 2020 {
return i * j * k;
}
}
}
}
0
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u64, u64) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test01() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 514579);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 241861950);
}
}