pier4r

aoc 2022 day 1 terraform

Dec 4th, 2022
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | Source Code | 0 0
  1. # Use the "split" function to split the string into an array
  2. locals {
  3. process_output_max = true
  4.  
  5. input_rows = split(
  6. "\n",
  7. local.process_output_max ?
  8. data.local_file.input_max.content :
  9. data.local_file.input_mini.content
  10. )
  11.  
  12. array_of_empty_positions = [
  13. # identifies the "" in the array so that we can split blocks
  14. for pos, value in local.input_rows : #range(0, length(local.input_rows)) is not usable for lists larger of 1024, and it is worse than index and value.
  15. pos if value == ""
  16. ]
  17.  
  18. array_of_backpacks = [
  19. # now we can get the backpacks as we know where the "ends" are
  20. for pos, value in local.array_of_empty_positions:
  21. pos == 0 ?
  22. slice(local.input_rows, 0, value ) # we pick the slices with values but we don't want the empty position, fortunately the end index is exclusive
  23. :
  24. slice(local.input_rows, local.array_of_empty_positions[pos - 1] + 1, value)
  25. ]
  26.  
  27. array_of_backpacks_sum = [
  28. for backpack in local.array_of_backpacks: sum(backpack)
  29. ]
  30. }
  31.  
  32. # Read the contents of a text file into a Terraform variable
  33. data "local_file" "input_mini" {
  34. # it needs to have an extra empty line at the end to let the code work
  35. filename = "/Users/user/local/scripting/recreational_math_and_prog/terraform/aoc_2022_1_input_mini.txt"
  36. }
  37.  
  38. data "local_file" "input_max" {
  39. # it needs to have an extra empty line at the end to let the code work
  40. filename = "/Users/user/local/scripting/recreational_math_and_prog/terraform/aoc_2022_1_input_max.txt"
  41. }
  42.  
  43. # Output the contents of the file
  44. #output "input_mini_contents" {
  45. # value = data.local_file.input_mini.content
  46. #}
  47.  
  48. #output "input_max_contents" {
  49. # value = data.local_file.input_max.content
  50. #}
  51.  
  52. output "various_debugging" {
  53. value = ""
  54. # value = max(local.array_of_backpacks_sum...)
  55. #value = local.array_of_backpacks_sum
  56. #value = local.array_of_backpacks
  57. #value = index(local.input_rows, "")
  58. #value = [ for pos in range(0, length(local.input_rows)) :
  59. # pos if local.input_rows[pos] == ""
  60. #]
  61. #value = concat([0],
  62. # [
  63. # for pos in range(0, length(local.input_rows)) :
  64. # pos if local.input_rows[pos] == ""
  65. # ]
  66. #)
  67. # value = local.input_rows
  68. }
  69.  
  70. output "part1" {
  71. value = max(local.array_of_backpacks_sum...)
  72. }
  73.  
  74. output "part2" {
  75. value = sum(slice(reverse(sort(local.array_of_backpacks_sum)), 0, 3))
  76. }
  77.  
  78.  
  79. #https://adventofcode.com/2022/day/1
  80. #
  81. #--- Day 1: Calorie Counting ---
  82. #
  83. #Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.
  84. #
  85. #To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.
  86. #
  87. #Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
  88. #
  89. #The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
  90. #
  91. #The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.
  92. #
  93. #For example, suppose the Elves finish writing their items' Calories and end up with the following list:
  94. #
  95. #1000
  96. #2000
  97. #3000
  98. #
  99. #4000
  100. #
  101. #5000
  102. #6000
  103. #
  104. #7000
  105. #8000
  106. #9000
  107. #
  108. #10000
  109. #
  110. #This list represents the Calories of the food carried by five Elves:
  111. #
  112. # The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
  113. # The second Elf is carrying one food item with 4000 Calories.
  114. # The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
  115. # The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
  116. # The fifth Elf is carrying one food item with 10000 Calories.
  117. #
  118. #In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).
  119. #
  120. #Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
  121. #
  122. #--- Part Two ---
  123. #
  124. #By the time you calculate the answer to the Elves' question, they've already realized that the Elf carrying the most Calories of food might eventually run out of snacks.
  125. #
  126. #To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.
  127. #
  128. #In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000.
  129. #
  130. #Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
  131. #
Advertisement
Add Comment
Please, Sign In to add comment