Skip to content

Instantly share code, notes, and snippets.

@mykdavies
Created December 10, 2022 23:54
Show Gist options
  • Save mykdavies/e84b2cb4330208d07b6714cdd50827bc to your computer and use it in GitHub Desktop.
Save mykdavies/e84b2cb4330208d07b6714cdd50827bc to your computer and use it in GitHub Desktop.
Simple text parser for 4x6 characters created in AOC
/// Parse an AoC output to find the 6x4 letters encoded in it.
/// Takes a list of bools and produces the resulting string.
readLetters(List<bool> l) {
var hexes = l
// Split into 5-element chunks.
.chunked(5)
// Remove the last element of each chunk.
.map((e) => e.take(4))
// Convert to the equivalent binary, then convert that to a hex digit.
.map((e) => e.fold(0, (s, t) => s * 2 + (t ? 1 : 0)).toRadixString(16))
.toList();
// Count how many letters there must be.
var ll = hexes.length ~/ 6;
// Helper function to skip through the list taking every ll'th hex digit,
// starting at the given index and build a hex string as a key.
hex(x) => 0.to(hexes.length, step: ll).map((e) => hexes[x + e]).join('');
// Call the function for each letter start index, then join the results.
// Look it up in the table, and return '?' for unrecognised character.
return 0.to(ll).map((e) => letters[hex(e)] ?? '?').join('');
}
// Each char is 4 wide by 6 high, so can conveniently be encoded
// as a string of 6 hex digits. Some letters have not been
// seen yet.
var letters = {
'699f99': 'A',
'e9e99e': 'B',
'698896': 'C',
'e9999e': 'D',
'f8e88f': 'E',
'f8e888': 'F',
'698b97': 'G',
'99f999': 'H',
'e4444e': 'I',
'311196': 'J',
'9acaa9': 'K',
'88888f': 'L',
'9ff999': 'M',
'9db999': 'N',
'699996': 'O',
'e99e88': 'P',
'6999a5': 'Q',
'e99ea9': 'R',
'78861e': 'S',
'f22222': 'T', //speculative!
'999996': 'U',
// no V
// no W
// no X
'885222': 'Y', //right arm actually drifts into 5th column!
'f1248f': 'Z',
'000000': ' ',
};
var textDisplay = '''
.##..###...##..###..####.####..##..#..#.###....##.#..#.#....#..#.#..#..##..###...##..###...###.######..#................#...#####.
#..#.#..#.#..#.#..#.#....#....#..#.#..#..#......#.#.#..#....####.##.#.#..#.#..#.#..#.#..#.#......#..#..#................#...#...#.
#..#.###..#....#..#.###..###..#....####..#......#.##...#....####.#.##.#..#.#..#.#..#.#..#.#......#..#..#.................#.#...#..
####.#..#.#....#..#.#....#....#.##.#..#..#......#.#.#..#....#..#.#..#.#..#.###..#..#.###...##....#..#..#..................#...#...
#..#.#..#.#..#.#..#.#....#....#..#.#..#..#...#..#.#.#..#....#..#.#..#.#..#.#....#.#..#.#.....#...#..#..#..................#..#....
#..#.###...##..###..####.#.....###.#..#.###...##..#..#.####.#..#.#..#..##..#.....#.#.#..#.###....#...##...................#..####.''';
var textAnswer = 'ABCDEFGHIJKLMNOPQRSTU YZ';
void main(List<String> args) {
var input =
textDisplay.replaceAll('\n', '').split('').map((e) => e == '#').toList();
print(readLetters(input));
print(textAnswer);
if (readLetters(input) != textAnswer) print('Ooops!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment