-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay08.c
More file actions
186 lines (146 loc) · 4.78 KB
/
Day08.c
File metadata and controls
186 lines (146 loc) · 4.78 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "Helpers.c"
#define CAP 200
typedef struct {
int signals[10];
int outputs[4];
} Entry;
static int segmentFromChar(char c) {
switch (c) {
case 'a': return 1;
case 'b': return 2;
case 'c': return 4;
case 'd': return 8;
case 'e': return 16;
case 'f': return 32;
case 'g': return 64;
default:
assert(0);
}
}
static int segmentsFromString(const char *s) {
int segments = 0;
for (; *s != 0; ++s) {
segments |= segmentFromChar(*s);
}
return segments;
}
static int parse(const char *input, Entry entries[CAP]) {
const char *inputStringEnd = input + strlen(input);
int charsRead = 0;
int n = 0;
char segmentsString[8] = {0};
for (; input < inputStringEnd; ++n) {
for (int i = 0; i < 10; ++i) {
int filled = sscanf(input, "%7[^ ]%n", segmentsString, &charsRead);
assert(filled == 1);
entries[n].signals[i] = segmentsFromString(segmentsString);
input += charsRead + 1;
assert(n < CAP);
}
sscanf(input, "| %n", &charsRead);
input += charsRead;
for (int i = 0; i < 4; ++i) {
int filled = sscanf(input, "%7[^ \n]%n", segmentsString, &charsRead);
assert(filled == 1);
entries[n].outputs[i] = segmentsFromString(segmentsString);
input += charsRead + 1;
}
}
assert(n > 0);
return n;
}
static int countSegments(int segments) {
int c = 0;
for (; segments > 0; segments >>= 1) {
c += segments & 1;
}
return c;
}
static int valueFromOutputs(const int decodedDigits[10], const int outputs[4]) {
int value = 0;
for (int i = 0, base = 1000; i < 4; ++i, base /= 10) {
for (int j = 0; j < 10; ++j) {
if (outputs[i] == decodedDigits[j]) {
value += j * base;
break;
}
}
}
return value;
}
static int partOne(int n, const Entry entries[n]) {
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 4; ++j) {
switch (countSegments(entries[i].outputs[j])) {
case 2: // 1
case 3: // 7
case 4: // 4
case 7: // 8
++count;
break;
}
}
}
return count;
}
static int partTwo(int n, const Entry entries[n]) {
int sum = 0;
for (int i = 0; i < n; ++i) {
int decodedDigits[10] = {0};
for (int j = 0; j < 10; ++j) {
int signals = entries[i].signals[j];
int activeSegments = countSegments(signals);
if (activeSegments == 2) {
decodedDigits[1] = signals;
} else if (activeSegments == 3) {
decodedDigits[7] = signals;
} else if (activeSegments == 4) {
decodedDigits[4] = signals;
} else if (activeSegments == 7) {
decodedDigits[8] = signals;
}
}
// We known the two active segments are C and F, but not exactly which one
int cAndF = decodedDigits[1];
// We known the two active segments are B and D, but not exactly which one
int bAndD = decodedDigits[4] & (~decodedDigits[1]);
for (int j = 0; j < 10; ++j) {
int signals = entries[i].signals[j];
int activeSegments = countSegments(signals);
if (activeSegments == 5) {
// 2, 3 or 5
if (countSegments(cAndF & signals) == 2) { // If C and F active then it's a 3
decodedDigits[3] = signals;
} else if (countSegments(bAndD & signals) == 1) { // If B or D active then it's a 2
decodedDigits[2] = signals;
} else { // Otherwise 5
decodedDigits[5] = signals;
}
} else if (activeSegments == 6) {
// 0, 6 or 9
if (countSegments(cAndF & signals) == 1) { // If C or F active then it's a 6
decodedDigits[6] = signals;
} else if (countSegments(bAndD & signals) == 2) { // If B and D active then it's a 9
decodedDigits[9] = signals;
} else { // Otherwise 0
decodedDigits[0] = signals;
}
}
}
sum += valueFromOutputs(decodedDigits, entries[i].outputs);
}
return sum;
}
int main() {
const char *input = Helpers_readInputFile(__FILE__);
Entry entries[CAP] = {0};
int n = parse(input, entries);
Helpers_assert(PART1, Helpers_clock(),
partOne(n, entries),
26, 519);
Helpers_assert(PART2, Helpers_clock(),
partTwo(n, entries),
61229, 1027483);
return 0;
}