Quest 1: Whispers in the Shell

  • 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

Link to participate: https://everybody.codes/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 4 points 1 year ago

So far I really like the difficulty level of EC, compared to AOC.
Quest 1 is pretty straightforward:

  • part 1: add offsets to index then clamp it between 0..limit e.g. min(limit, max(0, index))
  • part 2: add offsets and then modulo to figure out last index
  • part 3: swap first element with element at index mod names.len until done =)

My solution in Nim:

proc parseInput(input: string): tuple[names: seq[string], values: seq[int]] =
  let lines = input.splitLines()
  result.names = lines[0].split(',')

  let values = lines[2].multiReplace({"R":"","L":"-"})
  for num in values.split(','):
    result.values.add parseInt(num)

proc solve_part1*(input: string): Solution =
  let (names, values) = parseInput(input)
  var pos = 0
  for value in values:
    pos = min(names.high, max(0, pos + value))
  result := names[pos]

proc solve_part2*(input: string): Solution =
  let (names, values) = parseInput(input)
  let pos = values.sum()
  result := names[pos.euclMod(names.len)]

proc solve_part3*(input: string): Solution =
  var (names, values) = parseInput(input)
  for value in values:
    swap(names[0], names[euclMod(value, names.len)])
  result := names[0]

Full solution at Codeberg: solution.nim

  • source