-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.rs
77 lines (64 loc) · 2.51 KB
/
day20.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
use std::fs;
pub(crate) fn day20() {
println!("{}", part_a(fs::read_to_string("input/2016/day20/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2016/day20/input.txt").unwrap(), 4294967295));
}
fn part_a(input: String) -> usize {
let blacklist: Vec<(usize, usize)> = input.lines().map(|line| {
let (start, end) = line.split_once("-").unwrap();
(start.parse::<usize>().unwrap(), end.parse::<usize>().unwrap())
}).collect();
let mut min = 0;
let mut changes = 1;
while changes > 0 {
changes = 0;
for i in 0..blacklist.len() {
let (start, end) = blacklist[i];
if min >= start && min <= end {
min = end + 1;
changes += 1;
break;
}
}
}
min
}
fn part_b(input: String, range: usize) -> usize {
let mut allowlist = vec! { (0, range) };
let blacklist: Vec<(usize, usize)> = input.lines().map(|line| {
let (start, end) = line.split_once("-").unwrap();
(start.parse::<usize>().unwrap(), end.parse::<usize>().unwrap())
}).collect();
for (bl_start, bl_end) in &blacklist {
let mut new_allowlist = vec!();
allowlist.iter().for_each(|(al_start, al_end)| {
if al_start >= bl_start && al_start <= bl_end && al_end > bl_end {
new_allowlist.push((bl_end + 1, *al_end));
} else if al_start < bl_start && al_end >= bl_start && al_end <= bl_end {
new_allowlist.push((*al_start, bl_start - 1));
} else if al_start < bl_start && al_end > bl_end {
new_allowlist.push((*al_start, bl_start - 1));
new_allowlist.push((bl_end + 1, *al_end));
} else if al_end < bl_start || al_start > bl_end {
new_allowlist.push((*al_start, *al_end));
}
});
allowlist = new_allowlist
}
allowlist.iter().map(|(start, end)| *end - *start + 1).sum()
}
#[cfg(test)]
mod day20_tests {
use std::fs;
use crate::y2016::day20::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(3, part_a(fs::read_to_string("input/2016/day20/test.txt").unwrap()));
assert_eq!(2, part_b(fs::read_to_string("input/2016/day20/test.txt").unwrap(), 9));
}
#[test]
fn input_works() {
assert_eq!(19449262, part_a(fs::read_to_string("input/2016/day20/input.txt").unwrap()));
assert_eq!(119, part_b(fs::read_to_string("input/2016/day20/input.txt").unwrap(), 4294967295));
}
}