My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remove ✨ magic ✨ from 2022 solutions

+24 -33
+12 -3
2022/day01.py
··· 1 - import advent 1 + import fileinput 2 + 3 + elves = [] 4 + elf = 0 5 + for line in fileinput.input(): 6 + if line.strip(): 7 + elf += int(line) 8 + else: 9 + # New line -> new elf. 10 + elves.append(elf) 11 + elf = 0 2 12 3 - day = advent.Day(year=2022, day=1) 4 - elves = [sum(e) for e in day.nparagraphs] 13 + elves.append(elf) 5 14 6 15 print("Part 1:", max(elves)) 7 16 print("Part 2:", sum(sorted(elves)[-3:]))
+5 -22
2022/day02.py
··· 1 - import os, sys, re, math, copy, fileinput 2 - from string import ascii_uppercase, ascii_lowercase 3 - from collections import Counter, defaultdict, deque, namedtuple 4 - from itertools import count, product, permutations, combinations, combinations_with_replacement 5 - 6 - import advent 7 - from utils import parse_line, parse_nums, mul, all_unique, factors, memoize, primes, resolve_mapping 8 - from utils import chunks, gcd, lcm, print_grid, min_max_xy 9 - from utils import new_table, transposed, rotated, firsts, lasts 10 - from utils import md5, sha256, VOWELS, CONSONANTS 11 - from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW 12 - # Itertools Functions: 13 - # product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD 14 - # permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC 15 - # combinations('ABCD', 2) AB AC AD BC BD CD 16 - # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 17 - 18 - # day .lines .nlines .pars .npars .board .pboard .tboard 19 - 20 - day = advent.Day(year=2022, day=2) 1 + import fileinput 21 2 22 3 wins = { 23 4 'A': 'Y', ··· 43 24 'Z': 3, 44 25 } 45 26 27 + INPUT = [line.strip() for line in fileinput.input()] 28 + 46 29 part_1 = 0 47 - for line in day: 30 + for line in INPUT: 48 31 op, us = line.split(' ') 49 32 50 33 part_1 += score[us] ··· 58 41 print("Part 1:", part_1) 59 42 60 43 part_2 = 0 61 - for line in day: 44 + for line in INPUT: 62 45 op, outcome = line.split(' ') 63 46 if outcome == 'X': 64 47 us = losses[op]
+4 -5
2022/day03.py
··· 1 - import advent 1 + import fileinput 2 2 from utils import chunks, parts 3 3 4 - day = advent.Day(year=2022, day=3) 5 - 4 + INPUT = [line.strip() for line in fileinput.input()] 6 5 7 6 # Part 1 8 7 part_1 = 0 9 - for line in day: 8 + for line in INPUT: 10 9 n = len(line) 11 10 fst, snd = parts(line, 2) 12 11 ··· 22 21 23 22 # Part 2 24 23 part_2 = 0 25 - for a, b, c in chunks(day, 3): 24 + for a, b, c in chunks(INPUT, 3): 26 25 common = set(a) & set(b) & set(c) 27 26 28 27 for c in common:
+3 -3
2022/starter.py
··· 11 11 from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW 12 12 # Itertools Functions: 13 13 # product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD 14 - # permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC 15 - # combinations('ABCD', 2) AB AC AD BC BD CD 16 - # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 14 + # permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC 15 + # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 16 + # combinations('ABCD', 2) AB AC AD BC BD CD 17 17 18 18 # day .lines .nlines .pars .npars .board .pboard .tboard 19 19