Advertisement
mykdavies

AOC 2022 Day 8 - terse version

Dec 8th, 2022 (edited)
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.44 KB | None | 0 0
  1. /// Given a grid of (real) trees of different heights
  2. /// 1) Find how many are visible from any edge
  3. /// 2) Find the best view from any trees (= product of distances)
  4.  
  5. // ignore_for_file: unused_import
  6. import 'dart:io';
  7.  
  8. import 'package:collection/collection.dart';
  9. import 'package:more/more.dart';
  10.  
  11. // Add an offset of 1 to every tree so that 0-height tree work for this hack.
  12. List<List<int>> readTrees(List<String> lines) => lines
  13.     .map((e) => e.split('').map((e) => int.parse(e) + 1).toList())
  14.     .toList();
  15.  
  16. part1(List<String> lines) {
  17.   var trees = readTrees(lines);
  18.  
  19.   // helper function
  20.   int checkHeight(row, col, targetHeight) {
  21.     if (trees[row][col].abs() > targetHeight) {
  22.       targetHeight = trees[row][col].abs();
  23.       trees[row][col] = -targetHeight; // <-- means visible!
  24.     }
  25.     return targetHeight;
  26.   }
  27.  
  28.   var nCols = trees.first.length, nRows = trees.length;
  29.   for (var row in 0.to(nRows)) {
  30.     0.to(nCols).fold(-99, (s, t) => checkHeight(row, t, s));
  31.     0.to(nCols).reversed.fold(-99, (s, t) => checkHeight(row, t, s));
  32.   }
  33.   for (var col in 0.to(nCols)) {
  34.     0.to(nRows).fold(-99, (s, t) => checkHeight(t, col, s));
  35.     0.to(nRows).reversed.fold(-99, (s, t) => checkHeight(t, col, s));
  36.   }
  37.   return trees.flattened.where((e) => e < 1).length;
  38. }
  39.  
  40. // Part 2
  41.  
  42. var dirs = [
  43.   [0, 1],
  44.   [0, -1],
  45.   [1, 0],
  46.   [-1, 0]
  47. ];
  48.  
  49. List<int> viewsFromTree(List<List<int>> trees, List<int> rc) {
  50.   var row = rc[0], col = rc[1];
  51.   inBounds(r, c) =>
  52.       r >= 0 && r < trees.length && c >= 0 && c < trees.first.length;
  53.   var high = trees[row][col];
  54.   var vals = <int>[];
  55.   for (var dir in dirs) {
  56.     var hr = row, hc = col;
  57.     var val = 0;
  58.     while (true) {
  59.       hr += dir[0];
  60.       hc += dir[1];
  61.       if (!inBounds(hr, hc)) break;
  62.       val += 1;
  63.       if (trees[hr][hc] >= high) break;
  64.     }
  65.     vals.add(val);
  66.   }
  67.   return vals;
  68. }
  69.  
  70. part2(List<String> lines) {
  71.   var trees = readTrees(lines);
  72.   var rowColPairs = [
  73.     for (var row in 0.to(trees.length))
  74.       for (var col in 0.to(trees.first.length)) [row, col]
  75.   ];
  76.   return rowColPairs.fold(
  77.       -1,
  78.       (sofar, rc) =>
  79.           [sofar, viewsFromTree(trees, rc).reduce((s, t) => s * t)].max);
  80. }
  81.  
  82. void main(List<String> args) {
  83.   var lines = File('data/data08_01.txt').readAsLinesSync();
  84.   var t = DateTime.now();
  85.   print(part1(lines));
  86.   print(part2(lines));
  87.   print('${DateTime.now().difference(t).inMicroseconds} microseconds');
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement