diff --git a/2024/bonus/day03/main.tf b/2024/bonus/day03/main.tf index 35962f9..180df28 100644 --- a/2024/bonus/day03/main.tf +++ b/2024/bonus/day03/main.tf @@ -4,8 +4,21 @@ variable "input" { locals { muls = regexall("mul\\((\\d+),(\\d+)\\)", var.input) + ops = regexall("(don't\\(\\)|do\\(\\)|mul\\((\\d+),(\\d+)\\))", var.input) +} + +module "should_execute" { + count = length(local.ops) + source = "./should_execute" + + index = count.index + ops = local.ops } output "part1" { value = sum([for mul in local.muls : parseint(mul[1], 10) * parseint(mul[0], 10)]) } + +output "part2" { + value = sum(module.should_execute[*].value) +} diff --git a/2024/bonus/day03/should_execute/main.tf b/2024/bonus/day03/should_execute/main.tf new file mode 100644 index 0000000..fb595bf --- /dev/null +++ b/2024/bonus/day03/should_execute/main.tf @@ -0,0 +1,19 @@ +variable "ops" { + type = list(list(string)) +} + +variable "index" { + type = number +} + +locals { + is_mul = startswith(var.ops[var.index][0], "mul") + subslice = reverse(slice(var.ops[*][0], 0, var.index)) + + do_pos = contains(local.subslice, "do()") ? index(local.subslice, "do()") : var.index + dont_pos = contains(local.subslice, "don't()") ? index(local.subslice, "don't()") : var.index + 1 +} + +output "value" { + value = (local.is_mul && local.do_pos < local.dont_pos) ? (parseint(var.ops[var.index][1], 10) * parseint(var.ops[var.index][2], 10)) : 0 +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index a2766bc..89085e6 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -36,3 +36,7 @@ module "day03" { output "day03_1" { value = module.day03.part1 } + +output "day03_2" { + value = module.day03.part2 +} diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index 09dc5c4..365f58b 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -57,3 +57,20 @@ run "day3_1" { error_message = "Part1 output is wrong" } } + +run "day3_2" { + command = plan + + module { + source = "./day03" + } + + variables { + input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" + } + + assert { + condition = output.part2 == 48 + error_message = "Part2 output is wrong" + } +}