Advertisement
Guest User

Advent of Code 2020 day 24

a guest
Dec 24th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.55 KB | None | 0 0
  1. import 'dart:math';
  2. import 'package:petitparser/petitparser.dart';
  3. import 'advent2020.dart';
  4. import 'package:advent2020/eulib.dart' as eulib;
  5.  
  6. void main() {
  7.   day24a();
  8.   day24b();
  9. }
  10.  
  11. void day24a() {
  12.   var p = buildParser();
  13.   flipped = <Point, bool>{};
  14.   var lines = readLinesFromFile('data/day24_data.txt');
  15.   for (var l in lines) {
  16.     CURR = Point(0, 0);
  17.     p.parse(l);
  18.     flipped[CURR] = !(flipped[CURR] ?? false);
  19.   }
  20.   assert(flipped.values.where((e) => e).length == 254);
  21. }
  22.  
  23. void day24b() {
  24.   var p = buildParser();
  25.   flipped = <Point, bool>{};
  26.   var lines = readLinesFromFile('data/day24_data.txt');
  27.   for (var l in lines) {
  28.     CURR = Point(0, 0);
  29.     p.parse(l);
  30.     flipped[CURR] = !(flipped[CURR] ?? false);
  31.   }
  32.   // Actual test.
  33.   for (var _ in eulib.range(0, 100)) {
  34.     flipped = life(flipped);
  35.   }
  36.   assert(flipped.values.where((e) => e).length == 3697);
  37. }
  38.  
  39. // x y = bottom left.
  40. /*
  41. Two overlapping grids
  42.  
  43.       (1,3)---|---(3,3)------
  44.         |     |     |
  45. (0,2)-------(2,2)-------(4,2)--
  46.         |     |     |
  47.       (1,1)---|---(3,1)-------
  48. */
  49.  
  50. var dirs = {
  51.   'ne': Point(1, 1),
  52.   'se': Point(1, -1),
  53.   'sw': Point(-1, -1),
  54.   'nw': Point(-1, 1),
  55.   'e': Point(2, 0),
  56.   'w': Point(-2, 0),
  57. };
  58. Point CURR;
  59. void move(String dir) => CURR += dirs[dir];
  60.  
  61. Parser buildParser() {
  62.   var ne = (char('n') & char('e')).map((arr) => move('ne'));
  63.   var se = (char('s') & char('e')).map((arr) => move('se'));
  64.   var sw = (char('s') & char('w')).map((arr) => move('sw'));
  65.   var nw = (char('n') & char('w')).map((arr) => move('nw'));
  66.   var e = (char('e')).map((arr) => move('e'));
  67.   var w = (char('w')).map((arr) => move('w'));
  68.   var exp = (ne | se | sw | nw | e | w).plus();
  69.  
  70.   var b = ExpressionBuilder();
  71.   b.group().primitive(exp);
  72.   var rp = b.build().end();
  73.   return rp;
  74. }
  75.  
  76. Map<Point, bool> flipped;
  77.  
  78. Map<Point, bool> life(Map<Point, bool> current) {
  79.   // Expand borders to include all needed whites.
  80.   for (var p in current.keys.toList()) {
  81.     for (var d in dirs.values) {
  82.       current.putIfAbsent(p + d, () => false);
  83.     }
  84.   }
  85.   // Copy current state into future state.
  86.   var next = Map<Point, bool>.fromEntries(current.entries);
  87.   for (var p in current.keys) {
  88.     // Count flipped (=black) neighbours
  89.     var n = dirs.values.where((e) => current[p + e] ?? false).length;
  90.     // Are we on a black tile?
  91.     if (current[p]) {
  92.       if (n == 0 || n > 2) {
  93.         next[p] = false;
  94.       }
  95.       // White.
  96.     } else {
  97.       if (n == 2) {
  98.         next[p] = true;
  99.       }
  100.     }
  101.   }
  102.   return next;
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement