-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_4.rs
40 lines (34 loc) · 980 Bytes
/
day_4.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
pub fn run() {
let input_str = include_str!("../../inputs/input_4.txt")
.replace("\r\n", "\n");
let input: Vec<Vec<i32>> = input_str
.lines()
.map(|pair| pair
.split(&['-', ','])
.map(|i| i.parse().unwrap())
.collect::<Vec<i32>>()
).collect();
part_one(&input);
part_two(&input);
}
fn part_one(input: &Vec<Vec<i32>>) {
let mut count = 0;
for line in input {
if (line[0] <= line[2] && line[1] >= line[3]) ||
(line[0] >= line[2] && line[1] <= line[3]) {
count += 1;
}
}
println!("Part one: {count}");
}
fn part_two(input: &Vec<Vec<i32>>) {
let mut count = 0;
for line in input {
if (line[0] <= line[3] && line[1] >= line[3]) ||
(line[0] <= line[2] && line[1] >= line[2]) ||
(line[0] >= line[2] && line[1] <= line[3]) {
count += 1;
}
}
println!("Part two: {count}");
}