Skip to content

Instantly share code, notes, and snippets.

@freedomofkeima
Created December 8, 2021 08:29
Show Gist options
  • Save freedomofkeima/df697f017ddec276d97a8b96bb1a278c to your computer and use it in GitHub Desktop.
Save freedomofkeima/df697f017ddec276d97a8b96bb1a278c to your computer and use it in GitHub Desktop.
AoC Day 8 2021
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
string corrected_wires[] = {"abcefg", "cf", "acdeg", "acdfg", "bcdf", "abdfg", "abdefg", "acf", "abcdefg", "abcdfg"};
vector<string> build_wires(int config[]) {
vector<string> temp_wires;
for (int i = 0; i < 10; i++) {
string temp;
string current_wire = corrected_wires[i];
for (int j = 0; j < current_wire.length(); j++)
temp += ('a' + config[current_wire[j] - 'a']);
sort(temp.begin(), temp.end());
temp_wires.push_back(temp);
}
return temp_wires;
}
bool valid(int config[], string wires[]) {
vector<string> broken_wires = build_wires(config);
for (int i = 0; i < 10; i++) {
bool exist = false;
for (int j = 0; j < 10; j++) {
if (wires[i] == broken_wires[j]) exist = true;
}
if (!exist) return false;
}
return true;
}
int main() {
int N = 200, total_small = 0, total_big = 0;
for (int i = 0; i < N; i++) {
string wires[10], delimiter, digits[4];
for (int j = 0; j < 10; j++) {
cin >> wires[j];
sort(wires[j].begin(), wires[j].end());
}
cin >> delimiter;
for (int j = 0; j < 4; j++) {
cin >> digits[j];
sort(digits[j].begin(), digits[j].end());
if (digits[j].length() <= 4 || digits[j].length() == 7)
total_small++;
}
// Check the correct mapping between each character
int checker[] = {0,1,2,3,4,5,6};
do {
bool stop = valid(checker, wires);
if (stop) {
// Build a correct mapping based on checker
vector<string> modified_wires = build_wires(checker);
int temp = 0;
for (int j = 0; j < 4; j++) {
temp *= 10;
for (int k = 0; k < 10; k++) {
if (digits[j] == modified_wires[k]) temp += k;
}
}
total_big += temp;
break;
}
} while(next_permutation(checker, checker+7));
}
cout << total_small << " " << total_big << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment