🏡 index : ~doyle/aoc.git

author Jordan Doyle <jordan@doyle.la> 2023-12-11 4:12:31.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-12-11 4:32:38.0 +00:00:00
commit
96e45c19b758a8fc5f7d56e3716f77427633bf69 [patch]
tree
2c96cf1946b43146ec01ea26ddda32aeed2da403
parent
755dbc8076d7c07860a24150a07f0510a5dca452
download
96e45c19b758a8fc5f7d56e3716f77427633bf69.tar.gz

Add 2022 day 5



Diff

 .gitignore                 |  2 ++
 2022/5/.terraform.lock.hcl | 19 +++++++++++++++++++
 2022/5/default.nix         | 19 +++++++++++++++++++
 2022/5/main.tf.jinja2      | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2022/5/executor/main.tf    | 24 ++++++++++++++++++++++++
 5 files changed, 118 insertions(+)

diff --git a/.gitignore b/.gitignore
index b2be92b..ec8275a 100644
--- a/.gitignore
+++ a/.gitignore
@@ -1,1 +1,3 @@
result
.tfstate
.terraform
diff --git a/2022/5/.terraform.lock.hcl b/2022/5/.terraform.lock.hcl
new file mode 100644
index 0000000..b65dc1c 100644
--- /dev/null
+++ a/2022/5/.terraform.lock.hcl
@@ -1,0 +1,19 @@
# This file is maintained automatically by "tofu init".
# Manual edits may be lost in future updates.

provider "registry.opentofu.org/hashicorp/local" {

  version = "2.4.0"
  hashes = [

    "h1:cZKMbwSvmjK4bgXY+1gSBd5nodgkhRnw90lYvB8j0hI=",
    "zh:184d6ec1f0e77713b37f0d9cf943b1371f2aa2f44c2c5a618978e897ce3dccab",
    "zh:2205a7955a4051c2c25e69646a60746d9416b73001491808ae5d10620f7b7ac1",
    "zh:256ddc56457f725819dc6be62f2d0bb3b9fee40a61771317bb32353df5b5c1a0",
    "zh:70146e603f540523f6fa2251dd52c225db5a92bda8c07fd198ed51ae2b50176b",
    "zh:8c3f9fe12ab8843e25ff7edabc26e01df4a0e8db204e432600a4c77a95ec0535",
    "zh:b003e421f643d14247d31dcb7f0f6470c46f772d0e15a175a555a525ce344bf2",
    "zh:b4c8ad7c5696aeb2a52adf6047d1e01943fafa57dc123d5192542527406ffd72",
    "zh:c3b6fbfa431f3c085621c74596ee63681a278fd433a4758f33c627e8936d5cb3",
    "zh:d2e57b19295b326d84ca5f39b797849d901170d5509aa7558f2a6545c9ce72a9",
    "zh:e2307421b0b380eb0e8fcee008e0af98ae30fccbfc9e9a1d24d952489e9b0df9",
  ]
}
diff --git a/2022/5/default.nix b/2022/5/default.nix
new file mode 100644
index 0000000..915e140 100644
--- /dev/null
+++ a/2022/5/default.nix
@@ -1,0 +1,19 @@
# must use --option sandbox false
{ pkgs ? import <nixpkgs> { } }:
pkgs.stdenv.mkDerivation rec {
  name = "aoc-2022-5";
  src = ./.;

  phases = [ "buildPhase" ];
  doCheck = true;

  buildInputs = with pkgs; [ opentofu jinja2-cli ];

  buildPhase = ''
    cp -r $src/* .
    jinja2 main.tf.jinja2 > main.tf

    tofu init
    tofu apply -auto-approve -lock=false -var=outpath=$out
  '';
}
diff --git a/2022/5/main.tf.jinja2 b/2022/5/main.tf.jinja2
new file mode 100644
index 0000000..efe983f 100644
--- /dev/null
+++ a/2022/5/main.tf.jinja2
@@ -1,0 +1,54 @@
{% set maxRecursion = 501 %}

locals {
  input         = file("input")
  inputParts    = split("\n\n", local.input)
  blocks        = split("\n", local.inputParts[0])
  parsedBlocks  = [for s in [for s in slice(local.blocks, 0, length(local.blocks) - 1) : split("", s)] : [for j in range(1, length(s), 4) : s[j]]]
  stackedBlocks = [for i in range(length(local.parsedBlocks[0])) : [for j in range(length(local.parsedBlocks)) : local.parsedBlocks[j][i] if local.parsedBlocks[j][i] != " "]]
  instructions  = split("\n", replace(replace(replace(local.inputParts[1], "move ", ""), " from ", ","), " to ", ","))
  output = {
    "part1" = join("", [for v in module.executor-{{ maxRecursion - 1 }}.finalBlocks : v[0]]),
    "part2" = join("", [for v in module.executor-non-reversed-{{ maxRecursion - 1 }}.finalBlocks : v[0]])
  }
}

# you'll have to forgive me for this blasphemy. hashicorp did everything in their power
# to prevent any forms of recursion, and i can't be bothered copy and pasting this block
# 1000 times.
{% for i in range(maxRecursion) %}
module "executor-{{ i }}" {
  source = "./executor"

  {% if i == 0 %}
    stackedBlocks = local.stackedBlocks
    instructions = local.instructions
  {% else %}
    stackedBlocks = module.executor-{{ i - 1 }}.finalBlocks
    instructions = module.executor-{{ i - 1 }}.remainingInstructions
  {% endif %}

  reverse = true
}

module "executor-non-reversed-{{ i }}" {
  source = "./executor"

  {% if i == 0 %}
    stackedBlocks = local.stackedBlocks
    instructions = local.instructions
  {% else %}
    stackedBlocks = module.executor-non-reversed-{{ i - 1 }}.finalBlocks
    instructions = module.executor-non-reversed-{{ i - 1 }}.remainingInstructions
  {% endif %}

  reverse = false
}
{% endfor %}

resource "local_file" "out" {
  filename = var.outpath
  content  = jsonencode(local.output)
}

variable "outpath" {}
diff --git a/2022/5/executor/main.tf b/2022/5/executor/main.tf
new file mode 100644
index 0000000..3ed34a4 100644
--- /dev/null
+++ a/2022/5/executor/main.tf
@@ -1,0 +1,24 @@
locals {

  remainingInstructions = length(var.instructions) == 0 ? [] : slice(var.instructions, 1, length(var.instructions))
  currentInstruction = length(var.instructions) == 0 ? null : split(",", var.instructions[0])
  amount = local.currentInstruction == null ? null : tonumber(local.currentInstruction[0])
  fromStack = local.currentInstruction == null ? null : (tonumber(local.currentInstruction[1]) - 1)
  toStack = local.currentInstruction == null ? null : (tonumber(local.currentInstruction[2]) - 1)
  setFromBlocks = local.currentInstruction == null ? null : slice(var.stackedBlocks[local.fromStack], local.amount, length(var.stackedBlocks[local.fromStack]))
  newToBlocks = local.currentInstruction == null ? null : slice(var.stackedBlocks[local.fromStack], 0, local.amount)
  newToBlocksMaybeReversed = var.reverse ? reverse(local.newToBlocks) : local.newToBlocks
  setToBlocks = local.currentInstruction == null ? null : concat(local.newToBlocksMaybeReversed, var.stackedBlocks[local.toStack])
  finalBlocks = local.currentInstruction == null ? var.stackedBlocks : [for i in range(length(var.stackedBlocks)) : (i == local.fromStack ? local.setFromBlocks : (i == local.toStack ? local.setToBlocks : var.stackedBlocks[i]))]
}

variable "stackedBlocks" {}
variable "instructions" {}
variable "reverse" {}

output "finalBlocks" {

  value = local.finalBlocks
}

output "remainingInstructions" {

  value = local.remainingInstructions
}