-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2017_05.rs
83 lines (65 loc) · 1.72 KB
/
solution_2017_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use advent_of_code_common::parsing::{Error, parse_lines_to_vec};
type Data = Vec<i32>;
fn parse(input: &str) -> Result<Data, Error> {
parse_lines_to_vec(input)
}
fn solve<F>(data: &Data, f: F) -> usize
where
F: Fn(i32) -> i32,
{
let mut program = data.clone();
let mut ip: isize = 0;
let mut steps: usize = 0;
loop {
let ip_usize = usize::try_from(ip).unwrap();
if let Some(¤t_jmp) = program.get(ip_usize) {
let new_ip = ip + current_jmp as isize;
program[ip_usize] = current_jmp + f(current_jmp);
steps += 1;
ip = new_ip;
} else {
return steps;
}
}
}
fn solve_1(data: &Data) -> usize {
solve(data, |_| 1)
}
fn solve_2(data: &Data) -> usize {
solve(data, |x| if x >= 3 { -1 } else { 1 })
}
fn part_1(input: &str) -> Result<usize, Error> {
parse(input).map(|input| solve_1(&input))
}
fn part_2(input: &str) -> Result<usize, Error> {
parse(input).map(|input| solve_2(&input))
}
const DATA: &str = include_str!("../../resources/05.txt");
fn main() -> Result<(), Error> {
let result_1 = part_1(DATA)?;
println!("Part 1: {result_1}");
let result_2 = part_2(DATA)?;
println!("Part 2: {result_2}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = include_str!("../../resources/05-test.txt");
#[test]
fn test_solve_1_test() {
assert_eq!(part_1(TEST), Ok(5));
}
#[test]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), Ok(373_160));
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST), Ok(10));
}
#[test]
fn test_solve_2_real() {
assert_eq!(part_2(DATA), Ok(26_395_586));
}
}