mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Attempt at part two, OOM
This commit is contained in:
@@ -12,6 +12,18 @@ module "check_range" {
|
|||||||
max = local.min_max[count.index][1]
|
max = local.min_max[count.index][1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module "check_range2" {
|
||||||
|
source = "./range2"
|
||||||
|
count = length(local.min_max)
|
||||||
|
|
||||||
|
min = local.min_max[count.index][0]
|
||||||
|
max = local.min_max[count.index][1]
|
||||||
|
}
|
||||||
|
|
||||||
output "part1" {
|
output "part1" {
|
||||||
value = sum(module.check_range[*].invalid_sum)
|
value = sum(module.check_range[*].invalid_sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output "part2" {
|
||||||
|
value = sum(module.check_range2[*].invalid_sum)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
variable "half" {
|
variable "part" {
|
||||||
type = number
|
type = number
|
||||||
}
|
}
|
||||||
|
|
||||||
output "full" {
|
variable "repetitions" {
|
||||||
value = tonumber("${var.half}${var.half}")
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
repeated = [for _ in range(var.repetitions) : tostring(var.part)]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "full" {
|
||||||
|
value = join("", local.repeated)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,31 +6,41 @@ variable "max" {
|
|||||||
type = number
|
type = number
|
||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
variable "repetitions" {
|
||||||
digits = floor(length(tostring(var.max)) / 2)
|
type = number
|
||||||
maximum = substr(tostring(var.max), 0, local.digits)
|
default = 2
|
||||||
real_maximum = length(tostring(var.max)) % 2 == 0 ? tonumber(local.maximum) : pow(10, local.digits)
|
}
|
||||||
|
|
||||||
min_digits = max(floor(length(tostring(var.min)) / 2), 1)
|
locals {
|
||||||
|
digits = floor(length(tostring(var.max)) / var.repetitions)
|
||||||
|
maximum = substr(tostring(var.max), 0, local.digits)
|
||||||
|
real_maximum = length(tostring(var.max)) % var.repetitions == 0 ? tonumber(local.maximum) : pow(10, local.digits)
|
||||||
|
|
||||||
|
min_digits = max(floor(length(tostring(var.min)) / var.repetitions), 1)
|
||||||
minimum = tonumber(substr(tostring(var.min), 0, local.min_digits))
|
minimum = tonumber(substr(tostring(var.min), 0, local.min_digits))
|
||||||
|
|
||||||
count = max(local.real_maximum - local.minimum + 1, 1)
|
count = max(local.real_maximum - local.minimum + 1, 1)
|
||||||
|
|
||||||
can_work = anytrue([for n in range(length(tostring(var.min)), length(tostring(var.max)) + 1) : n % 2 == 0])
|
can_work = anytrue([for n in range(length(tostring(var.min)), length(tostring(var.max)) + 1) : n % var.repetitions == 0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// This "candidates" module ought really be a list comprehension from range, but Terraform does not
|
// This "candidates" module ought really be a list comprehension from range, but Terraform does not
|
||||||
// allow you to create ranges longer than 1024.
|
// allow you to create ranges longer than 1024.
|
||||||
module "candidates" {
|
module "candidates" {
|
||||||
source = "./item"
|
source = "./item"
|
||||||
count = local.can_work ? local.count : 0
|
count = local.can_work ? local.count : 0
|
||||||
half = count.index + local.minimum
|
part = count.index + local.minimum
|
||||||
|
repetitions = var.repetitions
|
||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
locals {
|
||||||
valid = [for n in module.candidates[*].full : n if n >= var.min && n <= var.max]
|
invalid = [for n in module.candidates[*].full : n if n >= var.min && n <= var.max]
|
||||||
}
|
}
|
||||||
|
|
||||||
output "invalid_sum" {
|
output "invalid_sum" {
|
||||||
value = length(local.valid) > 0 ? sum(local.valid) : 0
|
value = length(local.invalid) > 0 ? sum(local.invalid) : 0
|
||||||
|
}
|
||||||
|
|
||||||
|
output "invalid" {
|
||||||
|
value = toset(local.invalid)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user