-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
70 lines (64 loc) · 2.32 KB
/
03.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
fn main() {
let input = include_str!("../../input/03.txt");
println!("Part 1: {}", run(input, false));
println!("Part 2: {}", run(input, true));
}
fn find_path(input: &str, right: usize, down: usize) -> i64 {
let tree = '#';
let (num_tree, _right_count) =
input
.lines()
.step_by(down)
.fold((0, 0), |(num_tree, right_count), line| {
let mut chars = line.chars();
let len = chars.clone().count();
(
num_tree + i64::from(chars.nth(right_count).unwrap() == tree),
(right_count + right) % len,
)
});
num_tree
}
fn run(input: &str, part_two: bool) -> i64 {
if part_two {
[(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
.iter()
.map(|&(right, down)| find_path(input, right, down))
.product::<i64>()
} else {
find_path(input, 3, 1)
}
}
#[cfg(test)]
mod day_3_tests {
use super::*;
static INPUT: &str = "..##.........##.........##.........##.........##.........##.......
#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....
.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
.#........#.#........#.#........#.#........#.#........#.#........#
#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
#...##....##...##....##...##....##...##....##...##....##...##....#
.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#";
#[test]
fn test_part_1() {
assert!(run(INPUT, false) == 7);
assert!(run(include_str!("../../input/03.txt"), false) == 250);
}
#[test]
fn test_part_2() {
assert!(run(INPUT, true) == 336);
assert!(run(include_str!("../../input/03.txt"), true) == 1_592_662_500);
}
#[test]
fn test_run() {
assert!(find_path(INPUT, 1, 1) == 2);
assert!(find_path(INPUT, 3, 1) == 7);
assert!(find_path(INPUT, 5, 1) == 3);
assert!(find_path(INPUT, 7, 1) == 4);
assert!(find_path(INPUT, 1, 2) == 2);
}
}