-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.rs
More file actions
128 lines (115 loc) · 3.44 KB
/
07.rs
File metadata and controls
128 lines (115 loc) · 3.44 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use bit_vec::BitVec;
use colored::Colorize;
advent_of_code::solution!(7);
pub fn part_one(input: &str) -> Option<u64> {
let debug = false;
let lines = input
.lines()
.filter(|line| !line.is_empty())
.collect::<Vec<&str>>();
let width = lines[0].len();
let mut bv = BitVec::from_elem(width, false);
let mut splits = 0;
for line in lines {
let bv_read_only = bv.clone();
for (i, ch) in line.chars().enumerate() {
if ch == 'S' {
bv.set(i, true);
if debug {
print!("{}", "S".green().bold());
}
} else if bv_read_only.get(i).unwrap() && ch == '^' {
bv.set(i, false);
splits += 1;
if i > 0 {
bv.set(i - 1, true);
}
if i < width {
bv.set(i + 1, true);
}
if debug {
print!("{}", "^".white().bold());
}
} else if bv_read_only.get(i).unwrap() {
if debug {
print!("{}", "|".white().bold());
}
} else if debug {
print!("{}", ".".dimmed());
}
}
if debug {
println!();
}
}
Some(splits)
}
pub fn part_two(input: &str) -> Option<u64> {
let lines = input
.lines()
.filter(|line| !line.is_empty())
.collect::<Vec<&str>>();
let debug = false;
let width = lines[0].len();
let mut paths: Vec<u64> = vec![0; width];
let mut splits = 0;
for line in lines {
for (i, ch) in line.chars().enumerate() {
let timelines_on_this_path = paths[i];
if ch == 'S' {
paths[i] = 1;
if debug {
print!("{}", "S".green().bold());
}
} else if ch == '^' {
paths[i] = 0;
splits += 1;
if i > 0 {
paths[i - 1] += timelines_on_this_path
}
if i < width {
paths[i + 1] += timelines_on_this_path;
}
if debug {
print!("{}", "^".white().bold());
}
} else if paths[i] > 0 {
if debug {
print!("{}", "|".white().bold());
}
} else if debug {
print!("{}", ".".dimmed());
}
}
if debug {
println!(
"Splits: {splits}. Paths: {}",
paths.clone().iter().sum::<u64>()
);
}
}
/*
l3: 1 in, 1 ^ 2 choices 2 total = n * 2
l5: 2 in 2 ^ 2 splits 3 choices (121) 4 total = cumulative n*2
L7: 3 in, 3^ ,2,2 - 3 splits, 4choices (1331). 8 total != total paths to a. ^ * 2
L9: 3 splits, 6. total - 2 const
3
4
6
*/
Some(paths.iter().sum::<u64>())
}
#[cfg(test)]
mod day7_tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(21));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(40));
}
}