Today was pretty cool, a bit similar to day 15 but...
# advent-of-code
k
Today was pretty cool, a bit similar to day 15 but without a grid. I got a lot of use out of
sortedBy
,
sumBy
,
filter
,
maxWith
,
takeIf
etc! (spoiler in thread)
Initially I didn't expect that some battles would get stuck, my code blocked and I was afraid there was a "real" bug simple smile
m
Same, the stalemate got me by surprise. 😄
s
Ooh, yes, that was a good twist!
I used a sequence to dynamically ignore "recently-dead" groups while performing attacks; I think one of @todd.ginsberg's earlier solutions tipped me off as to that lazy trick! 👏
Much more satisfying day than 23 😄
k
Can you show that sequence code?
s
Copy code
val attackOrder = pendingAttacks
    .keys
    .sortedWith(compareByDescending(Group::initiative))

attackOrder
    .asSequence()
    .filterNot { it.size == 0 }
    .forEach { attacker ->
        pendingAttacks[attacker]?.let { target ->
            attacker.attack(target).also { unitsDied = unitsDied || it }
        }
    }
Note that target is mutated by the call to
attack
But itself may also be present in
attackOrder
, and need to be skipped because all units died
k
Ah that's neat.
s
Yeah, I remember liking it when I saw it so glad to have a chance to deploy 🙂