this post was submitted on 10 Dec 2023
24 points (96.2% liked)

Advent Of Code

770 readers
75 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] mykl@lemmy.world 3 points 11 months ago* (last edited 11 months 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;
}