Skip to content

Commit 9d7b538

Browse files
committed
2017 day 5
1 parent 75696ad commit 9d7b538

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

crates/year2017/src/day05.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use utils::prelude::*;
2+
3+
/// Counting steps through a maze of jump instructions.
4+
#[derive(Clone, Debug)]
5+
pub struct Day05 {
6+
jumps: Vec<i32>,
7+
}
8+
9+
impl Day05 {
10+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
11+
Ok(Self {
12+
jumps: parser::i32().parse_lines(input)?,
13+
})
14+
}
15+
16+
#[must_use]
17+
pub fn part1(&self) -> u64 {
18+
let mut jumps = self.jumps.clone();
19+
let mut steps = 0;
20+
let mut pc = 0;
21+
22+
while pc < jumps.len() {
23+
let offset = jumps[pc];
24+
jumps[pc] += 1;
25+
pc = pc.wrapping_add_signed(offset as isize);
26+
steps += 1;
27+
}
28+
29+
steps
30+
}
31+
32+
#[must_use]
33+
pub fn part2(&self) -> u64 {
34+
let mut jumps = self.jumps.clone();
35+
let mut steps = 0;
36+
let mut pc = 0;
37+
38+
while pc < jumps.len() {
39+
let offset = jumps[pc];
40+
jumps[pc] += if offset >= 3 { -1 } else { 1 };
41+
pc = pc.wrapping_add_signed(offset as isize);
42+
steps += 1;
43+
}
44+
45+
steps
46+
}
47+
}
48+
49+
examples!(Day05 -> (u64, u64) [
50+
{input: "0\n3\n0\n1\n-3", part1: 5, part2: 10},
51+
]);

crates/year2017/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ utils::year!(2017 => year2017, ${
66
2 => day02::Day02,
77
3 => day03::Day03,
88
4 => day04::Day04<'_>,
9+
5 => day05::Day05,
910
});

0 commit comments

Comments
 (0)