Skip Navigation

šŸŖ - 2023 DAY 14 SOLUTIONS -šŸŖ

Day 14: Parabolic Reflector Dish

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


šŸ”’ Thread is locked until there's at least 100 2 star entries on the global leaderboard

Edit: šŸ”“ Unlocked

17

You're viewing a single thread.

17 comments
  • Dart

    Big lump of code. I built a general slide function which ended up being tricksy in order to visit rocks in the correct order, but it works.

    int hash(List> rocks) =>
        (rocks.map((e) => e.join('')).join('\n')).hashCode;
    
    /// Slide rocks in the given (vert, horz) direction.
    List> slide(List> rocks, (int, int) dir) {
      // Work out in which order to check rocks for most efficient movement.
      var rrange = 0.to(rocks.length);
      var crange = 0.to(rocks.first.length);
      var starts = [
        for (var r in (dir.$1 == 1) ? rrange.reversed : rrange)
          for (var c in ((dir.$2 == 1) ? crange.reversed : crange)
              .where((c) => rocks[r][c] == 'O'))
            (r, c)
      ];
    
      for (var (r, c) in starts) {
        var dest = (r, c);
        var next = (dest.$1 + dir.$1, dest.$2 + dir.$2);
        while (next.$1.between(0, rocks.length - 1) &&
            next.$2.between(0, rocks.first.length - 1) &&
            rocks[next.$1][next.$2] == '.') {
          dest = next;
          next = (dest.$1 + dir.$1, dest.$2 + dir.$2);
        }
        if (dest != (r, c)) {
          rocks[r][c] = '.';
          rocks[dest.$1][dest.$2] = 'O';
        }
      }
      return rocks;
    }
    
    List> oneCycle(List> rocks) =>
        [(-1, 0), (0, -1), (1, 0), (0, 1)].fold(rocks, (s, t) => slide(s, t));
    
    spin(List> rocks, {int target = 1}) {
      var cycle = 1;
      var seen = {};
      while (cycle != target) {
        rocks = oneCycle(rocks);
        var h = hash(rocks);
        if (seen.containsKey(h)) {
          var diff = cycle - seen[h]!;
          var count = (target - cycle) ~/ diff;
          cycle += count * diff;
          seen = {};
        } else {
          seen[h] = cycle;
          cycle += 1;
        }
      }
      return weighting(rocks);
    }
    
    parse(List lines) => lines.map((e) => e.split('').toList()).toList();
    
    weighting(List> rocks) => 0
        .to(rocks.length)
        .map((r) => rocks[r].count((e) => e == 'O') * (rocks.length - r))
        .sum;
    
    part1(List lines) => weighting(slide(parse(lines), (-1, 0)));
    
    part2(List lines) => spin(parse(lines), target: 1000000000);
    
17 comments