-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
150 lines (139 loc) · 4.65 KB
/
main.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
use itertools::Itertools;
use std::collections::HashMap;
use std::str::FromStr;
const FILE_PATH: &str = "input.txt";
const REGULAR_PATTERNS: &[&str; 10] = &[
"abcefg", // 0
"cf", // 1
"acdeg", // 2
"acdfg", // 3
"bcdf", // 4
"abdfg", // 5
"abdefg", // 6
"acf", // 7
"abcdefg", // 8
"abcdfg", // 9
];
#[derive(Debug, Clone)]
struct Entry {
patterns: Vec<String>,
output_values: Vec<String>,
}
impl FromStr for Entry {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (pattern, output) = s
.split_once('|')
.ok_or_else(|| format!("{} needs 2 elements", s))?;
let mut patterns: Vec<String> = pattern
.split_ascii_whitespace()
.map(ToString::to_string)
.collect();
patterns.sort_unstable_by_key(String::len);
let output_values: Vec<String> = output
.split_ascii_whitespace()
.map(ToString::to_string)
.collect();
Ok(Self {
patterns,
output_values,
})
}
}
impl Entry {
fn pattern_matcher(&self) -> HashMap<char, Vec<char>> {
let mut done_values: Vec<char> = vec![];
self.patterns
.iter()
.filter(|p| [2, 3, 4, 7].contains(&p.len()))
.fold(HashMap::new(), |mut map, pattern| {
let new_values = REGULAR_PATTERNS
.iter()
.filter(|p| p.len() == pattern.len())
.fold(vec![], |mut vec, matched| {
let to = matched.chars().collect::<Vec<char>>();
for from_char in pattern.chars() {
let entry = map.entry(from_char).or_insert_with(|| {
to.iter()
.copied()
.filter(|c| !done_values.contains(c))
.map(|c| {
vec.push(c);
c
})
.collect()
});
*entry = entry.iter().filter(|c| to.contains(c)).copied().collect();
}
vec
});
done_values.extend(new_values);
map
})
}
fn possible_matches(output: &str, mapper: &HashMap<char, Vec<char>>) -> Vec<String> {
output.chars().fold(vec![], |acc, c| {
mapper
.get(&c)
.map(|options| {
options
.iter()
.flat_map(|option| {
if acc.is_empty() {
vec![option.to_string()]
} else {
acc.iter()
.map(|s| format!("{}{}", s, option))
.collect::<Vec<String>>()
}
})
.collect()
})
.unwrap_or(acc)
})
}
fn match_output(output: &str, mapper: &HashMap<char, Vec<char>>) -> usize {
let possible_matches = Self::possible_matches(output, mapper);
for candidate in &possible_matches {
let str: String = candidate.chars().sorted().collect();
if let Some(pos) = REGULAR_PATTERNS.iter().position(|s| s == &str.as_str()) {
return pos;
}
}
panic!("No matches for {}", output)
}
fn outputs_sum_str(&self) -> String {
let mapper = self.pattern_matcher();
self.output_values
.iter()
.map(|output_value| Self::match_output(output_value, &mapper).to_string())
.collect()
}
}
fn part1(entries: &[Entry]) -> usize {
entries
.iter()
.map(|entry| {
entry
.output_values
.iter()
.filter(|digit| [2, 3, 4, 7].contains(&digit.len()))
.count()
})
.sum()
}
fn part2(entries: &[Entry]) -> usize {
entries
.iter()
.map(|entry| entry.outputs_sum_str().parse::<usize>().unwrap())
.sum()
}
fn main() {
let entries: Vec<Entry> = std::fs::read_to_string(FILE_PATH)
.unwrap()
.lines()
.map(|s| Entry::from_str(s).unwrap())
.collect();
println!("Part1: {} outputs use 1, 4, 7, or 8 digit", part1(&entries));
println!("Part2: Output sum is {}", part2(&entries));
}