-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_05.rs
61 lines (54 loc) · 1.45 KB
/
day_05.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
//! https://adventofcode.com/2017/day/5
//! https://adventofcode.com/2017/day/5/input
use std::{fs::read_to_string, time::Instant};
fn parse(input: &str) -> Vec<isize> {
input.lines().map(|line| line.parse().unwrap()).collect()
}
pub mod part1 {
use super::parse;
pub fn solve(input: &str) -> usize {
let mut jumps = parse(input);
let mut steps = 0;
let mut ip = 0;
while (ip as usize) < jumps.len() {
steps += 1;
jumps[ip as usize] += 1;
ip += jumps[ip as usize] - 1;
}
steps
}
}
pub mod part2 {
use super::parse;
pub fn solve(input: &str) -> usize {
let mut jumps = parse(input);
let mut steps = 0;
let mut ip = 0;
while (ip as usize) < jumps.len() {
steps += 1;
let offset = if jumps[ip as usize] >= 3 { -1 } else { 1 };
jumps[ip as usize] += offset;
ip += jumps[ip as usize] - offset;
}
steps
}
}
pub fn main(test: bool) {
let test_input = "0
3
0
1
-3"
.to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2017/day_05_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}