use std::fs::File; use std::cmp; use std::io::{self, prelude::*, BufReader}; fn get_column_symmetry(map: &Vec, last_ref: u32) -> u32 { let mut row_vec: Vec = (0..map[1].len()).map(|col| { (1..map.len()) .map(|row| map[row].get(col..col+1).unwrap()) .collect() }).collect(); row_vec.insert(0, "".to_string()); return get_row_symmetry(&row_vec, last_ref); } fn get_row_symmetry(map: &Vec, last_ref: u32) -> u32 { let mut sum: u32 = 0; for i in 1..map.len()-1 { let min: usize = cmp::min(i as usize , map.len()-i-1 as usize); // println!("min {}", min); let mut symmetric = true; for j in 0..min { if map[i as usize - j] != map[i as usize + j + 1]{ symmetric = false; break; } } if symmetric { if u32::try_from(i).unwrap() != last_ref{ sum += u32::try_from(i).unwrap(); } } } sum } fn process_map(map: Vec) -> u32 { let mut ret = 0; let row = get_row_symmetry(&map,0); let col = get_column_symmetry(&map,0); for i in 1..map.len(){ for j in 0..map[1].len(){ let c = map[i].get(j..j+1).unwrap(); let new_c; if c == "."{ new_c = "#"; } else if c == "#"{ new_c = "."; } else{ panic!(); } let mut new_map = map.clone(); new_map[i].replace_range(j..j+1, new_c); let new_row = get_row_symmetry(&new_map,row); let new_col = get_column_symmetry(&new_map, col); let mut matched = false; if (new_row!=0 ) && ((new_row != row)){ ret += 100*new_row; matched = true; } if (new_col!=0) && ((new_col != col)){ ret += new_col; matched = true; } if matched { return ret; } } } return ret; } fn process_text(wall_of_text: Vec) -> u32 { let mut map_start = 0; let mut map_end = 0; let length = wall_of_text.len(); let mut answer = 0; while map_end < length-1 { for count in map_end+1..length { if wall_of_text[count] == "" { map_start = map_end; map_end = count; break; } else if count == length-1 { map_start = map_end; map_end = count+1; break; } } answer += process_map(wall_of_text.get(map_start..map_end).unwrap().to_vec()); } return answer; } fn main() -> io::Result<()> { let file = File::open("../input.txt")?; let reader = BufReader::new(file); let answer = process_text( reader.lines().collect::>>().expect("failed") ); println!("{}", answer); Ok(()) }