summaryrefslogtreecommitdiff
path: root/d08/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'd08/src/main.rs')
-rw-r--r--d08/src/main.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/d08/src/main.rs b/d08/src/main.rs
new file mode 100644
index 0000000..cbdd1fd
--- /dev/null
+++ b/d08/src/main.rs
@@ -0,0 +1,78 @@
+use std::fs;
+
+fn solve_puzzle(lines: &Vec<&str>) -> [[bool; 6]; 50] {
+ let mut pixels = [[false; 6]; 50];
+
+ for line in lines {
+ let words: Vec<&str> = line.split_ascii_whitespace().collect();
+ if words[0] == "rect" {
+ // Read operation parameters
+ let wxh: Vec<&str> = words[1].split('x').collect();
+ let w: usize = wxh[0].parse().unwrap();
+ let h: usize = wxh[1].parse().unwrap();
+
+ // Apply operation to state
+ for x in 0..w {
+ for y in 0..h {
+ pixels[x][y] = true;
+ }
+ }
+ } else if words[0] == "rotate" && words[1] == "row" {
+ // Read operation parameters
+ let which: Vec<&str> = words[2].split('=').collect();
+ let y: usize = which[1].parse().unwrap();
+ let n: usize = words[4].parse().unwrap();
+
+ // Apply operation to state
+ let mut old = Vec::new();
+ for x in 0..50 {
+ old.push(pixels[x][y]);
+ }
+ for x in 0..50 {
+ pixels[x][y] = old[(x + 50 - n) % 50];
+ }
+ } else if words[0] == "rotate" && words[1] == "column" {
+ // Read operation parameters
+ let which: Vec<&str> = words[2].split('=').collect();
+ let x: usize = which[1].parse().unwrap();
+ let n: usize = words[4].parse().unwrap();
+
+ // Apply operation to state
+ let old = pixels[x];
+ for y in 0..6 {
+ pixels[x][y] = old[(y + 6 - n) % 6];
+ }
+ }
+ }
+
+ pixels
+}
+
+fn main() {
+ let input = fs::read_to_string("input.txt").unwrap();
+ let lines = input.lines().collect();
+
+ let pixels = solve_puzzle(&lines);
+
+ // Part 2 must be read by eye, gives "UPOJFLBCEZ" for me
+ println!("Part 2 solution:");
+ let mut total = 0;
+ for y in 0..6 {
+ print!("\t");
+ for x in 0..50 {
+ print!(
+ "{}",
+ if pixels[x][y] {
+ total += 1;
+ '#'
+ } else {
+ ' '
+ }
+ );
+ }
+ print!("\n");
+ }
+
+ // Part 1 gives 116 for me
+ println!("Part 1 solution: {}", total);
+}