[-] janAkali@programming.dev 4 points 15 hours ago* (last edited 14 hours ago)

I am not complaining, but it's kinda peculiar that we got 2 "Counting HashMap + sorting" problems in a row :)

[-] janAkali@programming.dev 3 points 15 hours ago

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

[-] janAkali@programming.dev 6 points 1 day ago* (last edited 1 day ago)

Nim

Probably one of the easiest first days in AOC history.

Solved with HashTable -> sorting -> sequence
Runtime: 280 μs

proc solve(input: string): AOCSolution[int, int] =
  var cnt: CountTable[int]
  for line in input.splitLines():
    cnt.inc parseInt line
  cnt.sort()
  let s = cnt.keys.toseq()
  result.part1 = s[0] * s[1] * s[2]
  result.part2 = s[^1] * s[^2] * s[^3]

Full solution at Codeberg: solution.nim

[-] janAkali@programming.dev 1 points 1 day ago* (last edited 1 day ago)

Nim!
It is easy to write and read like python, but speed is matching C/Rust and it's also statically typed. Jack of all trades, sort of...

[-] janAkali@programming.dev 2 points 4 days ago

Oh, that just reminded me something. I should try this year to make some AoC visualizations in Tic-80 with my own Nim bindings. I hope there will be easier days, where I can spare more time on it.

[-] janAkali@programming.dev 2 points 4 days ago

Ah, it's that time of the year again 🎄⭐️

I'll be using Nim this year, same as previous years.
And maybe all the next years? Unless I find a language that is as good or better (it's hard :).
My solutions are not always elegant, but I try to make them short and easy to read.

Good Luck to everyone and don't forget to have fun!

janAkali

0 post score
0 comment score
joined 4 days ago