Skip to content

Instantly share code, notes, and snippets.

@claytonflesher
Created December 2, 2021 15:37
Show Gist options
  • Save claytonflesher/9b45da4b096da0a8c2c31d20cc12bbf9 to your computer and use it in GitHub Desktop.
Save claytonflesher/9b45da4b096da0a8c2c31d20cc12bbf9 to your computer and use it in GitHub Desktop.
AOC2021Day2b
output application/json
var inputData = readUrl("classpath://inputs/day2.txt", "text/plain")
then (result) -> result splitBy "\n"
var normalizedData = inputData map
using (pair = $ splitBy " ")
{
direction: pair[0],
distance: pair[1] as Number
}
var finalLocation = normalizedData reduce ((movement, acc = {horizontal: 0, depth: 0, aim: 0}) ->
movement.direction match {
case "forward" -> {horizontal: acc.horizontal + movement.distance, depth: acc.depth + (acc.aim * movement.distance), aim: acc.aim + 0}
case "down" -> {horizontal: acc.horizontal, depth: acc.depth + movement.distance, aim: acc.aim + movement.distance}
case "up" -> {horizontal: acc.horizontal, depth: acc.depth - movement.distance, aim: acc.aim - movement.distance}
}
)
---
(finalLocation.depth - finalLocation.aim) * finalLocation.horizontal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment