From 8eb7e8495cb3df4ccae5f162c1dfbd66229fdfe4 Mon Sep 17 00:00:00 2001 From: nekineki Date: Wed, 13 Dec 2023 09:06:28 +0100 Subject: day13 part1. part2 bi mogu delat --- 2023/day13.rs | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 2023/day13.rs (limited to '2023/day13.rs') 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(original: Vec>) -> Vec> { + assert!(!original.is_empty()); + let mut transposed = (0..original[0].len()).map(|_| vec![]).collect::>(); + + 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>, 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>) -> usize { + for i in 0..arr.len() - 1 { + if lines_mirrored(arr, i) { + return i + 1; + } + } + return 0; +} + +fn print_arr(arr: &Vec>) { + 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::new(); + for line in group.split("\n") { + let mut l: Vec = 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); +} -- cgit v1.2.3