mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Avoid instantiating module and backtracking, instead use regex for filtering. Much faster. Since we are not matching brackets, but rather only care about the last instance, the limitations of regex don't apply.
20 lines
521 B
HCL
20 lines
521 B
HCL
variable "input" {
|
|
type = string
|
|
}
|
|
|
|
locals {
|
|
filtered = replace(var.input, "/(?s)don't\\(\\).*?do\\(\\)/", "")
|
|
filtered2 = replace(local.filtered, "/(?s)don't\\(\\).*/", "")
|
|
|
|
muls = regexall("mul\\((\\d+),(\\d+)\\)", var.input)
|
|
filtered_muls = regexall("mul\\((\\d+),(\\d+)\\)", local.filtered2)
|
|
}
|
|
|
|
output "part1" {
|
|
value = sum([for mul in local.muls : tonumber(mul[1]) * tonumber(mul[0])])
|
|
}
|
|
|
|
output "part2" {
|
|
value = sum([for mul in local.filtered_muls : tonumber(mul[1]) * tonumber(mul[0])])
|
|
}
|