stupid decompression algorithm
This commit is contained in:
		
							
								
								
									
										44
									
								
								09-2.py
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								09-2.py
									
									
									
									
									
								
							| @@ -1,28 +1,34 @@ | ||||
| # Python! | ||||
| import re | ||||
|  | ||||
| class compression_group: | ||||
| 	def __init__(self, match_object): | ||||
| 		self.length, self.reps = [int(x) for x in match_object.groups()] | ||||
| 		self.marker_start, self.body_start = match_object.span() | ||||
| 		self.end = self.body_start + self.length | ||||
|  | ||||
|  | ||||
| with open('09.txt') as file: | ||||
| 	compressed = file.read().rstrip('\n') | ||||
|  | ||||
|  | ||||
| def split_groups(sequence) | ||||
| 	groups = [] | ||||
| 	while sequence: | ||||
| 		marker = marker_exp.match(sequence) | ||||
| 		stop = marker.span()[1] + int(marker.groups()[0]) | ||||
| 		groups.append(sequence[:stop]) | ||||
| 		sequence = sequence[stop:] | ||||
| 	return groups | ||||
|  | ||||
|  | ||||
| marker_exp = re.compile('\((\d*)x(\d*)\)') | ||||
| char_exp = re.compile('[\(\)\dx]') | ||||
|  | ||||
| sections = split_groups(compressed) | ||||
|  | ||||
| for section in sections: | ||||
| 	subgroups = [] | ||||
| 	while marker_exp.findall(subgroups) > 1: | ||||
| 		subgroups = split_groups(section) | ||||
|  | ||||
|  | ||||
| markers = marker_exp.finditer(compressed) | ||||
| groups = [compression_group(m) for m in markers] | ||||
|  | ||||
| characters = 0 | ||||
| index = 0 | ||||
| for i, c in enumerate(compressed): | ||||
| 	if not char_exp.match(c): | ||||
| 		current_groups = [] | ||||
| 		multiplier = 1 | ||||
| 		for group in groups: | ||||
| 			if group.body_start <= i < group.end: | ||||
| 				multiplier *= group.reps | ||||
| 				current_groups.append(group) | ||||
| 			elif current_groups and i > current_groups[0].end: | ||||
| 				break | ||||
| 		characters += multiplier | ||||
|  | ||||
| print(characters) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user