From f9416db2510069fc717f1355940b878bd911bb95 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 29 Jan 2025 00:43:14 +0100 Subject: [PATCH] Implement day 8 2024 in Terraform --- 2024/bonus/day08/freq/main.tf | 52 ++++++++++++++++++++++++++++ 2024/bonus/day08/freq/pair/main.tf | 34 +++++++++++++++++++ 2024/bonus/day08/main.tf | 54 ++++++++++++++++++++++++++++++ 2024/bonus/main.tf | 13 +++++++ 2024/bonus/tests.tftest.hcl | 23 +++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 2024/bonus/day08/freq/main.tf create mode 100644 2024/bonus/day08/freq/pair/main.tf create mode 100644 2024/bonus/day08/main.tf diff --git a/2024/bonus/day08/freq/main.tf b/2024/bonus/day08/freq/main.tf new file mode 100644 index 0000000..fc9234c --- /dev/null +++ b/2024/bonus/day08/freq/main.tf @@ -0,0 +1,52 @@ +variable "width" { + type = number +} + +variable "height" { + type = number +} + +variable "antennae" { + type = list(tuple([number, number])) +} + +locals { + pairs = concat([ + for i in range(length(var.antennae)) : + [ + for j in range(i + 1, length(var.antennae)) : [var.antennae[i], var.antennae[j]] + ] + ]...) +} + +module "pair" { + source = "./pair" + count = length(local.pairs) + + first = local.pairs[count.index][0] + second = local.pairs[count.index][1] + width = var.width + height = var.height +} + +output "nodes1" { + value = setunion([ + for i in range(length(local.pairs)) : + [ + for v in module.pair[i].nodes1 : + v + if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height + ] + ]...) +} + +output "nodes2" { + value = setunion([ + for i in range(length(local.pairs)) : + [ + for v in module.pair[i].nodes2 : + v + if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height + ] + ]...) +} diff --git a/2024/bonus/day08/freq/pair/main.tf b/2024/bonus/day08/freq/pair/main.tf new file mode 100644 index 0000000..ffbd3b4 --- /dev/null +++ b/2024/bonus/day08/freq/pair/main.tf @@ -0,0 +1,34 @@ +variable "first" { + type = tuple([number, number]) +} + +variable "second" { + type = tuple([number, number]) +} + +variable "width" { + type = number +} + +variable "height" { + type = number +} + +locals { + dx = var.second[0] - var.first[0] + dy = var.second[1] - var.first[1] +} + +output "nodes1" { + value = [ + [var.first[0] - local.dx, var.first[1] - local.dy], + [var.second[0] + local.dx, var.second[1] + local.dy], + ] +} + +output "nodes2" { + value = concat( + [for i in range(max(var.width, var.height)) : [var.first[0] - i * local.dx, var.first[1] - i * local.dy]], + [for i in range(max(var.width, var.height)) : [var.second[0] + i * local.dx, var.second[1] + i * local.dy]] + ) +} diff --git a/2024/bonus/day08/main.tf b/2024/bonus/day08/main.tf new file mode 100644 index 0000000..740e28d --- /dev/null +++ b/2024/bonus/day08/main.tf @@ -0,0 +1,54 @@ +variable "input" { + type = string + default = <<-EOT + ............ + ........0... + .....0...... + .......0.... + ....0....... + ......A..... + ............ + ............ + ........A... + .........A.. + ............ + ............ + EOT + +} + +locals { + lines = split("\n", chomp(var.input)) + height = length(local.lines) + width = length(local.lines[0]) + + antennae = concat([ + for y in range(local.height) : + [ + for x in range(local.width) : + [substr(local.lines[y], x, 1), x, y] + if substr(local.lines[y], x, 1) != "." + ] + ]...) + + by_freq = { + for antenna in local.antennae : + antenna[0] => [antenna[1], antenna[2]]... + } +} + +module "freq" { + source = "./freq" + for_each = local.by_freq + width = local.width + height = local.height + antennae = each.value +} + +output "part1" { + value = length(setunion([for _, v in module.freq : v.nodes1]...)) +} + +output "part2" { + value = length(setunion([for _, v in module.freq : v.nodes2]...)) +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index 868efc8..8054b59 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -66,6 +66,19 @@ output "day05_1" { value = module.day05.part1 } +module "day08" { + source = "./day08" + input = file("../inputs/08.txt") +} + +output "day08_1" { + value = module.day08.part1 +} + +output "day08_2" { + value = module.day08.part2 +} + module "day11" { source = "./day11" input = file("../inputs/11.txt") diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index 95c715a..9eb5fbe 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -131,6 +131,29 @@ run "day5_1" { } } +run "day08" { + command = plan + + module { + source = "./day08" + } + + variables { + input = file("../tests/samples/08.txt") + } + + assert { + condition = output.part1 == 14 + error_message = "Part1 output is wrong" + } + + + assert { + condition = output.part2 == 34 + error_message = "Part1 output is wrong" + } +} + run "day11" { command = plan