def main(): input_url = 'https://adventofcode.com/2018/day/1/input' drift_input = get_from_url(input_url) def _parse_int(x): if not x: return None elif x[0] == '+': return int(x[1:]) else: return int(x) data_as_list = [ _parse_int(di) for di in drift_input.split('\n') if _parse_int(di) is not None ] list_as_cycle = cycle(data_as_list) total_drift = 0 ctr = Counter() ctr[total_drift] += 1 while True: inp = next(list_as_cycle) total_drift += inp ctr[total_drift] += 1 if ctr[total_drift] > 1: break freq, change = ctr.most_common(1)[0] print('Freq: {}\nChange: {}'.format(freq, change))
def main(): input_url = 'https://adventofcode.com/2018/day/6/input' coords_input = get_from_url(input_url) x_vals = [] y_vals = [] coords = [] def man_distance(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) for raw_coord in coords_input.strip().split('\n'): x, y = [int(val) for val in raw_coord.split(', ')] x_vals.append(x) y_vals.append(y) coords.append(Coord(x, y)) max_x = max(x_vals) max_y = max(y_vals) count = 0 for y in range(max_y + 1): for x in range(max_x + 1): if (sum(man_distance(x, y, coord.x, coord.y) for coord in coords) < 10000): count += 1 print(count)
def main(): input_url = 'https://adventofcode.com/2018/day/2/input' boxes = get_from_url(input_url) two_count = 0 three_count = 0 for box_input in boxes.split('\n'): three_found = False two_found = False for letter, count in Counter(box_input).most_common(): if not three_found and count == 3: three_found = True three_count += 1 elif not two_found and count == 2: two_found = True two_count += 1 elif count < 2: break print('two_count: {}'.format(two_count)) print('three_count: {}'.format(three_count)) print(two_count * three_count)
def main(): input_url = 'https://adventofcode.com/2018/day/5/input' polymer_input = get_from_url(input_url).strip() print('Length at start: {}'.format(len(polymer_input))) def _reduce_polymer(polymer): idx = 0 while idx < len(polymer): char = polymer[idx] if idx == 0: prev_char = polymer[0] idx += 1 continue else: prev_char = polymer[idx - 1] if not isinstance(char, str): raise Exception('Not a letter! {}'.format(char)) if char.lower() == prev_char.lower() and char != prev_char: print('Removing {}{}'.format(polymer[idx - 1], polymer[idx])) polymer = polymer[:idx - 1] + polymer[idx + 1:] # Minus two for deletions and one more to account for incrementing below idx = idx - 3 prev_char = char idx += 1 return polymer final_polymer = _reduce_polymer(polymer_input) print(len(final_polymer))
def main(): input_url = 'https://adventofcode.com/2018/day/3/input' fabric_claims = get_from_url(input_url) max_x = 1000 max_y = 1000 matrix = [[0 for m_x in range(max_x)] for m_y in range(max_y)] for fabric_claim in fabric_claims.split('\n'): if fabric_claim == '': continue parts = fabric_claim.split(' @ ') claim_id = parts[0] parts_two = parts[1].split(': ') x, y = parts_two[0].split(',') width, length = parts_two[1].split('x') x = int(x) y = int(y) x_2 = x + int(width) y_2 = y + int(length) # Populate matrix for xs in range(x, x_2): for ys in range(y, y_2): matrix[xs][ys] += 1 for fabric_claim in fabric_claims.split('\n'): if fabric_claim == '': continue parts = fabric_claim.split(' @ ') claim_id = parts[0] parts_two = parts[1].split(': ') x, y = parts_two[0].split(',') width, length = parts_two[1].split('x') x = int(x) y = int(y) x_2 = x + int(width) y_2 = y + int(length) no_conflict = True for xs in range(x, x_2): for ys in range(y, y_2): if matrix[xs][ys] > 1: no_conflict = False if no_conflict: no_conflict_id = claim_id print(no_conflict_id)
def main(): input_url = 'https://adventofcode.com/2018/day/3/input' fabric_claims = get_from_url(input_url) max_x = 1000 max_y = 1000 for fabric_claim in fabric_claims.split('\n'): if fabric_claim == '': continue parts = fabric_claim.split(' @ ') parts_two = parts[1].split(': ') x, y = parts_two[0].split(',') width, length = parts_two[1].split('x') x_2 = int(x) + int(width) y_2 = int(y) + int(length) if x_2 > max_x: max_x = x_2 if y_2 > max_y: max_y = y_2 matrix = [[0 for m_x in range(max_x)] for m_y in range(max_y)] conflict_count = 0 for fabric_claim in fabric_claims.split('\n'): if fabric_claim == '': continue parts = fabric_claim.split(' @ ') parts_two = parts[1].split(': ') x, y = parts_two[0].split(',') width, length = parts_two[1].split('x') x = int(x) y = int(y) x_2 = x + int(width) y_2 = y + int(length) for xs in range(x, x_2): for ys in range(y, y_2): matrix[xs][ys] += 1 # If it's the first conflict for the coordinate if matrix[xs][ys] == 2: conflict_count += 1 print(conflict_count)
def main(): input_url = 'https://adventofcode.com/2018/day/1/input' drift_input = get_from_url(input_url) def _parse_int(x): if not x: return 0 elif x[0] == '+': return int(x[1:]) else: return int(x) total_drift = sum(map(_parse_int, drift_input.split('\n'))) print(total_drift)
def main(): input_url = 'https://adventofcode.com/2018/day/5/input' polymer_input = get_from_url(input_url).strip() def _reduce_polymer(polymer): idx = 0 while idx < len(polymer): char = polymer[idx] if idx == 0: prev_char = polymer[0] idx += 1 continue else: prev_char = polymer[idx - 1] if not isinstance(char, str): raise Exception('Not a letter! {}'.format(char)) if char.lower() == prev_char.lower() and char != prev_char: polymer = polymer[:idx - 1] + polymer[idx + 1:] # Minus two for deletions and one more to account for incrementing below idx = idx - 3 if idx < 0: idx = 0 prev_char = char idx += 1 return polymer all_units = set([char.lower() for char in polymer_input]) lengths = {u: 0 for u in all_units} for unit in all_units: print('Removing unit: {}'.format(unit)) polymer = polymer_input.replace(unit, '').replace(unit.upper(), '') print('Length at start: {}'.format(len(polymer))) final_polymer = _reduce_polymer(polymer) print(len(final_polymer)) lengths[unit] = len(final_polymer) shortest = min([v for k, v in lengths.items() if v > 0]) print('Smallest length: {}'.format(shortest))
def main(): input_url = 'https://adventofcode.com/2018/day/6/input' coords_input = get_from_url(input_url) x_vals = [] y_vals = [] coords = [] def man_distance(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) for raw_coord in coords_input.strip().split('\n'): x, y = [int(val) for val in raw_coord.split(', ')] x_vals.append(x) y_vals.append(y) coords.append(Coord(x, y)) max_x = max(x_vals) max_y = max(y_vals) cell_counts = defaultdict(int) infinites = set() for y in range(max_y + 1): for x in range(max_x + 1): distances = sorted((man_distance(x, y, px, py), i) for i, (px, py) in enumerate(coords)) if distances[0][0] != distances[1][0]: cell_counts[distances[0][1]] += 1 if x == 0 or y == 0 or x == max_x or y == max_y: infinites.add(distances[0][1]) for k in infinites: cell_counts.pop(k) print(max(cell_counts.values()))
def main(): input_url = 'https://adventofcode.com/2018/day/2/input' boxes = get_from_url(input_url) for base_idx, inp in enumerate(boxes.split('\n')): for idx, compare_inp in enumerate(boxes.split('\n')): mismatch_count = 0 mismatch_idx = None if base_idx != idx: for new_idx, (char_1, char_2) in enumerate(zip(inp, compare_inp)): if char_1 != char_2: mismatch_count += 1 mismatch_idx = new_idx if mismatch_count > 1: break if new_idx + 1 == len(compare_inp) and mismatch_count == 1: answer = [] for answer_idx, char in enumerate(inp): if answer_idx != mismatch_idx: answer.append(char) print(''.join(answer)) exit()