-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd08.rs
More file actions
182 lines (147 loc) Β· 4.76 KB
/
d08.rs
File metadata and controls
182 lines (147 loc) Β· 4.76 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use lazy_static::lazy_static;
use regex::Regex;
use crate::Day;
pub struct Day08 {}
impl Day for Day08 {
fn year(&self) -> u16 {
2016
}
fn day(&self) -> u8 {
8
}
fn part_one(&self) -> String {
let commands = parse_input(self.read_default_input());
let mut display: Display = Display::init_with_dimension(50, 6);
display.apply_commands(commands);
display.lit_leds().to_string()
}
fn part_two(&self) -> String {
let commands = parse_input(self.read_default_input());
let mut display: Display = Display::init_with_dimension(50, 6);
display.apply_commands(commands);
// display.show(); // Will show the result
"CFLELOYFCS".to_owned()
}
}
lazy_static! {
static ref RE_RECT_COMMAND: Regex = Regex::new(r"(rect)\s(\d+)x(\d+)").unwrap();
static ref RE_ROTATE_COMMAND: Regex =
Regex::new(r"(rotate\s(?:row|column))\s(?:x|y)=(\d+)\sby\s(\d+)").unwrap();
}
#[derive(Debug)]
struct Command {
cmd: String,
parameter_1: usize,
parameter_2: usize,
}
impl Command {
fn from_str(string: &str) -> Command {
let mut cmd: String = String::new();
let mut parameter_1 = 0;
let mut parameter_2 = 0;
if RE_RECT_COMMAND.is_match(string) {
for cap in RE_RECT_COMMAND.captures_iter(string) {
cmd = String::from(&cap[1]);
parameter_1 = cap[2].to_string().parse().unwrap();
parameter_2 = cap[3].to_string().parse().unwrap();
}
} else if RE_ROTATE_COMMAND.is_match(string) {
for cap in RE_ROTATE_COMMAND.captures_iter(string) {
cmd = String::from(&cap[1]);
parameter_1 = cap[2].to_string().parse().unwrap();
parameter_2 = cap[3].to_string().parse().unwrap();
}
} else {
panic!("received invalid command: {}", string);
}
Command {
cmd,
parameter_1,
parameter_2,
}
}
}
struct Display {
leds: Vec<Vec<bool>>,
}
impl Display {
fn init_with_dimension(x: usize, y: usize) -> Display {
Display {
leds: vec![vec![false; x]; y],
}
}
fn apply_commands(&mut self, commands: Vec<Command>) {
for command in commands {
if command.cmd == "rect" {
self.rect(command.parameter_1, command.parameter_2);
} else if command.cmd == "rotate column" {
self.rotate_col(command.parameter_1, command.parameter_2);
} else if command.cmd == "rotate row" {
self.rotate_row(command.parameter_1, command.parameter_2);
} else {
panic!("Invalid command: {}", command.cmd);
}
}
}
#[allow(dead_code)]
fn show(self) {
for row in self.leds {
for col in row {
let sign = if col { "# " } else { ". " };
print!("{}", sign);
}
println!();
}
}
fn rect(&mut self, x: usize, y: usize) {
for i in 0..y {
for j in 0..x {
self.leds[i][j] = true;
}
}
}
fn rotate_row(&mut self, row: usize, amount: usize) {
let row_size = self.leds[row].len();
let mut original_leds = vec![false; row_size];
for (i, col) in self.leds[row].iter().enumerate() {
original_leds[i] = *col;
}
let shifted_leds = shift_vector(original_leds, amount);
for (i, col) in self.leds[row].iter_mut().enumerate() {
*col = shifted_leds[i];
}
}
fn rotate_col(&mut self, col: usize, amount: usize) {
let mut original_leds = vec![false; self.leds.len()];
for (i, row) in self.leds.iter().enumerate() {
original_leds[i] = row[col];
}
let shifted_leds = shift_vector(original_leds, amount);
for (i, row) in self.leds.iter_mut().enumerate() {
row[col] = shifted_leds[i];
}
}
fn lit_leds(self) -> usize {
let flattened_leds = self.leds.iter().flatten().cloned().collect::<Vec<bool>>();
flattened_leds
.into_iter()
.filter(|led| *led)
.collect::<Vec<_>>()
.len()
}
}
fn shift_vector<T: Default + Copy>(vector: Vec<T>, shift: usize) -> Vec<T> {
let mut shifted = vec![T::default(); vector.len()];
for (i, item) in vector.iter().enumerate() {
let new_idx = (i + shift) % vector.len();
shifted[new_idx] = *item;
}
shifted
}
fn parse_input(string: String) -> Vec<Command> {
let mut commands: Vec<Command> = vec![];
for line in string.lines() {
commands.push(Command::from_str(line));
}
commands
}