-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday08.rs
86 lines (71 loc) · 2.58 KB
/
day08.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
type Input = (usize, Vec<i8>);
pub fn parse(input: &str) -> Input {
let bytes = input.as_bytes();
let width = bytes.iter().position(|b| b.is_ascii_whitespace()).unwrap();
let digits = bytes
.iter()
.filter(|b| b.is_ascii_digit())
.map(|&b| ((b as i8) - 48) * 6)
.collect();
(width, digits)
}
pub fn part1(input: &Input) -> usize {
let (width, digits) = input;
let mut visible: Vec<bool> = vec![false; digits.len()];
for i in 1..(*width - 1) {
let mut left_max = -1;
let mut right_max = -1;
let mut top_max = -1;
let mut bottom_max = -1;
for j in 0..(*width - 1) {
let left = (i * width) + j;
if digits[left] > left_max {
visible[left] = true;
left_max = digits[left];
}
let right = (i * width) + (width - j - 1);
if digits[right] > right_max {
visible[right] = true;
right_max = digits[right];
}
let top = (j * width) + i;
if digits[top] > top_max {
visible[top] = true;
top_max = digits[top];
}
let bottom = (width - j - 1) * width + i;
if digits[bottom] > bottom_max {
visible[bottom] = true;
bottom_max = digits[bottom];
}
}
}
4 + visible.iter().filter(|&&b| b).count()
}
pub fn part2(input: &Input) -> u64 {
let (width, digits) = input;
let ones: u64 = 0x0041041041041041;
let mask: u64 = 0xffffffffffffffc0;
let mut scenic = vec![1; digits.len()];
for i in 1..(*width - 1) {
let mut left_max = ones;
let mut right_max = ones;
let mut top_max = ones;
let mut bottom_max = ones;
for j in 1..(*width - 1) {
let left = (i * width) + j;
scenic[left] *= (left_max >> digits[left]) & 0x3f;
left_max = (left_max & (mask << digits[left])) + ones;
let right = (i * width) + (width - j - 1);
scenic[right] *= (right_max >> digits[right]) & 0x3f;
right_max = (right_max & (mask << digits[right])) + ones;
let top = (j * width) + i;
scenic[top] *= (top_max >> digits[top]) & 0x3f;
top_max = (top_max & (mask << digits[top])) + ones;
let bottom = (width - j - 1) * width + i;
scenic[bottom] *= (bottom_max >> digits[bottom]) & 0x3f;
bottom_max = (bottom_max & (mask << digits[bottom])) + ones;
}
}
*scenic.iter().max().unwrap()
}