From ecfe5e9f209c59e89b9e2d589f96b6403839c7e8 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 5 Dec 2024 22:14:35 +0100 Subject: [PATCH] Implement day 4 in Terraform --- .../day04/check_point/check_x_mas/main.tf | 30 +++++++++ .../day04/check_point/check_xmas/main.tf | 27 ++++++++ 2024/bonus/day04/check_point/main.tf | 64 +++++++++++++++++++ 2024/bonus/day04/main.tf | 28 ++++++++ 2024/bonus/main.tf | 6 ++ 2024/bonus/tests.tftest.hcl | 39 +++++++++++ 6 files changed, 194 insertions(+) create mode 100644 2024/bonus/day04/check_point/check_x_mas/main.tf create mode 100644 2024/bonus/day04/check_point/check_xmas/main.tf create mode 100644 2024/bonus/day04/check_point/main.tf create mode 100644 2024/bonus/day04/main.tf diff --git a/2024/bonus/day04/check_point/check_x_mas/main.tf b/2024/bonus/day04/check_point/check_x_mas/main.tf new file mode 100644 index 0000000..baa416c --- /dev/null +++ b/2024/bonus/day04/check_point/check_x_mas/main.tf @@ -0,0 +1,30 @@ +variable "grid" { + type = list(string) +} + +variable "x" { + type = number +} + +variable "y" { + type = number +} + +locals { + found_a = substr(var.grid[var.y], var.x, 1) == "A" + + c1 = substr(var.grid[var.y - 1], var.x - 1, 1) + c2 = substr(var.grid[var.y - 1], var.x + 1, 1) + c3 = substr(var.grid[var.y + 1], var.x + 1, 1) + c4 = substr(var.grid[var.y + 1], var.x - 1, 1) + + d1 = "${local.c1}${local.c3}" + d2 = "${local.c2}${local.c4}" + + found_d1 = contains(["SM", "MS"], local.d1) + found_d2 = contains(["SM", "MS"], local.d2) +} + +output "found" { + value = local.found_a && local.found_d1 && local.found_d2 +} diff --git a/2024/bonus/day04/check_point/check_xmas/main.tf b/2024/bonus/day04/check_point/check_xmas/main.tf new file mode 100644 index 0000000..ae93c36 --- /dev/null +++ b/2024/bonus/day04/check_point/check_xmas/main.tf @@ -0,0 +1,27 @@ +variable "grid" { + type = list(string) +} + +variable "x" { + type = number +} + +variable "y" { + type = number +} + +variable "dx" { + type = number +} + +variable "dy" { + type = number +} + +locals { + match = [for i in range(4) : var.x + i * var.dx >= 0 && try(substr(var.grid[var.y + i * var.dy], var.x + i * var.dx, 1), "F") == substr("XMAS", i, 1)] +} + +output "found" { + value = alltrue(local.match) ? 1 : 0 +} diff --git a/2024/bonus/day04/check_point/main.tf b/2024/bonus/day04/check_point/main.tf new file mode 100644 index 0000000..54450c4 --- /dev/null +++ b/2024/bonus/day04/check_point/main.tf @@ -0,0 +1,64 @@ +variable "grid" { + type = list(string) +} + +variable "index" { + type = number +} + +variable "width" { + type = number +} + +variable "height" { + type = number +} + +locals { + x = var.index % var.width + y = floor(var.index / var.width) + + directions = { + "UL" = [-1, -1] + "U" = [0, -1] + "UR" = [1, -1] + "L" = [-1, 0] + "R" = [1, 0] + "DL" = [-1, 1] + "D" = [0, 1] + "DR" = [1, 1] + } + + should_check_x_mas = local.x >= 1 && local.y >= 1 && local.x < var.width - 1 && local.y < var.height - 1 +} + +module "check_xmas" { + source = "./check_xmas" + for_each = local.directions + + grid = var.grid + + x = local.x + y = local.y + + dx = each.value[0] + dy = each.value[1] +} + +module "check_x_mas" { + source = "./check_x_mas" + count = local.should_check_x_mas ? 1 : 0 + + grid = var.grid + + y = local.y + x = local.x +} + +output "xmas" { + value = sum([for _, v in module.check_xmas : v.found]) +} + +output "x_mas" { + value = try(module.check_x_mas[0].found, false) ? 1 : 0 +} diff --git a/2024/bonus/day04/main.tf b/2024/bonus/day04/main.tf new file mode 100644 index 0000000..b7d8b18 --- /dev/null +++ b/2024/bonus/day04/main.tf @@ -0,0 +1,28 @@ +variable "input" { + type = string +} + +locals { + grid = split("\n", chomp(var.input)) + height = length(local.grid) + width = length(local.grid[0]) +} + +module "check_point" { + source = "./check_point" + + count = local.width * local.height + + width = local.width + height = local.height + grid = local.grid + index = count.index +} + +output "part1" { + value = sum(module.check_point[*].xmas) +} + +output "part2" { + value = sum(module.check_point[*].x_mas) +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index 89085e6..e72a573 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -40,3 +40,9 @@ output "day03_1" { output "day03_2" { value = module.day03.part2 } + +# Don't run this, it runs out of memory +# module "day04" { +# source = "./day04" +# input = file("../inputs/04.txt") +# } diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index 365f58b..4bfe25e 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -74,3 +74,42 @@ run "day3_2" { error_message = "Part2 output is wrong" } } + +run "day4_small" { + command = plan + + module { + source = "./day04" + } + + variables { + input = file("../tests/samples/04.1.txt") + } + + assert { + condition = output.part1 == 4 + error_message = "Part1 output is wrong" + } +} + +run "day4" { + command = plan + + module { + source = "./day04" + } + + variables { + input = file("../tests/samples/04.2.txt") + } + + assert { + condition = output.part1 == 18 + error_message = "Part1 output is wrong" + } + + assert { + condition = output.part2 == 9 + error_message = "Part2 output is wrong" + } +}