-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday08.rs
60 lines (53 loc) · 1.64 KB
/
day08.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
//! # Two-Factor Authentication
//!
//! The pixels are sparse enough that's it efficient to store them as [`Point`] objects and
//! manipulate individually. Pixels don't overlap so we can use a vec instead of a set to store
//! distinct points without overcounting.
//!
//! [`Point`]: crate::util::point
use crate::util::iter::*;
use crate::util::parse::*;
use crate::util::point::*;
pub fn parse(input: &str) -> Vec<Point> {
let amounts = input.iter_signed::<i32>().chunk::<2>();
let mut points = Vec::new();
for (line, [a, b]) in input.lines().zip(amounts) {
if line.starts_with("rect") {
for x in 0..a {
for y in 0..b {
points.push(Point::new(x, y));
}
}
} else if line.starts_with("rotate row") {
for point in &mut points {
if point.y == a {
point.x = (point.x + b) % 50;
}
}
} else {
for point in &mut points {
if point.x == a {
point.y = (point.y + b) % 6;
}
}
}
}
points
}
pub fn part1(input: &[Point]) -> usize {
input.len()
}
pub fn part2(input: &[Point]) -> String {
let width = input.iter().map(|p| p.x).max().unwrap() + 1;
let mut pixels = vec!['.'; width as usize * 6];
for point in input {
pixels[(width * point.y + point.x) as usize] = '#';
}
let mut result = pixels
.chunks_exact(width as usize)
.map(|row| row.iter().collect())
.collect::<Vec<String>>()
.join("\n");
result.insert(0, '\n');
result
}