mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +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]
|
||||
}
|
||||
|
||||
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" {
|
||||
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
|
||||
}
|
||||
|
||||
output "full" {
|
||||
value = tonumber("${var.half}${var.half}")
|
||||
variable "repetitions" {
|
||||
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
|
||||
}
|
||||
|
||||
locals {
|
||||
digits = floor(length(tostring(var.max)) / 2)
|
||||
maximum = substr(tostring(var.max), 0, local.digits)
|
||||
real_maximum = length(tostring(var.max)) % 2 == 0 ? tonumber(local.maximum) : pow(10, local.digits)
|
||||
variable "repetitions" {
|
||||
type = number
|
||||
default = 2
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
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
|
||||
// allow you to create ranges longer than 1024.
|
||||
module "candidates" {
|
||||
source = "./item"
|
||||
count = local.can_work ? local.count : 0
|
||||
half = count.index + local.minimum
|
||||
source = "./item"
|
||||
count = local.can_work ? local.count : 0
|
||||
part = count.index + local.minimum
|
||||
repetitions = var.repetitions
|
||||
}
|
||||
|
||||
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" {
|
||||
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