Skip to content

Instantly share code, notes, and snippets.

@msg555
Created December 4, 2023 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msg555/3c7e24738f7ae15aa2af9608444fa3e5 to your computer and use it in GitHub Desktop.
Save msg555/3c7e24738f7ae15aa2af9608444fa3e5 to your computer and use it in GitHub Desktop.
AdventAsCode Day 2
data "local_file" "input" {
filename = "${path.module}/input.txt"
}
locals {
games = [
for line in split("\n", chomp(data.local_file.input.content)) :
{
game = tonumber(substr(split(":", line)[0], 5, -1))
observations = [
for observations in split(";", split(":", line)[1]) :
{
for observation in split(",", observations) :
split(" ", trim(observation, " "))[1] => tonumber(split(" ", trim(observation, " "))[0])
}
]
}
]
max_amount = {
red = 12
green = 13
blue = 14
}
power_colors = ["red", "blue", "green"]
}
output "part1" {
value = sum([
for game in local.games :
game.game
if alltrue([
for observation in game.observations :
alltrue([
for color, amt in observation :
amt <= lookup(local.max_amount, color, 0)
])
])
])
}
output "part2" {
value = sum([
for game in local.games :
pow(
2,
sum([
for color in local.power_colors :
log(max([
for observation in game.observations :
lookup(observation, color, 1e-9)
]...), 2)
])
)
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment