-
Notifications
You must be signed in to change notification settings - Fork 0
/
y18d01.rs
75 lines (63 loc) · 1.92 KB
/
y18d01.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
use std::collections::BTreeSet;
use aoc::AoCInput;
fn solve_task(input: &str) -> (i32, i32) {
let calibrations: Vec<i32> = input
.trim()
.lines()
.map(|f| {
let (sign, num) = f.split_at(1);
let num = num.parse::<u32>().unwrap() as i32;
match sign {
"+" => num,
"-" => -num,
_ => panic!("Bad input"),
}
})
.collect();
let task1 = calibrations.iter().sum();
let mut task2 = 0;
let mut visited: BTreeSet<i32> = BTreeSet::new();
visited.insert(0);
for cal in calibrations.iter().cycle() {
task2 += cal;
// Bail if there's no end in sight
if !visited.insert(task2) || task2.abs() > 100000 {
break;
}
}
(task1, task2)
}
fn main() {
let input = AoCInput::from_env()
.expect("SESSION env variable not found")
.get_input(2018, 1)
.expect("Could not fetch input");
let (task1, task2) = solve_task(&input);
println!("Task 1: {}", task1);
println!("Task 2: {}", task2);
}
#[cfg(test)]
mod y2021d01 {
use super::*;
#[test]
fn examples() {
// Task 1
let (example1, _) = solve_task("+1\n-2\n+3\n+1");
assert_eq!(example1, 3);
let (example1, _) = solve_task("+1\n+1\n+1");
assert_eq!(example1, 3);
let (example1, _) = solve_task("+1\n+1\n-2");
assert_eq!(example1, 0);
let (example1, _) = solve_task("-1\n-2\n-3");
assert_eq!(example1, -6);
// Task 2
let (_, example2) = solve_task("+1\n-1");
assert_eq!(example2, 0);
let (_, example2) = solve_task("+3\n+3\n+4\n-2\n-4");
assert_eq!(example2, 10);
let (_, example2) = solve_task("-6\n+3\n+8\n+5\n-6");
assert_eq!(example2, 5);
let (_, example2) = solve_task("+7\n+7\n-2\n-7\n-4");
assert_eq!(example2, 14);
}
}