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
|
use std::fs;
fn solve_part1(ranges: &Vec<(u32, u32)>) -> u32 {
'outer: for i in 0..u32::MAX {
for &(lower, upper) in ranges {
if i >= lower && i <= upper {
// This IP is blacklisted
continue 'outer;
}
}
return i;
}
u32::MAX // shouldn't be reached
}
fn solve_part2(mut ranges: Vec<(u32, u32)>) -> u32 {
// Sort by upper limit, so we know if a range is still needed
ranges.sort_by_key(|x| x.1);
let mut count = 0;
'outer: for i in 0..u32::MAX {
// Reduce the size of `ranges' to keep loop time reasonable
if i > ranges[0].1 {
ranges.remove(0);
}
for &(lower, upper) in &ranges {
if i >= lower && i <= upper {
// This IP is blacklisted
continue 'outer;
}
}
count += 1;
}
count
}
fn main() {
// Read blacklist from input text file
let input = fs::read_to_string("input.txt").unwrap();
let lines: Vec<&str> = input.lines().collect();
let mut ranges = Vec::new();
for line in lines {
let split: Vec<&str> = line.split('-').collect();
let lower: u32 = split[0].parse().unwrap();
let upper: u32 = split[1].parse().unwrap();
ranges.push((lower, upper));
}
ranges.sort_by_key(|x| x.0);
// Part 1 gives 23923783 for me
println!("Part 1 solution: {}", solve_part1(&ranges));
// Part 2 gives 125 for me
println!("Part 2 solution: {}", solve_part2(ranges));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_example1() {
let ranges = vec![(5, 8), (0, 2), (4, 7)];
assert_eq!(solve_part1(&ranges), 3);
}
}
|