mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implement day 8 2024 in Terraform
This commit is contained in:
52
2024/bonus/day08/freq/main.tf
Normal file
52
2024/bonus/day08/freq/main.tf
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
variable "width" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "height" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "antennae" {
|
||||||
|
type = list(tuple([number, number]))
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
pairs = concat([
|
||||||
|
for i in range(length(var.antennae)) :
|
||||||
|
[
|
||||||
|
for j in range(i + 1, length(var.antennae)) : [var.antennae[i], var.antennae[j]]
|
||||||
|
]
|
||||||
|
]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
module "pair" {
|
||||||
|
source = "./pair"
|
||||||
|
count = length(local.pairs)
|
||||||
|
|
||||||
|
first = local.pairs[count.index][0]
|
||||||
|
second = local.pairs[count.index][1]
|
||||||
|
width = var.width
|
||||||
|
height = var.height
|
||||||
|
}
|
||||||
|
|
||||||
|
output "nodes1" {
|
||||||
|
value = setunion([
|
||||||
|
for i in range(length(local.pairs)) :
|
||||||
|
[
|
||||||
|
for v in module.pair[i].nodes1 :
|
||||||
|
v
|
||||||
|
if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height
|
||||||
|
]
|
||||||
|
]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
output "nodes2" {
|
||||||
|
value = setunion([
|
||||||
|
for i in range(length(local.pairs)) :
|
||||||
|
[
|
||||||
|
for v in module.pair[i].nodes2 :
|
||||||
|
v
|
||||||
|
if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height
|
||||||
|
]
|
||||||
|
]...)
|
||||||
|
}
|
||||||
34
2024/bonus/day08/freq/pair/main.tf
Normal file
34
2024/bonus/day08/freq/pair/main.tf
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
variable "first" {
|
||||||
|
type = tuple([number, number])
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "second" {
|
||||||
|
type = tuple([number, number])
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "width" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "height" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
dx = var.second[0] - var.first[0]
|
||||||
|
dy = var.second[1] - var.first[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "nodes1" {
|
||||||
|
value = [
|
||||||
|
[var.first[0] - local.dx, var.first[1] - local.dy],
|
||||||
|
[var.second[0] + local.dx, var.second[1] + local.dy],
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "nodes2" {
|
||||||
|
value = concat(
|
||||||
|
[for i in range(max(var.width, var.height)) : [var.first[0] - i * local.dx, var.first[1] - i * local.dy]],
|
||||||
|
[for i in range(max(var.width, var.height)) : [var.second[0] + i * local.dx, var.second[1] + i * local.dy]]
|
||||||
|
)
|
||||||
|
}
|
||||||
54
2024/bonus/day08/main.tf
Normal file
54
2024/bonus/day08/main.tf
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
variable "input" {
|
||||||
|
type = string
|
||||||
|
default = <<-EOT
|
||||||
|
............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............
|
||||||
|
EOT
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
lines = split("\n", chomp(var.input))
|
||||||
|
height = length(local.lines)
|
||||||
|
width = length(local.lines[0])
|
||||||
|
|
||||||
|
antennae = concat([
|
||||||
|
for y in range(local.height) :
|
||||||
|
[
|
||||||
|
for x in range(local.width) :
|
||||||
|
[substr(local.lines[y], x, 1), x, y]
|
||||||
|
if substr(local.lines[y], x, 1) != "."
|
||||||
|
]
|
||||||
|
]...)
|
||||||
|
|
||||||
|
by_freq = {
|
||||||
|
for antenna in local.antennae :
|
||||||
|
antenna[0] => [antenna[1], antenna[2]]...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module "freq" {
|
||||||
|
source = "./freq"
|
||||||
|
for_each = local.by_freq
|
||||||
|
width = local.width
|
||||||
|
height = local.height
|
||||||
|
antennae = each.value
|
||||||
|
}
|
||||||
|
|
||||||
|
output "part1" {
|
||||||
|
value = length(setunion([for _, v in module.freq : v.nodes1]...))
|
||||||
|
}
|
||||||
|
|
||||||
|
output "part2" {
|
||||||
|
value = length(setunion([for _, v in module.freq : v.nodes2]...))
|
||||||
|
}
|
||||||
@@ -66,6 +66,19 @@ output "day05_1" {
|
|||||||
value = module.day05.part1
|
value = module.day05.part1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module "day08" {
|
||||||
|
source = "./day08"
|
||||||
|
input = file("../inputs/08.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
output "day08_1" {
|
||||||
|
value = module.day08.part1
|
||||||
|
}
|
||||||
|
|
||||||
|
output "day08_2" {
|
||||||
|
value = module.day08.part2
|
||||||
|
}
|
||||||
|
|
||||||
module "day11" {
|
module "day11" {
|
||||||
source = "./day11"
|
source = "./day11"
|
||||||
input = file("../inputs/11.txt")
|
input = file("../inputs/11.txt")
|
||||||
|
|||||||
@@ -131,6 +131,29 @@ run "day5_1" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run "day08" {
|
||||||
|
command = plan
|
||||||
|
|
||||||
|
module {
|
||||||
|
source = "./day08"
|
||||||
|
}
|
||||||
|
|
||||||
|
variables {
|
||||||
|
input = file("../tests/samples/08.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = output.part1 == 14
|
||||||
|
error_message = "Part1 output is wrong"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = output.part2 == 34
|
||||||
|
error_message = "Part1 output is wrong"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
run "day11" {
|
run "day11" {
|
||||||
command = plan
|
command = plan
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user