Created
December 4, 2023 09:18
-
-
Save msg555/3c7e24738f7ae15aa2af9608444fa3e5 to your computer and use it in GitHub Desktop.
AdventAsCode Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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