Created
December 4, 2023 08:43
-
-
Save msg555/473921996d5bd087f799423ad4d19666 to your computer and use it in GitHub Desktop.
AdventAsInfrastructure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "local_file" "input" { | |
filename = "${path.module}/input.txt" | |
} | |
locals { | |
reps = { | |
one = "1" | |
two = "2" | |
three = "3" | |
four = "4" | |
five = "5" | |
six = "6" | |
seven = "7" | |
eight = "8" | |
nine = "9" | |
} | |
} | |
data "template_file" "transformed" { | |
template = <<-EOT | |
$${ | |
%{~ for word, num in local.reps ~} | |
replace( | |
%{~ endfor ~} | |
in | |
%{~ for word, num in local.reps ~} | |
, "${word}", "${word}${num}${word}") | |
%{~ endfor ~} | |
} | |
EOT | |
vars = { | |
in = data.local_file.input.content | |
} | |
} | |
locals { | |
filtered_input = <<-EOT | |
EOT | |
num_lines_p1 = [ | |
for line in split("\n", data.local_file.input.content) : | |
replace(line, "/[^0-9]/", "") | |
] | |
num_lines_p2 = [ | |
for line in split("\n", data.template_file.transformed.rendered) : | |
replace(line, "/[^0-9]/", "") | |
] | |
} | |
output "part1" { | |
value = sum([ | |
for line in local.num_lines_p1 : | |
( | |
length(line) > 0 ? | |
10 * tonumber(substr(line, 0, 1)) + tonumber(substr(line, -1, 1)) : | |
0 | |
) | |
]) | |
} | |
output "part2" { | |
value = sum([ | |
for line in local.num_lines_p2 : | |
( | |
length(line) > 0 ? | |
10 * tonumber(substr(line, 0, 1)) + tonumber(substr(line, -1, 1)) : | |
0 | |
) | |
]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment