Skip to content

Commit 81fd4f3

Browse files
committed
day13pt2: done
1 parent 06c33bf commit 81fd4f3

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ members = [
2525
"day12",
2626
"day12pt2",
2727
"day13",
28+
"day13pt2",
2829
]
2930

3031
[profile.dev]

day13pt2/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day13pt2"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day13pt2/src/main.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
fn reflects_at_row(rows: &[String], r: usize) -> bool {
2+
if r >= rows.len() - 1 {
3+
return false;
4+
}
5+
let mut mismatches: usize = 0;
6+
for i in 0..(r + 1).min(rows.len() - r - 1) {
7+
let above = r - i;
8+
let below = r + i + 1;
9+
if rows[above] != rows[below] {
10+
for (ca, cb) in rows[above].chars().zip(rows[below].chars()) {
11+
if ca != cb {
12+
mismatches += 1;
13+
}
14+
}
15+
if mismatches > 1 {
16+
break;
17+
}
18+
}
19+
}
20+
mismatches == 1
21+
}
22+
23+
fn transpose(rows: &[String]) -> Vec<String> {
24+
let mut new_rows: Vec<String> = Vec::new();
25+
let mut char_iters = rows
26+
.iter()
27+
.map(|s| s.chars())
28+
.collect::<Vec<std::str::Chars>>();
29+
loop {
30+
let row = char_iters
31+
.iter_mut()
32+
.map(Iterator::next)
33+
.flatten()
34+
.collect::<String>();
35+
if row == "" {
36+
break;
37+
} else {
38+
new_rows.push(row);
39+
}
40+
}
41+
new_rows
42+
}
43+
44+
fn main() {
45+
let mut maps: Vec<Vec<String>> = Vec::new();
46+
let mut rows: Vec<String> = Vec::new();
47+
for line in std::io::stdin().lines().map(Result::unwrap) {
48+
if line.is_empty() && !rows.is_empty() {
49+
maps.push(rows);
50+
rows = Vec::new();
51+
}
52+
if !line.is_empty() {
53+
rows.push(line.to_string());
54+
}
55+
}
56+
if !rows.is_empty() {
57+
maps.push(rows);
58+
}
59+
60+
let sum = maps
61+
.iter()
62+
.map(|rows| {
63+
let reflect_row = (0..rows.len())
64+
.position(|r| reflects_at_row(rows, r))
65+
.map(|r| r + 1)
66+
.unwrap_or(0);
67+
let t_rows = transpose(rows);
68+
let reflect_column = (0..t_rows.len())
69+
.position(|r| reflects_at_row(&t_rows, r))
70+
.map(|r| r + 1)
71+
.unwrap_or(0);
72+
reflect_row * 100 + reflect_column
73+
})
74+
.sum::<usize>();
75+
println!("{sum}");
76+
}
77+
78+
#[test]
79+
#[rustfmt::skip]
80+
fn test_transpose() {
81+
let input = &[
82+
"###",
83+
".#.",
84+
"...",
85+
].iter().map(|s| s.to_string()).collect::<Vec<String>>();
86+
let expected = &[
87+
"#..",
88+
"##.",
89+
"#..",
90+
].iter().map(|s| s.to_string()).collect::<Vec<String>>();
91+
assert_eq!(expected, &transpose(&input));
92+
}

0 commit comments

Comments
 (0)