mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement efficient algorithm in TF
This commit is contained in:
@@ -4,12 +4,15 @@ variable "input" {
|
||||
|
||||
locals {
|
||||
nums = [for s in split(" ", chomp(var.input)) : tonumber(s)]
|
||||
|
||||
grouped = { for num in local.nums : num => 1... }
|
||||
total = { for k, v in local.grouped : k => sum(v) }
|
||||
}
|
||||
|
||||
module "step1" {
|
||||
source = "./step"
|
||||
|
||||
prev = local.nums
|
||||
prev = local.total
|
||||
}
|
||||
|
||||
module "step2" {
|
||||
@@ -157,5 +160,5 @@ module "step25" {
|
||||
}
|
||||
|
||||
output "part1" {
|
||||
value = length(flatten(module.step25.next))
|
||||
value = sum(values(module.step25.next))
|
||||
}
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
variable "prev" {
|
||||
type = list(number)
|
||||
type = map(number)
|
||||
}
|
||||
|
||||
module "transform" {
|
||||
source = "../transform"
|
||||
for_each = var.prev
|
||||
|
||||
num = each.key
|
||||
}
|
||||
|
||||
locals {
|
||||
values = [
|
||||
for num in var.prev : num == 0 ? [1]
|
||||
: length(tostring(num)) % 2 == 0
|
||||
? [tonumber(substr(tostring(num), 0, length(tostring(num)) / 2)), tonumber(substr(tostring(num), length(tostring(num)) / 2, 10))]
|
||||
: [num * 2024]
|
||||
]
|
||||
by_value = flatten([
|
||||
for key, value in module.transform :
|
||||
[for result in value.result : { num = result, count = var.prev[key] }]
|
||||
])
|
||||
|
||||
grouped = { for kv in local.by_value : kv.num => kv.count... }
|
||||
}
|
||||
|
||||
# module "transform" {
|
||||
@@ -23,5 +30,5 @@ locals {
|
||||
# }
|
||||
|
||||
output "next" {
|
||||
value = flatten(local.values)
|
||||
value = { for num, groups in local.grouped : num => sum(groups) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user