aoc2017-day12

Run Settings
LanguagePython
Language Version
Run Command
import sys, re, math from functools import reduce from functools import partial from functools import cmp_to_key from operator import itemgetter from operator import attrgetter def rrotate(sequence, amount): l = list(sequence) amount %= len(l) return l[-amount:] + l[:-amount] def lrotate(sequence, amount): l = list(sequence) amount %= len(l) return l[amount:] + l[:amount] def slice(sequence, position, length): sequence = list(sequence) assert(length >= 0 and length <= len(sequence)) position %= len(sequence) return lrotate(sequence,position)[:length] def insert(sequence, toinsert, position, length=None): sequence = list(sequence) toinsert = list(toinsert) size = length if length != None else len(toinsert) assert(size >= 0 and size <= len(sequence)) position %= len(sequence) return rrotate(toinsert[:size] + lrotate(sequence,position)[size:], position) def xorall(sequence): return reduce(lambda x,y: x^y, sequence) def mulall(sequence): return reduce(lambda x,y: x*y, sequence) def sumall(sequence): return reduce(lambda x,y: x+y, sequence) def splitevery(sequence, step): l = list(sequence) r = [] num, rest = divmod(len(l), step) for e in range(num): r.append(slice(l, e*step, step)) if rest > 0: r.append(slice(l, num*step, rest)) return r def splitin(sequence, number): l = list(sequence) return splitevery(sequence,(len(l)+number-1)//number) def formatall(sequence, fmt="%s", sep=""): return sep.join(map(lambda x: fmt % x, sequence)) def vecapply(applypair, *sequences): return map(applypair, zip(*sequences)) def vecsum(*sequences): return list(vecapply(sumall, *sequences)) def vecmul(*sequences): return list(vecapply(mulall, *sequences)) def vecxor(*sequences): return list(vecapply(xorall, *sequences)) def mandist(*coords): return sumall([int(math.fabs(x)) for x in coords]) def parse(input): for line in input.split("\n"): if line: l = re.sub("[-'{}<>,]", "", line).split() yield list(l) #input = open("input12.txt").read() input = sys.stdin.read() succs = {} for l in parse(input): all = l for s in all: if s not in succs: succs[s] = set() for n in all: succs[s].add(n) #for i in succs: # print(i,succs[i]) def mark(initial, root, succs, group, groups): if root in group: return group[root] = initial if initial not in groups: groups[initial] = set() groups[initial].add(root) for s in succs[root]: mark(initial, s, succs, group, groups) group = {} groups = {} for initial in sorted(succs.keys()): mark(initial, initial, succs, group, groups) roots = set() for i in group: roots.add(group[i]) #print(len(roots)) assert(len(roots) == len(groups.keys())) print(len(groups.keys()))
Editor Settings
Theme
Key bindings
Full width
Lines