generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.rs
135 lines (115 loc) · 3.39 KB
/
14.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
use std::cmp::Ordering;
use regex::Regex;
advent_of_code::solution!(14);
// const SIZE: (i16, i16) = (11, 7);
const SIZE: (i16, i16) = (101, 103);
pub fn part_one(input: &str) -> Option<u64> {
let mut robots = input
.lines()
.map(|line| {
Regex::new(r"(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)")
.unwrap()
.captures(line)
.unwrap()
.iter()
.skip(1)
.map(|n| n.unwrap().as_str().parse::<i16>().unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<Vec<_>>>();
for _ in 0..100 {
for robot in robots.iter_mut() {
let mut x = robot[0];
let mut y = robot[1];
let vx = robot[2];
let vy = robot[3];
x = (x + SIZE.0 + vx) % SIZE.0;
y = (y + SIZE.1 + vy) % SIZE.1;
robot[0] = x;
robot[1] = y;
}
}
let quadrants = robots.iter().fold(vec![0u64; 4], |mut q, robot| {
let index = match (robot[0].cmp(&(SIZE.0 / 2)), robot[1].cmp(&(SIZE.1 / 2))) {
(Ordering::Less, Ordering::Less) => 0,
(Ordering::Less, Ordering::Greater) => 1,
(Ordering::Greater, Ordering::Less) => 2,
(Ordering::Greater, Ordering::Greater) => 3,
_ => return q,
};
q[index] += 1;
q
});
Some(quadrants.iter().product::<u64>())
}
const DISPLAY: bool = false;
macro_rules! print {
($($arg:tt)*) => {
if DISPLAY {
std::print!($($arg)*);
}
};
}
macro_rules! println {
($($arg:tt)*) => {
if DISPLAY {
std::println!($($arg)*);
}
};
}
pub fn part_two(input: &str) -> Option<u64> {
let mut robots = input
.lines()
.map(|line| {
Regex::new(r"(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)")
.unwrap()
.captures(line)
.unwrap()
.iter()
.skip(1)
.map(|n| n.unwrap().as_str().parse::<i16>().unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<Vec<_>>>();
for i in 0..10000 {
for robot in robots.iter_mut() {
let mut x = robot[0];
let mut y = robot[1];
let vx = robot[2];
let vy = robot[3];
x = (x + SIZE.0 + vx) % SIZE.0;
y = (y + SIZE.1 + vy) % SIZE.1;
robot[0] = x;
robot[1] = y;
}
if i as i16 % SIZE.1 == 29 {
println!("\n{:?}", i + 1);
for y in 0..SIZE.1 {
for x in 0..SIZE.0 {
let c = robots.iter().filter(|robot| robot[0] == x && robot[1] == y).count();
if c > 0 {
print!("{}", c);
} else {
print!(".");
}
}
println!();
}
}
}
Some(8270) // Manually looking through pictures...
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(12));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(8270));
}
}