Day 3: Foggy Forest

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)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • janAkali@programming.dev
    link
    fedilink
    arrow-up
    8
    ·
    edit-2
    3 days ago

    Nim

    Not a hard problem today, but I still managed to over-complicate part 2 in my head. Then it finally clicked, and I solved it basically the same way as part 1.

    Part 1 - we find all pairs of corners, consider them as the left side of the square and then transpose them to where the right side of the square should be. Then check if those points do exist. I used hashset here, because I was lazy to do bounds checks, reading original grid is probably faster?
    Part 2 - same as part 1, but second corner can also be to the left of first corner.

    Runtime: 24.5 ms 8.6 ms

    type Vec2 = tuple[x,y:int]
    
    proc checkExists(grid: seq[string], x,y: int): bool =
      x < grid[0].len and y < grid.len and grid[y][x] == '#'
    
    proc solve(input: string): AOCSolution[int, int] =
      let grid = input.splitLines()
      var points: seq[Vec2]
      for y, line in grid:
        for x, c in line:
          if c == '#': points.add (x,y)
    
      for i, (x,y) in points:
        for (tx,ty) in points.toOpenArray(i+1, points.high):
          let (dx,dy) = (tx-x, ty-y)
          if dx <= 0 and grid.checkExists(x+dy, y-dx) and grid.checkExists(tx+dy, ty-dx):
            if dx == 0:
              inc result.part1
            inc result.part2
    
    older slower version
    type Vec2 = tuple[x,y:int]
    
    proc solve(input: string): AOCSolution[int, int] =
      var list: seq[Vec2]
      for y, line in enumerate input.splitLines():
        for x, c in line:
          if c == '#':
            list.add (x,y)
    
      var points = list.toHashSet()
      for i, (x,y) in list:
        for (tx,ty) in list.toOpenArray(i+1, list.high):
          let (dx,dy) = (tx-x, ty-y)
          if dx <= 0 and (x+dy, y-dx) in points and (tx+dy, ty-dx) in points:
            if dx == 0:
              inc result.part1
            inc result.part2
    

    Full code on Codeberg: solution.nim

    • CameronDev@programming.devOPM
      link
      fedilink
      arrow-up
      4
      ·
      4 days ago

      I like this approach much better than mine, I waste so much time walking around the grid to find the next corner, instead of checking the known trees.

      • addie@feddit.uk
        link
        fedilink
        arrow-up
        3
        ·
        4 days ago

        Indeed; very nice. Saves wasting loads of time checking for ‘every possible grid and skew’ like I was doing, since it only checks possibilities, and would scale very well to truly enormous grids of trees.

          • janAkali@programming.dev
            link
            fedilink
            arrow-up
            3
            ·
            4 days ago

            hashset lookup is just brutally slow for some reason

            You’re probably doing way too many hashing checks, my version prunes a lot of incorrect pairs (where second corner is above or to the right of the first) cheaply.

            But you also don’t need hashset here at all, you can just index into the existing grid e.g. grid[y][x] == '#'.

            • CameronDev@programming.devOPM
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              4 days ago

              Priming the hash checks didnt help, but now you mention it, just indexing into the grid is very obvious…

              Edit: down to 22ms, completely dropped hashset. Still not quite happy, but getting there!