Created
November 25, 2023 11:06
-
-
Save mykdavies/004680836fce05e69a8be7e05b68cf3c to your computer and use it in GitHub Desktop.
AOC2022 day25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AOC 2022, Day 25: Full of Hot Air | |
// Convert numbers to "balanced base"(q.v.) 5 and back | |
// https://dartpad.dev/?id=004680836fce05e69a8be7e05b68cf3c | |
import 'package:collection/collection.dart'; | |
var si = {'2': 2, '1': 1, '0': 0, '-': -1, '=': -2}; | |
var i2s = ['0', '1', '2', '=', '-']; | |
int sToI(String n) => n.split('').fold(0, (int s, String t) => s * 5 + si[t]!); | |
String iToS(int i) => [ | |
for (var j = i; j > 0; j = (j ~/ 5) + ((j % 5 >= 3) ? 1 : 0)) i2s[j % 5] | |
].reversed.join(); | |
part1(List<String> lines) => iToS(lines.map(sToI).sum); | |
const testdata = [ | |
"1=-0-2", | |
"12111", | |
"2=0=", | |
"21", | |
"2=01", | |
"111", | |
"20012", | |
"112", | |
"1=-1=", | |
"1-12", | |
"12", | |
"1=", | |
"122" | |
]; | |
void main(List<String> args) { | |
var dt = DateTime.now(); | |
assert(part1(testdata) == "2=-1=0"); | |
var rt = DateTime.now().difference(dt).inMilliseconds; | |
print('tests succeeded in $rt ms'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment