def first_invalid(numbers): invalids = [ w[25] for w in windowed(numbers, 26) if w[25] not in {a + b for a, b in combinations(w[:25], 2)} ] return invalids[0]
def got_matching_pairs(self): # cannot overlap - 4 in a row is a match, 3 in a row is not if SantaString2.four_in_a_row.match(self.s): return True without_triples = SantaString2.three_in_a_row.sub(lambda m: m.group(1) * 2, self.s) c = collections.Counter(windowed(without_triples, 2)) return any(map(lambda v: v >= 2, c.values()))
def part1(): with ManyLineInput('./input.txt', int) as data: socket = 0 inbuilt = max(data) + 3 counter = collections.Counter( map(lambda w: w[1] - w[0], windowed(sorted(data + [socket, inbuilt]), 2))) answer = counter[1] * counter[3] print(f"part 1: {answer}")
def part2(): with ManyLineInput('./input.txt', Distance) as data: distances = {d.locations: d.distance for d in data} locations = reduce(lambda s, d: set(d.locations).union(s), data, set()) answer = max( map( lambda selection: sum( map(lambda w: distances[tuple(sorted(w))], windowed(selection, 2))), permutations(locations, len(locations)))) print(f"part 2: {answer}")
def got_straight(p): return any( map( lambda w: ord(w[0]) + 1 == ord(w[1]) and ord(w[1]) + 1 == ord(w[2] ), windowed(p, 3)))
def got_split_pair(self): return any(map(lambda w: w[0] == w[2], windowed(self.s, 3)))
def got_double_letter(self): return any(map(lambda w: w[0] == w[1], windowed(self.s, 2)))