def cog_pages(iterable, start): for name, count in run_length.encode(map(operator.itemgetter(0), iterable)): if count == 1: yield str(start), name else: yield f'{start}-{start + count - 1}', name start += count
def question_2(self): with open("input.txt") as f: numbers = sorted([int(x) for x in f]) cands = [ e for i, e in run_length.encode(np.diff([0] + numbers)) if i == 1 and e > 1 ] combinations = {2: 2, 3: 4, 4: 7} return str(np.prod([combinations[e] for e in cands]))
def number_of_wake_bouts(predictions): """ Calculates the number of wake bouts present in an array of sleep/wake predictions in one minute epochs. Number of wake bouts is exclusive of first wake before sleep, and last wake after sleep. Parameters ---------- predictions : array-like Binary sleep/wake predictions. Awake encoded as 1 and sleep as 0. Returns ------- nwb : int Total number of wake bouts based on sleep/wake predictions provided. """ first_sleep_epoch = predictions.argmin() last_sleep_epoch = predictions[::-1].argmin() predictions = predictions[first_sleep_epoch : -1 - last_sleep_epoch] bouts = list(run_length.encode(predictions)) nwb = len([i for i in bouts if i[0] == 1]) return nwb
def check2(number): number = str(number) pairs = list(zip(number[:-1], number[1:])) return all(num2 >= num1 for num1, num2 in pairs) and any( counter == 2 for num, counter in run_length.encode(number))
import numpy as np from more_itertools import run_length with open("input.txt") as f: numbers = sorted([int(x) for x in f]) k = np.diff(numbers) print((1 + (k == 1).sum()) * (1 + (k == 3).sum())) cands = [ e for i, e in run_length.encode(np.diff([0] + numbers)) if i == 1 and e > 1 ] # Tribonacci numbers - http://oeis.org/wiki/Tribonacci_numbers # 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, ... combinations = {2: 2, 3: 4, 4: 7} print(np.prod([combinations[e] for e in cands]))
import numpy as np adapters = list(map(int, open('day10.txt').readlines())) #adapters=[16,10,15,5,1,11,7,19,6,12,4] #adapters=[28,33,18,42,31,14,46,20,48,47,24,23,49,45,19,38,39,11,1,32,25,35,8,17,7,9,4,2,34,10,3] adapters += [0, max(adapters) + 3] adapted = [b - a for a, b in windowed(sorted(adapters), 2)] diff_dict = map_reduce(adapted, keyfunc=lambda x: x, valuefunc=lambda x: 1, reducefunc=sum) print(diff_dict[1] * diff_dict[3]) ones_differences = map( lambda x: x[1], filter(lambda x: x[0] != 3, list(run_length.encode(adapted)))) @lru_cache def tribonacci(n): if 0 <= n <= 1: return 0 elif n == 2: return 1 else: return tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3) print(reduce(mul, map(lambda x: tribonacci(x + 2), ones_differences), 1)) adapters.sort()
def convert_rank(self, rank): return ''.join(value * count if value else str(count) for value, count in run_length.encode( map(self.convert_cell, rank)))