-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_1.rs
65 lines (55 loc) · 1.86 KB
/
day_1.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
pub fn parse(input_raw: &str) -> Result<(Vec<u32>, Vec<u32>), String> {
let mut left: Vec<u32> = vec![];
let mut right: Vec<u32> = vec![];
for line in input_raw.lines() {
let line_split = line.split_ascii_whitespace().collect::<Vec<&str>>();
left.push(line_split[0].parse().unwrap());
right.push(line_split[1].parse().unwrap());
}
left.sort_unstable();
right.sort_unstable();
Ok((left, right))
}
pub fn part_one(cols: &(Vec<u32>, Vec<u32>)) -> Result<u32, String> {
let (left, right) = cols;
Ok(
left.iter()
.zip(right)
.fold(0,
|acc,(&l,&r)| acc + l.abs_diff(r)
)
)
}
pub fn part_two(cols: &(Vec<u32>, Vec<u32>)) -> Result<usize, String> {
let (left, right) = cols;
let mut left = left.iter();
let mut right = right.iter();
let mut l = left.next();
let mut r = left.next();
let mut total = 0;
while l.is_some() && r.is_some() {
let lx = *l.unwrap();
let rx = *r.unwrap();
match lx.cmp(&rx) {
std::cmp::Ordering::Less => {
let rx = r.unwrap();
let _ = left.by_ref().skip_while(|&x| x < rx);
l = left.next();
},
std::cmp::Ordering::Greater => {
let lx = l.unwrap();
let _ = right.by_ref().skip_while(|&x| x < lx);
r = right.next();
},
std::cmp::Ordering::Equal => {
let n = l.unwrap();
let l_count = 1 + left.by_ref().take_while(|&x| x == n).count(); // this seems to only ever be 1 but whatever
let r_count = 1 + right.by_ref().take_while(|&x| x == n).count();
total += l_count * r_count;
l = left.next();
r = right.next();
},
}
}
Ok(total)
}