Skip to content

Instantly share code, notes, and snippets.

@mykdavies
Last active December 11, 2021 18:26
Show Gist options
  • Save mykdavies/b8bb56369fdb957de4ba3d9844b2c068 to your computer and use it in GitHub Desktop.
Save mykdavies/b8bb56369fdb957de4ba3d9844b2c068 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:console/console.dart';
// ignore: unused_import
import 'package:more/more.dart';
// ignore: unused_import
import 'package:collection/collection.dart';
List<int> parseLine(String l) => l.split('').map(int.parse).toList();
List<List<int>> buildGrid(List<String> lines) {
return lines.map((l) => parseLine(l)).toList();
}
near(r, c) => <List<int>>[
[r - 1, c],
[r + 1, c],
[r, c - 1],
[r, c + 1],
[r - 1, c - 1],
[r - 1, c + 1],
[r + 1, c - 1],
[r + 1, c + 1],
];
set(int row, int col, String chars, int color) {
Console.moveCursor(row: row, column: col);
Console.setTextColor(color);
Console.write(chars);
}
int step(List<List<int>> grid, {animate = true}) {
var s = <List<int>>[];
var flashes = 0;
var flashed = <List<int>>{};
for (int r in 0.to(grid.length)) {
for (int c in 0.to(grid.first.length)) {
grid[r][c] = grid[r][c] + 1;
if (grid[r][c] > 9) s.add([r, c]);
}
}
printG(grid);
while (s.isNotEmpty) {
var t = s.removeLast();
if (flashed.contains(t)) continue;
var r = t.first, c = t.last;
var v = grid[r][c];
if (v > 9) {
flashes += 1;
flashed.add([r, c]);
set(r + 1, c + 1, String.fromCharCode(base[0]), Color.WHITE.id);
v = 0;
for (var n in near(r, c)) {
if (n.first >= 0 &&
n.first < grid.length &&
n.last >= 0 &&
n.last < grid.first.length) {
grid[n.first][n.last] = grid[n.first][n.last] + 1;
if (animate) {
var g = grid[n.first][n.last];
set(n.first + 1, n.last + 1,
String.fromCharCode(base[g > 9 ? 0 : g]), Color.YELLOW.id);
}
if (grid[n.first][n.last] > 9) s.add(n);
}
}
}
grid[r][c] = v;
if (animate) {
sleep(Duration(milliseconds: 5));
}
}
for (var f in flashed) {
grid[f.first][f.last] = 0;
}
if (animate) {
sleep(Duration(milliseconds: 100));
}
printG(grid);
return flashes;
}
printG(List<List<int>> grid) {
Console.moveCursor(row: 1, column: 1);
Console.setTextColor(Color.GRAY.id, bright: true);
for (int r in 0.to(grid.length)) {
for (int c in 0.to(grid.first.length)) {
Console.moveCursor(row: r + 1, column: c + 1);
var e = grid[r][c];
Console.setTextColor(
(e > 9 ? 0 : e) == 0 ? Color.WHITE.id : Color.GRAY.id,
bright: true);
print(String.fromCharCode(base[e > 9 ? 0 : e]));
}
}
}
// Unicode chars for partially filled blocks.
var base = [9608, 32, 32, 9601, 9602, 9603, 9604, 9605, 9606, 9607];
main() {
var lines = File('data/data11_test2.txt').readAsLinesSync();
var g = buildGrid(lines);
Console.init();
Console.eraseDisplay(1);
Console.setTextColor(Color.WHITE.id, bright: true);
sleep(Duration(seconds: 1));
Console.moveCursor(row: 1, column: 1);
//Console.hideCursor();
var i = 0;
var f = 0;
for (i in 1.to(101)) {
f += step(g);
Console.moveCursor(row: 12, column: 1);
Console.setTextColor(Color.YELLOW.id);
print('generation: $i');
print('flashes: $f');
}
Console.moveCursor(row: 15, column: 1);
Console.setTextColor(Color.YELLOW.id);
print('Part 1 = $f');
Console.moveCursor(row: 1, column: 1);
var s = g.first.length * g.length;
for (i in 101.to(1000000)) {
var t = step(g, animate: false);
f += t;
Console.moveCursor(row: 12, column: 1);
Console.setTextColor(Color.YELLOW.id);
print('generation: $i');
print('flashes: $f');
if (t == s) break;
sleep(Duration(milliseconds: 100));
}
Console.moveCursor(row: 16, column: 1);
Console.setTextColor(Color.YELLOW.id);
print('Part 2 = $i');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment