Skip to content

Commit

Permalink
Implement 2024 day 14 part 1 in Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
bertptrs committed Dec 27, 2024
1 parent d07bb92 commit 5c030d5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 2024/bonus/day14/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "input" {
type = string
}

variable "width" {
type = number
default = 101
}

variable "height" {
type = number
default = 103
}

locals {
lines = regexall("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)", var.input)
positions = [
for line in local.lines :
[
((line[0] + 100 * line[2]) % var.width + var.width) % var.width,
((line[1] + 100 * line[3]) % var.height + var.height) % var.height,
]
]

q1 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] < floor(var.height / 2)])
q2 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] < floor(var.height / 2)])
q3 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] > floor(var.height / 2)])
q4 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] > floor(var.height / 2)])
}

output "part1" {
value = local.q1 * local.q2 * local.q3 * local.q4
}
9 changes: 9 additions & 0 deletions 2024/bonus/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ output "day13_2" {
value = module.day13.part2
}

module "day14" {
source = "./day14"
input = file("../inputs/14.txt")
}

output "day14_1" {
value = module.day14.part1
}

module "day19" {
source = "./day19"
input = file("../inputs/19.txt")
Expand Down
19 changes: 19 additions & 0 deletions 2024/bonus/tests.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ run "day13" {
}
}

run "day14" {
command = plan

module {
source = "./day14"
}

variables {
input = file("../tests/samples/14.txt")
height = 7
width = 11
}

assert {
condition = output.part1 == 12
error_message = "Part1 output is wrong"
}
}

run "day19" {
command = plan

Expand Down

0 comments on commit 5c030d5

Please sign in to comment.