-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_08.rs
156 lines (127 loc) Β· 4.28 KB
/
day_08.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use common::{Answer, Solution};
use hashbrown::HashMap;
const CHARS: [char; 7] = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const DIGITS: [&str; 10] = [
"abcefg", "cf", "acdeg", "acdfg", "bcdf", "abdfg", "abdefg", "acf", "abcdefg", "abcdfg",
];
pub struct Day08;
impl Solution for Day08 {
fn name(&self) -> &'static str {
"Seven Segment Search"
}
fn part_a(&self, input: &str) -> Answer {
let data = parse(input);
let mut inc = 0;
for i in data {
inc +=
i.1.iter()
.filter(|x| [2, 3, 4, 7].contains(&x.len()))
.count();
}
inc.into()
}
fn part_b(&self, input: &str) -> Answer {
let data = parse(input);
let mut inc = 0;
let perms = permutations(CHARS.to_vec());
let mut sort_digits = DIGITS.to_vec();
sort_digits.sort_unstable();
for i in data {
for p in &perms {
let mut wires = HashMap::new();
for j in CHARS {
let pos = CHARS.iter().position(|x| *x == j).unwrap();
*wires.entry(j).or_insert(p[pos]) = p[pos];
}
let mut new_clues = Vec::new();
for clue in &i.0 {
let mut x = String::new();
for char in clue.chars() {
x.push(*wires.get(&char).unwrap());
}
let mut to_sort = x.chars().collect::<Vec<char>>();
to_sort.sort_unstable();
new_clues.push(
to_sort
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(""),
);
}
new_clues.sort();
if new_clues == sort_digits {
let mut n = Vec::new();
for d in &i.1 {
let mut x = String::new();
for char in d.chars() {
x.push(*wires.get(&char).unwrap());
}
let mut to_sort = x.chars().collect::<Vec<char>>();
to_sort.sort_unstable();
x = to_sort
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("");
n.push(DIGITS.iter().position(|i| **i == x).unwrap());
}
inc += n
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("")
.parse::<u32>()
.unwrap();
break;
}
}
}
inc.into()
}
}
fn parse(inp: &str) -> Vec<(Vec<String>, Vec<String>)> {
let mut out = Vec::new();
for i in inp.lines() {
let mut parts = i.split('|');
let test = parts
.next()
.unwrap()
.split(' ')
.filter(|x| !x.is_empty())
.map(|x| x.to_owned())
.collect::<Vec<String>>();
let check = parts
.next()
.unwrap()
.split(' ')
.filter(|x| !x.is_empty())
.map(|x| x.to_owned())
.collect::<Vec<String>>();
out.push((test, check));
}
out
}
// Modified from https://stackoverflow.com/a/59939809/12471934
fn permutations<T: Clone>(items: Vec<T>) -> Vec<Vec<T>>
where
T: Ord,
{
if items.len() == 1 {
return vec![items];
}
let mut output: Vec<Vec<T>> = Vec::new();
let mut unique_items = items.clone();
unique_items.sort();
unique_items.dedup();
for first in unique_items {
let mut remaining_elements = items.clone();
let index = remaining_elements.iter().position(|x| *x == first).unwrap();
remaining_elements.remove(index);
for mut permutation in permutations(remaining_elements) {
permutation.insert(0, first.clone());
output.push(permutation);
}
}
output
}