Advent of Code 2018 - clj's Nim Solutions

Day 15: Beverage Bandits

Day 15 is about simulating a battle between the hot chocolate making Elves and the hot chocolate wanting Goblins.

Terminal Emulation

The original code already output the state of the battle for each round, in the same way as shown in day’s problem description. It would be fairly easy to render the battle using the asciinema library from Day 13, but part II of the problem is all about changing the parameters to find the attack power required for the Elves to win. Compiling the problem to JavaScript would make it possible to let the play with this parameter.

So instead of recording the terminal output, why not make a small HTML5 canvas based terminal renderer?

Input

3

Pitfalls

While optimising the code I ran into some very strange behaviour that was hard to pin down. It turns out I should have tried the development snapshot of the Nim compiler, which has fixed the code generation problem that caused this reduced test case to behave differently when compiled through the C code generator and the JS code gen:

type
  Point = tuple[x, y: int]

var targets = newSeq[tuple[d: int, p: Point]]()
for point in [(x: 1, y: 1), (x: 1, y: 2)]:
  echo($point)
  #let point = point
  targets.add((0, point))
echo($targets)
C:
(x: 1, y: 1)
(x: 1, y: 2)
@[(d: 0, p: (x: 1, y: 1)), (d: 0, p: (x: 1, y: 2))]
JS:
(x: 1, y: 1)
(x: 1, y: 2)
@[(d: 0, p: (x: 1, y: 2)), (d: 0, p: (x: 1, y: 2))]

Notice how the resulting arrays are different, which is because the loop variable point is an array containing an object representing a Point, and this object is reused on each iteration. With the Nim 0.19.4 compiler however, it is this object, and not a copy, that is placed in the results array. So at the end of the loop, all the Points in the target array will have the same value.

Is it the Same?

The optimised code gives the correct answer for all of the test input as well as the actual puzzle input. It is not quite the same algorithm as the original code though, as can be seen below:

original:
################################
#######...######################
########.....###################
##########....############.....#
###########...#####..#####.....#
###########...###..............#
##########..#####....#######..##
###########.....#...############
#####.#####..........####....###
####.....###.........##.#....###
####.#.............G.......#####
####......#.....G......G....####
##....#.......#####.....G..#####
########.....#######..G....#####
########....#########..G..######
########....#########.G.G.######
#######.....#########GEG..######
#######.....#########.G..#######
#######...#.#########..G.#######
####.........#######.G.#.#######
##...#........#####....#.#######
###..#...##..G.............###.#
######.......G...........#.....#
#######.....G...........########
#.###...#######........#########
#..##.######..#.#.....##########
#..#....##......##.....#########
#.......###.#..##......#########
#....#######...........#########
#.##########..........##########
#############.###.......########
################################
new:
################################
#######...######################
########.....###################
##########....############.....#
###########...#####..#####.....#
###########...###..............#
##########..#####....#######..##
###########.....#...############
#####.#####..........####....###
####.....###.........##.#....###
####.#..........G......G...#####
####......#..G........G.....####
##....#.......#####.....G..#####
########.....#######...G...#####
########....#########...G.######
########....#########.G...######
#######.....#########GEG..######
#######.....#########.GG.#######
#######...#.#########....#######
####.........#######.G.#.#######
##...#........#####....#.#######
###..#...##..G.............###.#
######.......G...........#.....#
#######.....G...........########
#.###...#######........#########
#..##.######..#.#.....##########
#..#....##......##.....#########
#.......###.#..##......#########
#....#######...........#########
#.##########..........##########
#############.###.......########
################################

I could spend the time checking which of the programs generating the above output (if any) is correct… but lets not.