From 401b3271a56394b77e89744a5ad6709280c2468d Mon Sep 17 00:00:00 2001 From: Lucidiot <15960782+Lucidiot@users.noreply.github.com> Date: Sat, 9 Dec 2017 12:05:32 +0100 Subject: [PATCH] =?UTF-8?q?Puzzle=20du=209=20d=C3=A9cembre=202017?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2017/9/stream.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2017/9/stream.py diff --git a/2017/9/stream.py b/2017/9/stream.py new file mode 100644 index 0000000..7cd7953 --- /dev/null +++ b/2017/9/stream.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import sys +data = sys.stdin.readline() +total, i, currentscore, garbagecount = 0, 0, 0, 0 +in_garbage = False +scores = [] + +while i < len(data): + if data[i] == "!": + i += 1 + elif data[i] == "<" and not in_garbage: + in_garbage = True + elif in_garbage: + if data[i] == ">": + in_garbage = False + else: + garbagecount += 1 + elif data[i] == "{" and not in_garbage: + currentscore += 1 + scores.append(currentscore) + elif data[i] == "}" and not in_garbage: + currentscore -= 1 + total += scores.pop() + i += 1 + +print(total, garbagecount)