summaryrefslogtreecommitdiff
path: root/2023/day13.rs
diff options
context:
space:
mode:
Diffstat (limited to '2023/day13.rs')
-rw-r--r--2023/day13.rs126
1 files changed, 126 insertions, 0 deletions
diff --git a/2023/day13.rs b/2023/day13.rs
new file mode 100644
index 0000000..b2f2b05
--- /dev/null
+++ b/2023/day13.rs
@@ -0,0 +1,126 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_mut)]
+use std::fs::File;
+use std::io::Read;
+
+fn transpose<T>(original: Vec<Vec<T>>) -> Vec<Vec<T>> {
+ assert!(!original.is_empty());
+ let mut transposed = (0..original[0].len()).map(|_| vec![]).collect::<Vec<_>>();
+
+ for original_row in original {
+ for (item, transposed_row) in original_row.into_iter().zip(&mut transposed) {
+ transposed_row.push(item);
+ }
+ }
+ transposed
+}
+
+fn lines_mirrored(arr: &Vec<Vec<bool>>, start: usize) -> bool {
+ for (i, j) in (0..=start).rev().zip(start + 1..arr.len()) {
+ if arr[i] != arr[j] {
+ return false;
+ }
+ }
+ return true;
+}
+
+fn mirror_horizontal_line(arr: &Vec<Vec<bool>>) -> usize {
+ for i in 0..arr.len() - 1 {
+ if lines_mirrored(arr, i) {
+ return i + 1;
+ }
+ }
+ return 0;
+}
+
+fn print_arr(arr: &Vec<Vec<bool>>) {
+ for i in 0..arr.len() {
+ for j in 0..arr[0].len() {
+ if arr[i][j] == true {
+ print!("#");
+ } else {
+ print!(".");
+ }
+ }
+ println!("");
+ }
+ println!("");
+}
+
+fn main() {
+ // let filename = "in/day13.ref";
+ let filename = "in/day13.pzl";
+
+ let mut f = File::open(filename).expect("cannot open file");
+ let mut content = String::new();
+ f.read_to_string(&mut content).expect("cannot read file");
+ let groups = content.trim_end().split("\n\n");
+
+ let mut res1 = 0;
+ let mut res2 = 0;
+
+ for group in groups {
+ let mut arr: Vec<Vec<bool>> = Vec::new();
+ for line in group.split("\n") {
+ let mut l: Vec<bool> = Vec::new();
+ for c in line.chars() {
+ l.push(if c == '#' { true } else { false });
+ }
+ arr.push(l);
+ }
+
+ let (x, y) = (
+ mirror_horizontal_line(&transpose(arr.clone())),
+ mirror_horizontal_line(&arr),
+ );
+ res1 += x + 100 * y;
+ println!("aaa {x} {y}");
+
+ if x != 0 && y != 0 {
+ panic!("asdf");
+ }
+
+ 'outer: for i in 0..arr.len() {
+ for j in 0..arr[0].len() {
+ let mut a = arr.clone();
+ a[i][j] ^= true;
+ let (x2, y2) = (
+ mirror_horizontal_line(&transpose(a.clone())),
+ mirror_horizontal_line(&a),
+ );
+
+ if x == 0 {
+ if (y != y2) && (y2 != 0) {
+ println!("a {x} {y}, {x2} {y2}");
+ print_arr(&a);
+ res2 += 100 * y2;
+ break 'outer;
+ } else if x2 != 0 {
+ println!("b {x} {y}, {x2} {y2}");
+ print_arr(&a);
+ res2 += x2;
+ break 'outer;
+ }
+ }
+
+ if y == 0 {
+ if y2 != 0 {
+ println!("c {x} {y}, {x2} {y2}");
+ print_arr(&a);
+ res2 += 100 * y2;
+ break 'outer;
+ } else if (x != x2) && (x2 != 0) {
+ println!("d {x} {y}, {x2} {y2}");
+ print_arr(&a);
+ res2 += x2;
+ break 'outer;
+ }
+ }
+ }
+ }
+ }
+
+ println!("res1: {}", res1);
+ println!("res2: {}", res2);
+}