#!/usr/bin/env python3 # -*- coding: utf-8 -*- data = [] # parse input input_file = open("input-ex1", "r") pattern = [] for line in input_file: for value in line.rstrip().split(","): data.append(value) input_file.close() def get_hash(val): h = 0 for b in val: h += ord(b) h *= 17 h %= 256 return h hashsum = 0 boxes = {} for value in data: if value.count("-") == 1: value = value.replace("-", "=") h = get_hash(value[:-1]) if h in boxes.keys(): lin = boxes[h] x = -1 for j in range(len(lin)): if lin[j].startswith(value): x = j if x != -1: lin.pop(x) if len(lin) == 0: boxes.pop(h) else: boxes[h] = lin else: hv = value.split("=")[0] h = get_hash(hv) ersetzt = False if h in boxes.keys(): linsen = boxes[h] for i in range(len(linsen)): if linsen[i].startswith(hv+"="): linsen[i] = value ersetzt = True if not ersetzt: linsen.append(value) boxes[h] = linsen else: boxes[h] = [value] print(boxes) solution=0 for key in boxes.keys(): for k in range(len(boxes[key])): pass