Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. --- Day 9: Stream Processing ---
  2. A large stream blocks your path. According to the locals, it's not safe to cross the stream at the moment because it's full of garbage. You look down at the stream; rather than water, you discover that it's a stream of characters.
  3.  
  4. You sit for a while and record part of the stream (your puzzle input). The characters represent groups - sequences that begin with { and end with }. Within a group, there are zero or more other things, separated by commas: either another group or garbage. Since groups can contain other groups, a } only closes the most-recently-opened unclosed group - that is, they are nestable. Your puzzle input represents a single, large group which itself contains many smaller ones.
  5.  
  6. Sometimes, instead of a group, you will find garbage. Garbage begins with < and ends with >. Between those angle brackets, almost any character can appear, including { and }. Within garbage, < has no special meaning.
  7.  
  8. In a futile attempt to clean up the garbage, some program has canceled some of the characters within it using !: inside garbage, any character that comes after ! should be ignored, including <, >, and even another !.
  9.  
  10. You don't see any characters that deviate from these rules. Outside garbage, you only find well-formed groups, and garbage always terminates according to the rules above.
  11.  
  12. Here are some self-contained pieces of garbage:
  13.  
  14. <>, empty garbage.
  15. <random characters>, garbage containing random characters.
  16. <<<<>, because the extra < are ignored.
  17. <{!>}>, because the first > is canceled.
  18. <!!>, because the second ! is canceled, allowing the > to terminate the garbage.
  19. <!!!>>, because the second ! and the first > are canceled.
  20. <{o"i!a,<{i<a>, which ends at the first >.
  21. Here are some examples of whole streams and the number of groups they contain:
  22.  
  23. {}, 1 group.
  24. {{{}}}, 3 groups.
  25. {{},{}}, also 3 groups.
  26. {{{},{},{{}}}}, 6 groups.
  27. {<{},{},{{}}>}, 1 group (which itself contains garbage).
  28. {<a>,<a>,<a>,<a>}, 1 group.
  29. {{<a>},{<a>},{<a>},{<a>}}, 5 groups.
  30. {{<!>},{<!>},{<!>},{<a>}}, 2 groups (since all but the last > are canceled).
  31. Your goal is to find the total score for all groups in your input. Each group is assigned a score which is one more than the score of the group that immediately contains it. (The outermost group gets a score of 1.)
  32.  
  33. {}, score of 1.
  34. {{{}}}, score of 1 + 2 + 3 = 6.
  35. {{},{}}, score of 1 + 2 + 2 = 5.
  36. {{{},{},{{}}}}, score of 1 + 2 + 3 + 3 + 3 + 4 = 16.
  37. {<a>,<a>,<a>,<a>}, score of 1.
  38. {{<ab>},{<ab>},{<ab>},{<ab>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
  39. {{<!!>},{<!!>},{<!!>},{<!!>}}, score of 1 + 2 + 2 + 2 + 2 = 9.
  40. {{<a!>},{<a!>},{<a!>},{<ab>}}, score of 1 + 2 = 3.
  41. What is the total score for all groups in your input?
  42.  
  43. Your puzzle answer was 12897.
  44.  
  45. The first half of this puzzle is complete! It provides one gold star: *
  46.  
  47. --- Part Two ---
  48. Now, you're ready to remove the garbage.
  49.  
  50. To prove you've removed it, you need to count all of the characters within the garbage. The leading and trailing < and > don't count, nor do any canceled characters or the ! doing the canceling.
  51.  
  52. <>, 0 characters.
  53. <random characters>, 17 characters.
  54. <<<<>, 3 characters.
  55. <{!>}>, 2 characters.
  56. <!!>, 0 characters.
  57. <!!!>>, 0 characters.
  58. <{o"i!a,<{i<a>, 10 characters.
  59. How many non-canceled characters are within the garbage in your puzzle input?
  60.  
  61.  
  62. InputURL: https://adventofcode.com/2017/day/9/input
  63. -----------------------------------------------Pakris solution:------------------------------
  64.  
  65.  
  66. tableday9 = open("TableDay9.txt", "r").readlines()
  67.  
  68. # testgarbage1 = '<>'
  69. # testgarbage2 = '<random characters>'
  70. # testgarbage3 = '<<<<>'
  71. # testgarbage4 = '<{!>}>'
  72. # testgarbage5 = '<!!>'
  73. # testgarbage6 = '<!!!>>'
  74. # testgarbage7 = '<{o"i!a,<{i<a>'
  75.  
  76. # testgarbages = [testgarbage1 + testgarbage2 + testgarbage3 + testgarbage4 + testgarbage5 + testgarbage6 + testgarbage7]
  77.  
  78. # teststream1 = '{}'
  79. # teststream2 = '{{{}}}'
  80. # teststream3 = '{{},{}}'
  81. # teststream4 = '{{{},{},{{}}}}'
  82. # teststream5 = '{<{},{},{{}}>}'
  83. # teststream6 = '{<a>,<a>,<a>,<a>}'
  84. # teststream7 = '{{<a>},{<a>},{<a>},{<a>}}'
  85. # teststream8 = '{{<!>},{<!>},{<!>},{<a>}}'
  86.  
  87. # teststreams = [teststream1 + teststream2 + teststream3 + teststream4 + teststream5 + teststream6 + teststream7, teststream8]
  88.  
  89. # newtest = '''{{{{{{<o,!!!!!!!!iuo!!!>}{{{o{{a<u}>},{<!>,<,">}},{},{{{<},e<i!!!>!!!{'u,!>!!!>}>'''
  90.  
  91. characterlist = list(str(tableday9).strip("['']"))
  92.  
  93. idx = 0
  94. depth = 0
  95. garbage = False
  96. opengroup = False
  97. score = 0
  98. removedgarbage = 0
  99.  
  100. while idx < len(characterlist):
  101. #print("\nIndex: ", idx, "\nCharacter: ", characterlist[idx])
  102. #if garbage == True:
  103. #print("Garbage!")
  104. if characterlist[idx] == "!":
  105. idx += 2
  106. #print("Skip next")
  107. continue
  108. elif characterlist[idx] == "{" and garbage == False:
  109. depth += 1
  110. idx += 1
  111. opengroup = True
  112. #print("depth: ", depth)
  113. continue
  114. elif characterlist[idx] == "}" and garbage == False and opengroup == True:
  115. score += depth
  116. depth -= 1
  117. idx += 1
  118. if depth <= 0:
  119. opengroup = False
  120. #print("depth: ", depth)
  121. #print("Score: ", score)
  122. continue
  123. elif characterlist[idx] == "<" and garbage == False:
  124. idx += 1
  125. garbage = True
  126. continue
  127. elif characterlist[idx] == ">" and garbage == True:
  128. idx += 1
  129. garbage = False
  130. continue
  131. elif garbage == True:
  132. removedgarbage += 1
  133. idx += 1
  134. #print("Total removed garbage: ", removedgarbage)
  135. continue
  136. else:
  137. idx += 1
  138. continue
  139.  
  140. print(removedgarbage)
  141.  
  142.  
  143.  
  144.  
  145. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement