mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 2 in Terraform
This commit is contained in:
@@ -12,7 +12,7 @@ run "run" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = output.part2 == 31
|
condition = output.part2 == 31
|
||||||
error_message = "Part2 output is wrong"
|
error_message = "Part2 output is wrong"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
2024/bonus/day02/is_savable/main.tf
Normal file
17
2024/bonus/day02/is_savable/main.tf
Normal file
@@ -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)
|
||||||
|
}
|
||||||
14
2024/bonus/day02/is_valid/main.tf
Normal file
14
2024/bonus/day02/is_valid/main.tf
Normal file
@@ -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
|
||||||
|
}
|
||||||
28
2024/bonus/day02/main.tf
Normal file
28
2024/bonus/day02/main.tf
Normal file
@@ -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])
|
||||||
|
}
|
||||||
19
2024/bonus/day02/sample.tftest.hcl
Normal file
19
2024/bonus/day02/sample.tftest.hcl
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ terraform {
|
|||||||
|
|
||||||
module "day01" {
|
module "day01" {
|
||||||
source = "./day01"
|
source = "./day01"
|
||||||
input = file("../inputs/01.txt")
|
input = file("../inputs/01.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
output "day01_1" {
|
output "day01_1" {
|
||||||
@@ -14,3 +14,16 @@ output "day01_1" {
|
|||||||
output "day01_2" {
|
output "day01_2" {
|
||||||
value = module.day01.part2
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user