-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.rs
88 lines (70 loc) · 1.82 KB
/
day5.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
76
77
78
79
80
81
82
83
84
85
86
87
88
//! [Day 5: A Maze of Twisty Trampolines, All Alike](https://adventofcode.com/2017/day/5)
struct Puzzle {
jumps: Vec<i32>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
jumps: data.lines().map(|s| s.parse().unwrap()).collect(),
}
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut jumps = self.jumps.clone();
let length = i32::try_from(jumps.len()).unwrap();
let mut offset = 0;
let mut n = 0;
while 0 <= offset && offset < length {
let index = usize::try_from(offset).unwrap();
let jump = jumps[index];
jumps[index] += 1;
offset += jump;
n += 1;
}
n
}
/// Solve part two.
fn part2(&self) -> u32 {
let mut jumps = self.jumps.clone();
let length = i32::try_from(jumps.len()).unwrap();
let mut offset = 0;
let mut n = 0;
while 0 <= offset && offset < length {
let index = usize::try_from(offset).unwrap();
let jump = jumps[index];
if jump >= 3 {
jumps[index] -= 1;
} else {
jumps[index] += 1;
}
offset += jump;
n += 1;
}
n
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u32, u32) {
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(), 5);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 10);
}
}