I kind of got it to work in the end. With a fudge I got the right answers to the puzzles but it doesn't actually pass all the test cases. Anyway, here goes...

Day 15

Goblins fighting elves. We're getting into serious business now. This puzzle seems quite involved, with a bunch of searching and sorting and a shortest-path finding algorithm. Let's start as usual by building a model and parsing the input.

Model

There's a lot in common with some of the earlier problems. I'm going to try a slightly different approach from Day 13 and model the open positions (i.e. the inverse of the walls) as a set of positions, as that's a useful starting point for the Dijkstra shortest path algorithm.

data classPos(valx:Int,valy:Int)enumclassDir{UP,LEFT,RIGHT,DOWN}enumclassCreatureType{ELF,GOBLIN}data classCreature(valid:Int,valtype:CreatureType,valpos:Pos,valhitPoints:Int=200,valattackPower:Int=3)data classCave(valsize:Size,valopenPositions:Set<Pos>=setOf(),valcreatures:Map<ID,Creature>=mapOf())

Parsing

Simply a matter of turning the 2D array of characters into a model of the things found.

funSize.positions():Sequence<Pos>=(0..height-1).asSequence().flatMap{y->(0..width-1).asSequence().map{x->Pos(x,y)}}funparse(input:String,elfAttackPower:Int=3):Cave{valrows:List<CharArray>=input.trim().lines().map{s->s.trim().toCharArray()}valsize=Size(rows.size,rows.map{it.size}.max()!!)val(walls,creatures)=size.positions().fold(Pair(listOf<Pos>(),listOf<Creature>())){(w,c),pos->when(rows[pos.y][pos.x]){'.'->Pair(w,c)'#'->Pair(w+pos,c)'E'->Pair(w,c+Creature(c.size,CreatureType.ELF,pos,attackPower=elfAttackPower))'G'->Pair(w,c+Creature(c.size,CreatureType.GOBLIN,pos,attackPower=3))else->throwIllegalArgumentException("unexpected '${rows[pos.y][pos.x]}' at $pos")}}returnCave(size,(size.positions()-walls).toSet(),creatures.associateBy{it.id})}

Simulation

Let's make use of some nice Kotlin features. Ordering:

We'll model the timeline not as an infinite sequence but one that runs until the simulation is stable. Note that this inside the sequence lambda refers to the SequenceScope so we have to qualify it with the label of the outer scope, which is the function name. This is a nice feature of Kotlin and a massive improvement in readability over the outer-class this from Java:

Let's start with a basic structure for turns then flesh it out from there. This is really similar to the minecart-crashing scenario from two days ago. It was not clear in today's puzzle description whether actions should all apply at the end of a round, or as they happen. So I wrote the code in a way that each creature returns an Action from its turn, and I could apply them in different ways to experiment.

There's still an issue here as while I got the right answers in the end there's a cosmological constant fudge in my solution (well, a subtraction of one) and it only gives the correct result for some of the example inputs.

The meat comes in each creature's turn. If it can't attack it moves. After it has moved if it can attack it does so. The orderings defined above come in useful for selecting the target to attack and the best target to move towards.

Moving is an application of Dijkstra's shortest path algorithm, uses in the moveTowards() inner function. For the product of valid start positions (which are neighbours as a creature can only move one step) and potential targets we calculate the shortest path. Then take the shortest of those, if present, and move to the associated start position.

Dijkstra's algorithm is straightforward. At this point I guess I should admit I once worked at TomTom and this stuff was bread and butter for me in those days.

Start with a set of unvisited nodes, and a current node at the start node with a distance of 0

While there are unvisited nodes:

Go to all unvisited unblocked neighbours of the current node and update the distance to (current+1) if that is less than that node's current value

Remove the current node from the unvisited set

Make the unvisited node with the shortest distance the current node

At the end, if there is a distance recorded on the end node, that is the shortest distance from start to end

I have been working only on this problem for the past 2 days. I must have already spent 10 hours or so. I have a working solution for all the examples but not for the real solution.

Long story short, I had decided to use a greedy algorithm and after I saw your reply I will now try with Dijkstra's algorithm.

## re: AoC Day 15: Beverage Bandits VIEW POST

TOP OF THREAD FULL DISCUSSIONI kind of got it to work in the end. With a fudge I got the right answers to the puzzles but it doesn't actually pass all the test cases. Anyway, here goes...

## Day 15

Goblins fighting elves. We're getting into serious business now. This puzzle seems quite involved, with a bunch of searching and sorting and a shortest-path finding algorithm. Let's start as usual by building a model and parsing the input.

## Model

There's a lot in common with some of the earlier problems. I'm going to try a slightly different approach from Day 13 and model the open positions (i.e. the inverse of the walls) as a set of positions, as that's a useful starting point for the Dijkstra shortest path algorithm.

## Parsing

Simply a matter of turning the 2D array of characters into a model of the things found.

## Simulation

Let's make use of some nice Kotlin features. Ordering:

We'll model the timeline not as an infinite sequence but one that runs until the simulation is stable. Note that

`this`

inside the`sequence`

lambda refers to the`SequenceScope`

so we have to qualify it with the label of the outer scope, which is the function name. This is a nice feature of Kotlin and a massive improvement in readability over the outer-class`this`

from Java:Let's start with a basic structure for turns then flesh it out from there. This is really similar to the minecart-crashing scenario from two days ago. It was not clear in today's puzzle description whether actions should all apply at the end of a round, or as they happen. So I wrote the code in a way that each creature returns an

`Action`

from its turn, and I could apply them in different ways to experiment.There's still an issue here as while I got the right answers in the end there's a cosmological constant fudge in my solution (well, a subtraction of one) and it only gives the correct result for some of the example inputs.

The update is tricky as we need to deal with creatures that have possibly died, and also remove creatures which die during an attack.

The meat comes in each creature's turn. If it can't attack it moves. After it has moved if it can attack it does so. The orderings defined above come in useful for selecting the target to attack and the best target to move towards.

Moving is an application of Dijkstra's shortest path algorithm, uses in the

`moveTowards()`

inner function. For the product of valid start positions (which are neighbours as a creature can only move one step) and potential targets we calculate the shortest path. Then take the shortest of those, if present, and move to the associated start position.Dijkstra's algorithm is straightforward. At this point I guess I should admit I once worked at TomTom and this stuff was bread and butter for me in those days.

Full code as ever here

I have been working only on this problem for the past 2 days. I must have already spent 10 hours or so. I have a working solution for all the examples but not for the real solution.

Long story short, I had decided to use a greedy algorithm and after I saw your reply I will now try with Dijkstra's algorithm.

Thank you so much!