-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.rs
65 lines (54 loc) · 1.4 KB
/
lib.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
extern crate core;
pub fn fn1(input: &str) -> i32 {
let mut lines: Vec<_> = input.lines().map(|s| s.parse::<i32>().unwrap()).collect();
let mut i: i32 = 0;
let mut steps = 0;
while i < lines.len() as i32 {
let offset = lines[i as usize];
steps += 1;
lines[i as usize] += 1;
i += offset;
}
steps
}
pub fn fn2(input: &str) -> i32 {
let mut lines: Vec<_> = input.lines().map(|s| s.parse::<i32>().unwrap()).collect();
let mut i: i32 = 0;
let mut steps = 0;
while i < lines.len() as i32 {
let offset = lines[i as usize];
steps += 1;
if offset >= 3 {
lines[i as usize] -= 1;
} else {
lines[i as usize] += 1;
}
i += offset;
}
steps
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_fn1_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(fn1(s.as_str()), 5);
}
#[test]
fn test_fn1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn1(s.as_str()), 372671);
}
#[test]
fn test_fn2_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(fn2(s.as_str()), 10);
}
#[test]
fn test_fn2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn2(s.as_str()), 25608480);
}
}