def run(input_file): twos = 0 threes = 0 box_ids = [] for line in process(input_file): box_id = line.strip() box_ids.append(box_id) two_counted = False three_counted = False for letter in box_id: c = box_id.count(letter) if c == 2 and not two_counted: twos += 1 two_counted = True if c == 3 and not three_counted: threes += 1 three_counted = True print(twos * threes) for i, box_id in enumerate(box_ids): for box_id2 in box_ids[i + 1:]: diff = 0 index = -1 for i in range(len(box_id)): if box_id[i] != box_id2[i]: index = i diff += 1 if diff == 1: print("{}{}".format( box_id[0:index], box_id[index + 1:], ))
def run(input_file): inst = re.compile(r"Step (\w) .* step (\w)") rgraph = defaultdict(list) nodes = set() for instruction in process(input_file): steps = inst.match(instruction) head = steps.group(1) step = steps.group(2) nodes.add(head) nodes.add(step) rgraph[step].append(head) nodes = sorted(list(nodes)) nodes_2 = copy.deepcopy(nodes) print("".join(find_path(rgraph, nodes))) cost = {} i = 0 for c in string.ascii_uppercase: cost[c] = i i += 1 workers = Workers() print(find_path_with_workers(rgraph, nodes_2, workers, 60, cost))
def run(input_file): point_re = re.compile( r"position=<\s?([-]?\d+), \s?([-]?\d+)> velocity=<\s?([-]?\d+), \s?([-]?\d+)>" ) points = [] for point in process(input_file): x, y, vx, vy = list(map(int, point_re.match(point).groups())) points.append(Point(x, y, vx, vy)) for i in range(1, 25000): maxx = maxy = 0 minx = miny = 1000000 for p in points: nx = p.x + p.vx ny = p.y + p.vy p.x, p.y = nx, ny if nx > maxx: maxx = nx elif nx < minx: minx = nx if ny > maxy: maxy = ny elif ny < miny: miny = ny if abs(maxx - minx) < 70 and abs(maxy - miny) < 25: print(i) display(points, minx, maxx, miny, maxy) break
def run(input_file): for line in process(input_file): players, last = map(int, line.split()) scores = defaultdict(int) current = Node(0, None, None) current.n = current for i in range(1, last + 1): if (i % 23) == 0: for _ in range(0, 7): current = current.p scores[i % players] += i + current.v current.p.n = current.n current.n.p = current.p current = current.n else: tmp = Node(i, None, None) relink = current.n tmp.n = relink.n tmp.p = relink relink.n = tmp tmp.n.p = tmp current = tmp print(max(scores.values()))
def run(input_file): line = next(process(input_file)) tree = list(map(int, line.split(" "))) metadata = [] dfs_visit(tree, 0, metadata) print(sum(metadata)) metadata2 = [] dfs_visit_2(tree, 0, metadata2) print(metadata2.pop())
def run(input_file): file_contents = process(input_file) initial_state = next(file_contents) initial_state = initial_state[15:] # remove the line description low = 0 high = len(initial_state) state = {} for i, c in enumerate(initial_state): if c == PLANT: state[i] = c next(file_contents) ## drain empty line print_state(state, low, high) patterns = [] for line in file_contents: pattern, result = line.split(" => ") patterns.append(Pattern(pattern, result)) for g in range(0, 20): changes = [] for i in range(low - 2, high + 2): llcrr = ( state.get(i - 2, EMPTY) + state.get(i - 1, EMPTY) + state.get(i, EMPTY) + state.get(i + 1, EMPTY) + state.get(i + 2, EMPTY) ) for pattern in patterns: if llcrr == pattern.pattern and pattern.result == PLANT: changes.append(i) continue new_state = {} for idx in changes: new_state[idx] = PLANT if idx < low: low = idx if idx > high: high = idx state = new_state print(sum(state))
def run(input_file): for grid_serial_number in process(input_file): grid_serial_number = int(grid_serial_number) size = 300 summed_table = summed_area_table(grid_serial_number, size) # part 1 print(compute_for_grid_size(summed_table, size, 3)) # # part 2 best_gs = 0 best_spl = -1000 best_top_left = (1, 1) for grid_size in range(2, 300): top_left, spl = compute_for_grid_size(summed_table, size, grid_size) if spl > best_spl: best_spl = spl best_top_left = top_left best_gs = grid_size print(best_top_left, best_spl, best_gs)
def run(input_file): inputs = next(process(input_file)) cur = 0 while cur < len(inputs) - 1: unit1, unit2 = inputs[cur:cur + 2] if unit1.upper() == unit2.upper() and ( (unit1.islower() and unit2.isupper()) or (unit1.isupper() and unit2.islower())): inputs = inputs[:cur] + inputs[cur + 2:] if cur > 0: cur -= 1 else: cur += 1 print("{}... {}".format(inputs[:100], len(inputs))) chars = set(inputs.lower()) cur_min = len(inputs) min_char = 0 for char in chars: cur = 0 icc = inputs.replace(char, "") icc = icc.replace(char.upper(), "") while cur < len(icc) - 1: unit1, unit2 = icc[cur:cur + 2] if unit1.upper() == unit2.upper() and ( (unit1.islower() and unit2.isupper()) or (unit1.isupper() and unit2.islower())): icc = icc[:cur] + icc[cur + 2:] if cur > 0: cur -= 1 else: cur += 1 if len(icc) < cur_min: cur_min = len(icc) min_char = char print(cur_min, min_char)
def run(input_file): current_frequency = 0 frequencies = defaultdict(int) fr = [] for line in process(input_file): line = line.strip() frequency = int(line) fr.append(frequency) current_frequency += frequency frequencies[current_frequency] += 1 print(current_frequency) i = 0 while frequencies[current_frequency] < 2: current_frequency += fr[i] frequencies[current_frequency] += 1 i += 1 if i >= len(fr): i = 0 print(current_frequency)
def run(input_file): pattern = re.compile("#(\d+) @ (\d+)\,(\d+): (\d+)x(\d+)") cloth = defaultdict(int) whole = set({}) intersect = set({}) overlap = 0 for line in process(input_file): claim = line.strip() g = pattern.match(claim) cid, lefte, righte, width, height = ( g.group(1), int(g.group(2)), int(g.group(3)), int(g.group(4)), int(g.group(5)), ) for i in range(lefte + 1, lefte + width + 1): for j in range(righte + 1, righte + height + 1): # cloth["{}, {}".format(i, j)] += 1 coord = "{},{}".format(i, j) if cloth.get(coord, 0) == 0: whole.add(cid) cloth[coord] = cid else: intersect.add(cid) intersect.add(cloth.get(coord)) # if cloth[coord] != -1: # overlap += 1 cloth[coord] = -1 for v in cloth.values(): if v == -1: overlap += 1 print(overlap) print(whole - intersect)
def run(input_file): guard_number_p = re.compile(".*#(\d+).*") guard_number = -1 minute_p = re.compile(".* 00:(\d+)\].*") asleep = 0 waking = 0 patterns = dict() for line in sorted([line for line in process(input_file)]): log_entry = line.strip() if "Guard" in log_entry: guard_number = guard_number_p.match(line).group(1) if not patterns.get(guard_number): patterns[guard_number] = defaultdict(int) elif "asleep" in log_entry: asleep = minute_p.match(line).group(1) else: waking = minute_p.match(line).group(1) for i in range(int(asleep), int(waking)): patterns[guard_number][i] += 1 # print(log_entry, guard_number, asleep, waking) max_minutes = 0 heavy_sleepers = [] for g, p in patterns.items(): if len(p.keys()) >= max_minutes: max_minutes = len(p.keys()) for g, p in patterns.items(): if len(p.keys()) == max_minutes: heavy_sleepers.append(g) max_minute = -1 heavy_sleep_minute = -1 heavy_sleeper = -1 for hs in heavy_sleepers: for k, v in patterns.get(hs).items(): # print(v, k, hs) if v >= max_minute and k > heavy_sleep_minute: max_minute = v heavy_sleep_minute = k heavy_sleeper = hs mostest_max = -1 mostest_minute = -1 mostest_guard = -1 for k, v in patterns.items(): for kk, vv in v.items(): if vv > mostest_max: mostest_max = vv mostest_minute = kk mostest_guard = k print("{}*{} {}".format( heavy_sleeper, heavy_sleep_minute, int(heavy_sleeper) * heavy_sleep_minute, )) # 33 * 3023 # print(patterns.get(mostest_guard)) print("{}*{} {}".format(mostest_guard, mostest_minute, int(mostest_guard) * mostest_minute)) # 2719 * 36
def run(input_file): for line in process(input_file): print(line)
def run(input_file): Coord = namedtuple("Coord", ["n", "x", "y"]) coords = [] bounded = [] alpha = list(string.ascii_letters) max_value = 0 for line in process(input_file): x, y = map(int, line.split(",")) if max(x, y) > max_value: max_value = max(x, y) coords.append(Coord(alpha.pop(0), x, y)) max_value += 1 def closest_char(x, y, coords, max_value): mind = max_value * max_value char = "." for coord in coords: d = abs(coord.x - x) + abs(coord.y - y) if d == mind: char = "." elif d < mind: char = coord.n mind = d return char counts = defaultdict(int) for y in range(0, max_value): for x in range(0, max_value): char = closest_char(x, y, coords, max_value) counts[char] += 1 # check if area expands if we go outside of box infinite = set() for y in [-2, -1, max_value + 1, max_value + 2]: for x in range(0, max_value): char = closest_char(x, y, coords, max_value) infinite.add(char) for y in range(0, max_value): for x in [-2, -1, max_value + 1, max_value + 2]: char = closest_char(x, y, coords, max_value) infinite.add(char) largest_area = 0 for k, v in counts.items(): if k not in infinite and v > largest_area: largest_area = v print(largest_area) area = 0 for y in range(-10, max_value + 10): for x in range(-10, max_value + 10): sumd = 0 for coord in coords: sumd += abs(coord.x - x) + abs(coord.y - y) if sumd < 10000: area += 1 print(area)
def run(input_file): for number in process(input_file): part1(number, e1=0, e2=1, results=[3, 7]) part2(number, e1=0, e2=1, results=[3, 7])