Browse Source

Day 9

master
Chris Smith 7 years ago
parent
commit
5ae8d91115
2 changed files with 27 additions and 0 deletions
  1. 26
    0
      09.py
  2. 1
    0
      09.txt

+ 26
- 0
09.py View File

@@ -0,0 +1,26 @@
1
+#!/usr/bin/python3
2
+
3
+import re
4
+
5
+# Version of parse() that recursively parses repeated sections
6
+rparse = lambda x: parse(x, rparse)
7
+
8
+def parse(data, rfun=len):
9
+	# Find the first bracketed part (if one exists)
10
+	index, bracket = data.find('('), data.find(')')
11
+	if index == -1: return len(data)
12
+
13
+	# Grab the (NxR) values from the brackets
14
+	num, reps = map(int, data[index+1:bracket].split('x'))
15
+	
16
+	# Return the initial, non-repeated data, plus...
17
+	return (len(data[:index])
18
+			# the repeated data, potentially parsed recursively, plus...
19
+	        + reps * rfun(data[bracket+1:bracket+1+num]) 
20
+	        # the remainder of the string, parsed
21
+	        + parse(data[bracket+1+num:], rfun))
22
+
23
+with open('09.txt', 'r') as file:
24
+    input = re.sub('\s+', '', file.read())
25
+    print(parse(input))
26
+    print(rparse(input))

+ 1
- 0
09.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save