-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay13.rs
81 lines (70 loc) · 1.93 KB
/
Day13.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
use std::collections::HashSet;
use std::env;
use std::io;
fn main() {
let mut points: HashSet<(u32, u32)> = HashSet::new();
let mut folds: Vec<(char, u32)> = Vec::new();
let mut parse_nums = true;
for l in io::stdin().lines().map(Result::unwrap) {
if l.is_empty() {
parse_nums = false;
continue;
}
if parse_nums {
let (a, b) = l.split_once(',').unwrap();
points.insert((a.parse().unwrap(), b.parse().unwrap()));
} else {
let (a, b) = l.split_once('=').unwrap();
folds.push((a.chars().last().unwrap(), b.parse().unwrap()));
}
}
let bound_of_axis = |axis: char| {
folds
.iter()
.filter(|(a, _)| *a == axis)
.map(|(_, v)| v)
.max()
.unwrap()
* 2
+ 1
};
let mut w = bound_of_axis('x');
let mut h = bound_of_axis('y');
let (top, is_first) = if env::args().nth(1).unwrap() == "1" {
(1, true)
} else {
(folds.len(), false)
};
for (dir, n) in folds.iter().take(top) {
let mut new_points: HashSet<(u32, u32)> = HashSet::new();
for p @ (x, y) in points.iter() {
new_points.insert(match dir {
'x' if x >= n => (w - x - 1, *y),
'y' if y >= n => (*x, h - y - 1),
_ => *p,
});
}
match dir {
'x' => w /= 2,
'y' => h /= 2,
_ => (),
}
points = new_points;
}
if is_first {
println!("{}", points.len());
} else {
for y in 0..h {
for x in 0..w {
print!(
"{}",
match points.contains(&(x, y)) {
true => '#',
false => ' ',
}
)
}
println!();
}
}
}