-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-8.rs
152 lines (136 loc) · 3.73 KB
/
day-8.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
use std::str::FromStr;
const INPUT: &str = include_str!("day-8.input");
#[derive(Clone, Copy)]
struct Digit(u8);
impl Digit {
fn number_of_segments(&self) -> usize {
self.0.count_ones() as usize
}
}
impl FromStr for Digit {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut segments = 0;
for c in s.chars() {
let bit = (c as usize - 'a' as usize) as usize;
assert!(bit < 7);
segments |= 1 << bit;
}
Ok(Self(segments))
}
}
struct Digits(Vec<Digit>);
impl FromStr for Digits {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(
s.trim().split(' ').map(|s| s.parse().unwrap()).collect(),
))
}
}
fn main() {
let input: Vec<_> = INPUT
.lines()
.map(|line| {
let (signals, segments) = line.split_once('|').unwrap();
(
signals.parse::<Digits>().unwrap().0,
segments.parse::<Digits>().unwrap().0,
)
})
.collect();
// part 1
println!(
"part 1: {}",
input
.iter()
.map(|(_, s)| s
.iter()
.map(Digit::number_of_segments)
.filter(|n| matches!(n, 2 | 3 | 4 | 7))
.count())
.sum::<usize>()
);
// part 2
let mut sum = 0;
for (signals, digits) in input {
let mut on = [0_u8; 10];
let mut m5 = 0x7f;
let mut m6 = 0x7f;
for &signal in signals.iter() {
match signal.number_of_segments() {
2 => {
// 1
on[1] = signal.0;
}
3 => {
// 7
on[7] = signal.0;
}
4 => {
// 4
on[4] = signal.0;
}
5 => {
// 2, 3 or 5
m5 &= signal.0;
}
6 => {
// 0, 6 or 9
m6 &= signal.0;
}
7 => {
// 8
on[8] = 0x7f;
}
_ => unreachable!(),
}
}
on[3] = m5;
on[0] = m6;
//let a = on[1] ^ on[7];
let d = on[3] & on[4];
on[0] = on[8] ^ d;
on[3] |= on[1];
// 0 1 3 4 7 8
for &signal in signals.iter() {
match signal.number_of_segments() {
2 | 3 | 4 | 7 => (),
5 => {
// 2, 3 or 5
if signal.0 != on[3] {
// 2 or 5
if (signal.0 & on[4]).count_ones() == 3 {
on[5] = signal.0;
} else {
on[2] = signal.0;
}
}
}
6 => {
// 0, 6 or 9
if signal.0 != on[0] {
// 6 or 9
if (signal.0 & on[1]).count_ones() == 2 {
on[9] = signal.0;
} else {
on[6] = signal.0;
}
}
}
_ => unreachable!(),
}
}
let mut val = 0;
for &digit in digits.iter() {
for (i, &o) in on.iter().enumerate() {
if o == digit.0 {
val = val * 10 + i;
break;
}
}
}
sum += val;
}
println!("part 2: {}", sum);
}