From b2add928ade96a8804d22f14594a5757ace28081 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 2 Dec 2024 18:25:01 +0100 Subject: [PATCH] Implement day 2 in Terraform --- 2024/bonus/day01/sample.tftest.hcl | 2 +- 2024/bonus/day02/is_savable/main.tf | 17 +++++++++++++++++ 2024/bonus/day02/is_valid/main.tf | 14 ++++++++++++++ 2024/bonus/day02/main.tf | 28 ++++++++++++++++++++++++++++ 2024/bonus/day02/sample.tftest.hcl | 19 +++++++++++++++++++ 2024/bonus/main.tf | 17 +++++++++++++++-- 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 2024/bonus/day02/is_savable/main.tf create mode 100644 2024/bonus/day02/is_valid/main.tf create mode 100644 2024/bonus/day02/main.tf create mode 100644 2024/bonus/day02/sample.tftest.hcl diff --git a/2024/bonus/day01/sample.tftest.hcl b/2024/bonus/day01/sample.tftest.hcl index 51fc196..ed219ea 100644 --- a/2024/bonus/day01/sample.tftest.hcl +++ b/2024/bonus/day01/sample.tftest.hcl @@ -12,7 +12,7 @@ run "run" { } assert { - condition = output.part2 == 31 + condition = output.part2 == 31 error_message = "Part2 output is wrong" } diff --git a/2024/bonus/day02/is_savable/main.tf b/2024/bonus/day02/is_savable/main.tf new file mode 100644 index 0000000..cedf031 --- /dev/null +++ b/2024/bonus/day02/is_savable/main.tf @@ -0,0 +1,17 @@ +variable "report" { + type = list(number) +} + +module "is_valid" { + source = "../is_valid" + count = length(var.report) + + report = concat( + count.index > 0 ? slice(var.report, 0, count.index) : [], + count.index < length(var.report) - 1 ? slice(var.report, count.index + 1, length(var.report)) : [] + ) +} + +output "valid" { + value = anytrue(module.is_valid[*].valid) +} diff --git a/2024/bonus/day02/is_valid/main.tf b/2024/bonus/day02/is_valid/main.tf new file mode 100644 index 0000000..f175613 --- /dev/null +++ b/2024/bonus/day02/is_valid/main.tf @@ -0,0 +1,14 @@ +variable "report" { + type = list(number) +} + +locals { + delta = [for i in range(1, length(var.report)) : var.report[i] - var.report[i - 1]] + + all_negative = alltrue([for d in local.delta : d <= -1 && d >= -3]) + all_positive = alltrue([for d in local.delta : d >= 1 && d <= 3]) +} + +output "valid" { + value = local.all_negative || local.all_positive +} diff --git a/2024/bonus/day02/main.tf b/2024/bonus/day02/main.tf new file mode 100644 index 0000000..6274b8f --- /dev/null +++ b/2024/bonus/day02/main.tf @@ -0,0 +1,28 @@ +variable "input" { + type = string +} + +locals { + reports = [for line in split("\n", trim(var.input, "\n")) : [for num in split(" ", line) : parseint(num, 10)]] +} + +module "part1_valid" { + source = "./is_valid" + + count = length(local.reports) + report = local.reports[count.index] +} + +module "part2_valid" { + source = "./is_savable" + count = length(local.reports) + report = local.reports[count.index] +} + +output "part1" { + value = length([for i in range(length(local.reports)) : true if module.part1_valid[i].valid]) +} + +output "part2" { + value = length([for i in range(length(local.reports)) : true if module.part2_valid[i].valid]) +} diff --git a/2024/bonus/day02/sample.tftest.hcl b/2024/bonus/day02/sample.tftest.hcl new file mode 100644 index 0000000..d8c4b53 --- /dev/null +++ b/2024/bonus/day02/sample.tftest.hcl @@ -0,0 +1,19 @@ +variables { + input = file("../../tests/samples/02.txt") +} + +run "run" { + + command = plan + + assert { + condition = output.part1 == 2 + error_message = "Part1 output is wrong" + } + + assert { + condition = output.part2 == 4 + error_message = "Part2 output is wrong" + } + +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index d157bb3..c714f21 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -1,10 +1,10 @@ terraform { - + } module "day01" { source = "./day01" - input = file("../inputs/01.txt") + input = file("../inputs/01.txt") } output "day01_1" { @@ -14,3 +14,16 @@ output "day01_1" { output "day01_2" { value = module.day01.part2 } + +module "day02" { + source = "./day02" + input = file("../inputs/02.txt") +} + +output "day02_1" { + value = module.day02.part1 +} + +output "day02_2" { + value = module.day02.part2 +}