Implement day 4 in Terraform

This commit is contained in:
2024-12-05 22:14:35 +01:00
parent 0967a3dfe3
commit ecfe5e9f20
6 changed files with 194 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}