Advent of Code: Day 10 Solution

This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.

And now, on with the solution:

Day 10, part 1

Simplified problem: You are given a list of instructions, in the formats:

  • Value x goes to bot y
  • Bot x gives low to [bot or output] y and high to [bot or output] z

A bot can hold a maximum of two values at one time. An output holds one value only. High and low values are passed only if a bot holds two values.

Which bot is responsible for passing values x and y (where x and y are specific values)?

Solution

  1. Create a Bot class with properties:
    1. ID = the bot number
    2. Values = list of values (maximum 2)
  2. Set low = (low specific value)
  3. Set high = (high specific value)
  4. Set input = (your input)
  5. Set botList = new list of Bot
  6. Set outputList = new list of Dictionary<int,int>
  7. Set botId=nothing (this is the ID of the bot that is responsible for passing the specific values we’re interested in)
  8. For each instruction in input staring with “value” (these instructions set up bots and give them a value)
    1. Get the values from instruction
    2. Set bot = new Bot with ID=[first value] and Values=[second value]
    3. Add bot to botList
  9. End For
  10. Set instructions = new Queue
  11. For each instruction in input not starting with “value” (these instructions tell bots where to pass their values)
    1. Add instruction to instructions
  12. End For
  13. While instructions is not empty
    1. Remove instruction from instructions
    2. Set currentBotId = bot ID
    3. Set lowValueDestination = (bot or output)
    4. Set lowValueId = (bot or output ID)
    5. Set highValueDestination = (bot or output)
    6. Set highValueId = (bot or output ID)
    7. If currentBot ID is not found in botList
      1. Set bot = new Bot with ID=currentBotId
      2. Add bot to botList
      3.  Add instruction back to instructions
      4. Return to Step 13 and process the next instruction
    8. End If
    9. Set currentBot = the bot in botList with ID currentBotId
    10. If currentBot does not hold two values
      1. Add instruction back to instructions
      2. Return to Step 13 and process the next instruction
    11. End If
    12. Set botLowValue = the lowest of the two values held by currentBot
    13. Set botHighValue = the remaining value held by currentBot
    14. If botLowValue = lowValue and botHighValue = highValue
      1. Set botId = currentBot ID
    15. If lowValueDestination = “bot”
      1. If botList does not contain a bot with lowValueId
        1. Set bot = new Bot with ID=lowValueId and value botLowValue
        2. Add bot to botList
      2. Else
        1. Add botLowValue to the bot in botList with ID lowValueId
      3. End If
    16. Else (lowValueDestination = “output”)
      1. If outputList does not contain an output with lowValueId
        1. Set output = new dictionaryItem with Key=lowValueId and value botLowValue
        2. Add output to outputList
      2. Else
        1. Set outputList[lowValueId] = botLowValue
      3. End If
    17. End If
    18. Remove botLowValue from currentBot
    19. If highValueDestination = bot
      1. If botList does not contain a bot with highValueId
        1. Set bot = new Bot with ID=highValueId and value botHighValue
        2. Add bot to botList
      2. Else
        1. Add botHighValue to the bot in botList with ID highValueId
      3. End If
    20. Else (lowValueDestination = output)
      1. If outputList does not contain an output with highValueId
        1. Set output = new dictionaryItem with Key=highValueId and value botHighValue
        2. Add output to outputList
      2. Else
        1. Set outputList[highValueId] = botHighValue
      3. End If
    21. End If
  14. End While
  15. Return botId

Day 10, part 2

Using the same rules and input as in Part 1, what is the multiplied result of the values in Output 0, 1 and 2?

Solution

 

  1. Perform the same steps as in Part 1
  2. Return the value of outputList[0] * outputList[1] * outputList[2]

You can find the full solution on Github

Leave a Reply

Your email address will not be published. Required fields are marked *