• 0 Posts
  • 6 Comments
Joined 5 days ago
cake
Cake day: November 28th, 2026

help-circle

  • Nim

    Part 1, is pretty simple, just some modulo shenanigans.
    Part 2, I solved partially by hand - best order of waypoints with maximum distance between them is just hardcoded. Then for N-1 points of each type, distance is 6, and for the last one - it’s distance to the next waypoint (same formula as part1).

    Runtime: 27 μs

    proc toMap(s: string): array[128, int] =
      for i, c in s: result[c.ord] = i
    
    proc dist(map: openarray[int], curr, next: char): int =
      result = map[next.ord] - map[curr.ord]
      if result < 1: result += 6
    
    proc solve(input: string): AOCSolution[int, int] =
      const pattern = toMap("roygbv")
      const maxdist = toMap("rvbgyo")
    
      block p1:
        var prev = 'r'
        for c in input:
          result.part1 += pattern.dist(prev, c)
          prev = c
    
      block p2:
        let pairs = input.toCountTable().pairs.toseq.sortedByIt maxdist[ord it[0]]
        var prev = 'r'
        for (c, am) in pairs:
          if am > 1: result.part2 += 6 * (am - 1)
          result.part2 += pattern.dist(prev, c)
          prev = c
    

    Full solution at Codeberg: solution.nim