-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rs
131 lines (104 loc) · 3.29 KB
/
day13.rs
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
127
128
129
130
131
use std::iter::zip;
use itertools::Itertools;
use num::range;
use crate::input_reader::read_lines;
fn summarize_notes(notes: &Vec<Vec<String>>, maximum_smudge: usize) -> usize {
notes.iter().map(|note| summarize_note(note, maximum_smudge)).sum::<usize>()
}
fn summarize_note(note: &Vec<String>, maximum_smudge: usize) -> usize {
if let Some(reflection_row) = finds_reflection_row(note, maximum_smudge) {
return reflection_row * 100;
}
if let Some(reflection_column) = finds_reflection_column(note, maximum_smudge) {
return reflection_column;
}
panic!("Should have find something!")
}
fn finds_reflection_column(note: &Vec<String>, maximum_smudge: usize) -> Option<usize> {
finds_reflection_row(&flip_note(note), maximum_smudge)
}
fn finds_reflection_row(note: &Vec<String>, maximum_smudge: usize) -> Option<usize> {
finds_candidate_reflections(note).iter()
.flat_map(|&candidate| try_candidate(candidate, note, maximum_smudge))
.next()
}
fn try_candidate(candidate: usize, note: &Vec<String>, maximum_smudge: usize) -> Option<usize> {
let note_len = note.len() as isize;
let mut current: isize = candidate as isize;
let mut next: isize = (candidate + 1) as isize;
let mut total_smudge: usize = 0;
while current >= 0 && next < note_len {
total_smudge += smudge(¬e[current as usize], ¬e[next as usize]);
next += 1;
current -= 1;
}
if total_smudge != maximum_smudge {
return None;
}
Some(candidate + 1)
}
fn finds_candidate_reflections(note: &Vec<String>) -> Vec<usize> {
(0..note.len() - 1)
.filter(|&prev| smudge(¬e[prev], ¬e[prev + 1]) <= 1)
.collect()
}
fn flip_note(note: &Vec<String>) -> Vec<String> {
let mut flipped_note = vec![];
for y in range(0, note[0].len()) {
let mut column: Vec<char> = vec![];
for row in note {
column.push(row.chars().nth(y).unwrap());
}
flipped_note.push(column.iter().join(""));
}
flipped_note
}
fn parse_notes(input: &str) -> Vec<Vec<String>> {
read_lines(input).split(|l| l.is_empty())
.map(|s| s.to_vec())
.collect::<Vec<_>>()
}
fn smudge(s1: &str, s2: &str) -> usize {
let mut diff: usize = 0;
for (c1, c2) in zip(s1.chars(), s2.chars()) {
if c1 != c2 {
diff += 1;
}
}
diff
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use crate::day13::*;
use crate::input_reader::read_input_file;
#[test]
fn it_solves_puzzle() {
let input = &read_input_file("input_day13.txt");
assert_eq!(30802, summarize_notes(&parse_notes(input), 0));
assert_eq!(37876, summarize_notes(&parse_notes(input), 1));
}
#[test]
fn it_summarize_notes() {
let input = indoc! {"
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#"};
let notes = parse_notes(input);
assert_eq!(5, summarize_note(¬es[0], 0));
assert_eq!(400, summarize_note(¬es[1], 0));
assert_eq!(405, summarize_notes(¬es, 0));
assert_eq!(400, summarize_notes(¬es, 1));
}
}