Solve a couple of 2017 problems.
This commit is contained in:
33
2017/d9.py
Normal file
33
2017/d9.py
Normal file
@@ -0,0 +1,33 @@
|
||||
def part_1(data):
|
||||
i = 0
|
||||
group_score, garbage_score = 0, 0
|
||||
in_garbage = False
|
||||
group_level = 0
|
||||
while i < len(data):
|
||||
match data[i]:
|
||||
case "{" if not in_garbage:
|
||||
group_level += 1
|
||||
case "}" if not in_garbage:
|
||||
assert group_level > 0
|
||||
group_score += group_level
|
||||
group_level -= 1
|
||||
case "<" if not in_garbage:
|
||||
in_garbage = True
|
||||
case ">" if in_garbage:
|
||||
in_garbage = False
|
||||
case "!" if in_garbage:
|
||||
i += 1
|
||||
case _ if in_garbage:
|
||||
garbage_score += 1
|
||||
i += 1
|
||||
print(group_score)
|
||||
print(garbage_score)
|
||||
|
||||
|
||||
def main():
|
||||
data = open(0).read().strip()
|
||||
part_1(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user