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
|
#![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);
}
|