for i, row in enumerate(fabric[top:top+height]): for j, col in enumerate(row[left:left+width]): fabric[top+i][left+j].append(cid) return len([e for e in chain(*fabric) if len(e) >= 2]) def part2(data: str): fabric = list([list() for i in range(1000)] for j in range(1000)) claims = {} for claim in data.strip().split('\n'): p = re.compile("#(\d+) @ (\d+),(\d+): (\d+)x(\d+)") m = p.match(claim) cid, left, top, width, height = (int(e) for e in m.groups()) claims[cid] = width * height for i, row in enumerate(fabric[top:top+height]): for j, col in enumerate(row[left:left+width]): fabric[top+i][left+j].append(cid) for k, c in Counter(chain(*(l for l in chain(*fabric) if len(l) == 1))).items(): if claims[k] == c: return k if __name__ == '__main__': data = aoc.data(day=3) print("part 1:", part1(data)) print("part 2:", part2(data))
from code import aoc from code.utils import * def part1(data: str): return sum(int(e.replace('+', '')) for e in data.strip().split()) def part2(data: str): digits = [int(e.replace('+', '')) for e in data.strip().split()] i = 0 past_freq = set() freq = 0 while True: past_freq.add(freq) freq += digits[i] if freq in past_freq: break i = (i + 1) % len(digits) return freq if __name__ == '__main__': data = aoc.data(day=1) print("part 1:", part1(data)) print("part 2:", part2(data))
from code import aoc from code.utils import * def part1(data: str): pass def part2(data: str): pass if __name__ == '__main__': data = aoc.data(day=0) print("part 1:", part1(data)) print("part 2:", part2(data))
guard_log.append({'id': guard_id, 'asleep': mins_asleep}) continue return [{ 'id': int(k), 'asleep': list(chain.from_iterable(e.get('asleep') for e in grp)) } for k, grp in groupby(sorted(guard_log, key=lambda e: e.get('id')), key=lambda e: e.get('id'))] def part1(data: str): guards = parse_log(data) sleepy_guard = max(guards, key=lambda e: len(e.get('asleep'))) hours = sleepy_guard.get('asleep') return sleepy_guard.get('id') * max(hours, key=hours.count) def part2(data: str): guards = parse_log(data) modes = [(guard.get('id'), *Counter(guard.get('asleep')).most_common(1)[0]) for guard in guards] predictable = max(modes, key=lambda e: e[2]) return predictable[0] * predictable[1] if __name__ == '__main__': data = aoc.data(day=4) print("part 1:", part1(data)) print("part 2:", part2(data))
def part1(data: str): twos, threes = 0, 0 for id in data.strip().split(): groups = [list(g) for k, g in groupby(sorted(id))] if [e for e in groups if len(e) == 2]: twos += 1 if [e for e in groups if len(e) == 3]: threes += 1 return twos * threes def part2(data: str): ids = data.strip().split() common = [] for i1, id1 in enumerate(ids): for i2, id2 in enumerate(ids): if i1 == i2: continue common.append(cat(ch for i, ch in enumerate(id1) if id2[i] == ch)) return max(common, key=len) if __name__ == '__main__': data = aoc.data(day=2) print("part 1:", part1(data)) print("part 2:", part2(data))