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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard
🔓 Unlocked after 40 mins
You must log in or register to comment.
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; }
Scala3
forgot to post this
import Area.* import Dir.* enum Dir(num: Int, diff: (Int, Int)): val n = num val d = diff case Up extends Dir(3, (0, -1)) case Down extends Dir(1, (0, 1)) case Left extends Dir(2, (-1, 0)) case Right extends Dir(0, (1, 0)) def opposite = Dir.from(n + 2) object Dir: def from(n: Int): Dir = Dir.all.filter(_.n == n % 4).ensuring(_.size == 1).head def all = List(Up, Down, Left, Right) enum Area: case Inside, Outside, Loop case class Pos(x: Int, y: Int) type Landscape = Map[Pos, Pipe] type Loop = Map[Pos, LoopPiece] def walk(p: Pos, d: Dir): Pos = Pos(p.x + d.d._1, p.y + d.d._2) val pipeMap = Map('|' -> List(Up, Down), '-' -> List(Left, Right), 'L' -> List(Up, Right), 'J' -> List(Up, Left), 'F' -> List(Right, Down), '7' -> List(Left, Down)) case class Pipe(neighbors: List[Dir]) case class LoopPiece(from: Dir, to: Dir): def left: List[Dir] = ((from.n + 1) until (if to.n < from.n then to.n + 4 else to.n)).map(Dir.from).toList def right: List[Dir] = LoopPiece(to, from).left def parse(a: List[String]): (Pos, Landscape) = val pipes = for (r, y) <- a.zipWithIndex; (v, x) <- r.zipWithIndex; p <- pipeMap.get(v) yield Pos(x, y) -> Pipe(p) val start = for (r, y) <- a.zipWithIndex; (v, x) <- r.zipWithIndex if v == 'S' yield Pos(x, y) (start.head, pipes.toMap) def walkLoop(start: Pos, l: Landscape): Loop = @tailrec def go(pos: Pos, last_dir: Dir, acc: Loop): Loop = if pos == start then acc else val dir = l(pos).neighbors.filter(_ != last_dir.opposite).ensuring(_.size == 1).head go(walk(pos, dir), dir, acc + (pos -> LoopPiece(last_dir.opposite, dir))) Dir.all.filter(d => l.get(walk(start, d)).exists(p => p.neighbors.contains(d.opposite))) match case List(start_dir, return_dir) => go(walk(start, start_dir), start_dir, Map(start -> LoopPiece(return_dir, start_dir))) case _ => Map() def task1(a: List[String]): Long = walkLoop.tupled(parse(a)).size.ensuring(_ % 2 == 0) / 2 def task2(a: List[String]): Long = val loop = walkLoop.tupled(parse(a)) val ys = a.indices val xs = a.head.indices val points = (for x <- xs; y <- ys yield Pos(x, y)).toSet // floodfill @tailrec def go(outside: Set[Pos], q: List[Pos]): Set[Pos] = if q.isEmpty then outside else val nbs = Dir.all.map(walk(q.head, _)).filter(points.contains(_)).filter(!outside.contains(_)) go(outside ++ nbs, nbs ++ q.tail) // start by floodfilling from the known outside: beyond the array bounds val boundary = ys.flatMap(y => List(Pos(-1, y), Pos(xs.end, y))) ++ xs.flatMap(x => List(Pos(x, -1), Pos(x, ys.end))) val r = go(boundary.toSet ++ loop.keySet, boundary.toList) // check on which side of the pipe the outside is, then continue floodfill from there val xsl = List[LoopPiece => List[Dir]](_.left, _.right).map(side => loop.flatMap((p, l) => side(l).map(d => walk(p, d))).filter(!loop.contains(_)).toSet).map(a => a -> a.intersect(r).size).ensuring(_.exists(_._2 == 0)).filter(_._2 != 0).head._1 (points -- go(r ++ xsl, xsl.toList)).size
deleted by creator