Inline loop

This commit is contained in:
2024-12-27 21:45:36 +01:00
parent 5c030d5272
commit dc92b65830
2 changed files with 10 additions and 29 deletions

View File

@@ -1,21 +0,0 @@
variable "update" {
type = list(number)
}
variable "disallow_rules" {
type = map(list(number))
}
locals {
not_disallowed = alltrue([
for i in range(1, length(var.update)) :
!contains(
flatten([for j in range(i) : lookup(var.disallow_rules, var.update[j], [])]),
var.update[i]
)
])
}
output "valid" {
value = local.not_disallowed ? var.update[floor(length(var.update) / 2)] : 0
}

View File

@@ -8,16 +8,18 @@ locals {
disallow_rules = { for rule in local.rules : rule[1] => rule[0]... }
updates = [for update_line in split("\n", local.parts[1]) : [for v in split(",", update_line) : tonumber(v)]]
}
module "is_valid" {
source = "./is_correct"
count = length(local.updates)
update = local.updates[count.index]
disallow_rules = local.disallow_rules
scores = [
for update in local.updates :
alltrue([
for i in range(1, length(update)) :
!contains(
flatten([for j in range(i) : lookup(local.disallow_rules, update[j], [])]),
update[i]
)
]) ? update[floor(length(update) / 2)] : 0]
}
output "part1" {
value = sum(module.is_valid[*].valid)
value = sum(local.scores[*])
}