Day 10: Pipe Maze

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

🔓 Unlocked after 40 mins

  • @mykl
    link
    3
    edit-2
    1 year ago

    Dart

    Finally got round to solving part 2. Very easy once I realised it’s just a matter of counting line crossings.

    Edit: having now read the other comments here, I’m reminded that the line-crossing logic is actually an application of Jordan’s Curve Theorem which looks like a mathematical joke when you first see it, but turns out to be really useful here!

    var up = Point(0, -1),
        down = Point(0, 1),
        left = Point(-1, 0),
        right = Point(1, 0);
    var pipes = >>{
      '|': [up, down],
      '-': [left, right],
      'L': [up, right],
      'J': [up, left],
      '7': [left, down],
      'F': [right, down],
    };
    late List> grid; // Make grid global for part 2
    Set> buildPath(List lines) {
      grid = lines.map((e) => e.split('')).toList();
      var points = {
        for (var row in grid.indices())
          for (var col in grid.first.indices()) Point(col, row): grid[row][col]
      };
      // Find the starting point.
      var pos = points.entries.firstWhere((e) => e.value == 'S').key;
      var path = {pos};
      // Replace 'S' with assumed pipe.
      var dirs = [up, down, left, right].where((el) =>
          points.keys.contains(pos + el) &&
          pipes.containsKey(points[pos + el]) &&
          pipes[points[pos + el]]!.contains(Point(-el.x, -el.y)));
      grid[pos.y][pos.x] = pipes.entries
          .firstWhere((e) =>
              (e.value.first == dirs.first) && (e.value.last == dirs.last) ||
              (e.value.first == dirs.last) && (e.value.last == dirs.first))
          .key;
    
      // Follow the path.
      while (true) {
        var nd = dirs.firstWhereOrNull((e) =>
            points.containsKey(pos + e) &&
            !path.contains(pos + e) &&
            (points[pos + e] == 'S' || pipes.containsKey(points[pos + e])));
        if (nd == null) break;
        pos += nd;
        path.add(pos);
        dirs = pipes[points[pos]]!;
      }
      return path;
    }
    
    part1(List lines) => buildPath(lines).length ~/ 2;
    part2(List lines) {
      var path = buildPath(lines);
      var count = 0;
      for (var r in grid.indices()) {
        var outside = true;
        // We're only interested in how many times we have crossed the path
        // to get to any given point, so mark anything that's not on the path
        // as '*' for counting, and collapse all uninteresting path segments.
        var row = grid[r]
            .indexed()
            .map((e) => path.contains(Point(e.index, r)) ? e.value : '*')
            .join('')
            .replaceAll('-', '')
            .replaceAll('FJ', '|') // zigzag
            .replaceAll('L7', '|') // other zigzag
            .replaceAll('LJ', '') // U-bend
            .replaceAll('F7', ''); // n-bend
        for (var c in row.split('')) {
          if (c == '|') {
            outside = !outside;
          } else {
            if (!outside && c == '*') count += 1;
          }
        }
      }
      return count;
    }