-
Notifications
You must be signed in to change notification settings - Fork 0
/
five.rs
155 lines (147 loc) · 5.94 KB
/
five.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::collections::HashMap;
use libaoc::{Day, DayNumber};
pub fn five() -> Day<2021> {
Day::new(
DayNumber::Five,
|input| {
let mut points = HashMap::<(usize, usize), usize>::new();
let lines = input.lines();
lines.for_each(|line| {
let points_str = line.split_once(" -> ").unwrap();
let point_str_a = points_str.0.split_once(',').unwrap();
let point_str_b = points_str.1.split_once(',').unwrap();
if point_str_a.0 == point_str_b.0 || point_str_a.1 == point_str_b.1 {
let point_a = (
point_str_a.0.parse::<usize>().unwrap(),
point_str_a.1.parse::<usize>().unwrap(),
);
let point_b = (
point_str_b.0.parse::<usize>().unwrap(),
point_str_b.1.parse::<usize>().unwrap(),
);
let end: usize;
let start: usize;
if point_a.0 == point_b.0 {
if point_a.1 > point_b.1 {
end = point_a.1;
start = point_b.1;
} else {
end = point_b.1;
start = point_a.1;
};
for i in start..=end {
let point = (point_a.0, i);
if let Some(entry) = points.get_mut(&point) {
*entry += 1;
} else {
points.insert(point, 1);
}
}
} else if point_a.1 == point_b.1 {
if point_a.0 > point_b.0 {
end = point_a.0;
start = point_b.0;
} else {
end = point_b.0;
start = point_a.0;
};
for i in start..=end {
let point = (i, point_a.1);
if let Some(entry) = points.get_mut(&point) {
*entry += 1;
} else {
points.insert(point, 1);
}
}
}
}
});
let at_least_2_overlap = points
.values()
.filter(|&&intersecting| intersecting >= 2)
.count();
Box::new(at_least_2_overlap)
},
|input| {
let mut points = HashMap::<(usize, usize), usize>::new();
let lines = input.lines();
lines.for_each(|line| {
let points_str = line.split_once(" -> ").unwrap();
let point_str_a = points_str.0.split_once(',').unwrap();
let point_str_b = points_str.1.split_once(',').unwrap();
let point_a = (
point_str_a.0.parse::<usize>().unwrap(),
point_str_a.1.parse::<usize>().unwrap(),
);
let point_b = (
point_str_b.0.parse::<usize>().unwrap(),
point_str_b.1.parse::<usize>().unwrap(),
);
let end: usize;
let start: usize;
if point_a.0 == point_b.0 {
if point_a.1 > point_b.1 {
end = point_a.1;
start = point_b.1;
} else {
end = point_b.1;
start = point_a.1;
};
for i in start..=end {
let point = (point_a.0, i);
if let Some(entry) = points.get_mut(&point) {
*entry += 1;
} else {
points.insert(point, 1);
}
}
} else if point_a.1 == point_b.1 {
if point_a.0 > point_b.0 {
end = point_a.0;
start = point_b.0;
} else {
end = point_b.0;
start = point_a.0;
};
for i in start..=end {
let point = (i, point_a.1);
if let Some(entry) = points.get_mut(&point) {
*entry += 1;
} else {
points.insert(point, 1);
}
}
} else {
let mut y: usize;
let check = (point_a.0 > point_b.0 && point_a.1 < point_b.1)
|| (point_a.0 < point_b.0 && point_a.1 > point_b.1);
let xs = if point_a.0 > point_b.0 {
y = point_b.1;
point_b.0..=point_a.0
} else {
y = point_a.1;
point_a.0..=point_b.0
};
for x in xs {
let point = (x, y);
if let Some(entry) = points.get_mut(&point) {
*entry += 1;
} else {
points.insert(point, 1);
}
if check {
y -= 1;
} else {
y += 1;
}
}
}
});
let at_least_2_overlap = points
.values()
.filter(|&&intersecting| intersecting >= 2)
.count();
Box::new(at_least_2_overlap)
},
)
}