from string import ascii_lowercase, ascii_uppercase from commons import get_input removals = [''.join(x) for x in list(zip(ascii_uppercase, ascii_lowercase)) + list(zip(ascii_lowercase, ascii_uppercase))] inp = get_input(5) def find_reduced_length(txt): last = "" while last != txt: last = txt for removal in removals: txt = txt.replace(removal, '') if txt != last: break return len(txt) def find_best_removal(txt): min_length = 1000000000 for ch in ascii_lowercase: length = find_reduced_length(txt.replace(ch, '').replace(ch.upper(), '')) if length < min_length: min_length = length return min_length
mm[minute] += 1 return max(mm.items(), key=lambda x: x[1]) def find_sleepiest_minute_guard(): currentmax = 0 gm, mm = '0', 0 for guard in guards.keys(): minute, value = find_sleepiest_minute(guard) if value > currentmax: currentmax = value gm, mm = guard, minute return int(gm) * mm txt = get_input(4) events = [] sleepy_time = defaultdict(int) guards = defaultdict(lambda: defaultdict(lambda: defaultdict(bool))) parse_tree() events.sort() find_times() find_sums() sleepiest = max(sleepy_time, key=sleepy_time.get) sleepiest_minute = find_sleepiest_minute(sleepiest)[0] if __name__ == '__main__': print('first:', sleepiest_minute * int(sleepiest)) print('second:', find_sleepiest_minute_guard())
infinite_areas.add(grid[i, 0]) infinite_areas.add(grid[i, size - 1]) def find_counts(): for i in range(size): for j in range(size): ar = grid[i, j] if ar in infinite_areas: continue counts[ar] += 1 points = [ tuple(int(co.strip()) + 1 for co in line.split(',')) for line in remove_empty(get_input(6).split('\n')) ] size = max(map(max, points)) grid = np.zeros((size, size), dtype=int) infinite_areas = set() safe_points = 0 counts = defaultdict(int) fill_grid() find_infinite_areas() find_counts() biggest_area = max(counts.values()) if __name__ == '__main__': print("first:", biggest_area) print("second:", safe_points)
print(time) available = set(find_available_steps(available, done)) found = available - set(steps) - set(map(lambda w: w[0], worker)) if len(worker) == 0 and len(found) == 0: break for i in range(len(worker)): job, passed = worker[i] worker[i] = job, passed + 1 finished = list( filter(lambda w: w[1] >= ord(w[0]) - ord('A') + 60, worker)) for d in finished: done.add(d[0]) steps.append(d[0]) worker = list( filter(lambda w: w[1] < ord(w[0]) - ord('A') + 60, worker)) for job in list(found)[:5 - len(worker)]: worker.append((job, 0)) time += 1 return time lines = remove_empty(get_input(7).splitlines(keepends=False)) requirements = defaultdict(list) fill_requirements() if __name__ == '__main__': print('first:', ''.join(find_order())) print('second:', process_order())
from itertools import cycle from commons import get_input, remove_empty seq = list(map(int, remove_empty(get_input(1).split('\n')))) found = set() freq = 0 dup = 0 for el in cycle(seq): freq += el if freq in found: dup = freq break found.add(freq) if __name__ == '__main__': print("first:", sum(seq)) print("second:", dup)
from collections import Counter from commons import get_input, remove_empty txt = set(remove_empty(get_input(2).split('\n'))) counts = [set(Counter(l).values()) for l in txt] threes = sum(3 in count for count in counts) twos = sum(2 in count for count in counts) def find(): for i in txt: for j in txt: if i == j: continue for k in range(len(j)): i_, j_ = list(i), list(j) i_[k] = j_[k] if i_ == j_: return i[:k] + i[k + 1:] if __name__ == '__main__': print("first: ", twos * threes) print("second:", find())
for i in range(1000): for j in range(1000): if grid[i, j] > 1: c += 1 return c def is_overlapping(x, y, w, h): for i in range(w): for j in range(h): if grid[x + i, y + j] != 1: return True return False def find_unique(): for id, x, y, w, h in selection: if not is_overlapping(x, y, w, h): return id lines = get_input(3).splitlines(keepends=False) selection = [parse_line(line) for line in lines] insert_values() if __name__ == '__main__': print('first:', count_overlaps()) print('second:', find_unique())