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.

Add 2022/03

+45 -3
+34
2022/day03.py
··· 1 + import advent 2 + from utils import chunks, parts 3 + 4 + day = advent.Day(year=2022, day=3) 5 + 6 + 7 + # Part 1 8 + part_1 = 0 9 + for line in day: 10 + n = len(line) 11 + fst, snd = parts(line, 2) 12 + 13 + common = set(fst) & set(snd) 14 + 15 + for c in common: 16 + if c == c.lower(): 17 + part_1 += ord(c) - ord('a') + 1 18 + else: 19 + part_1 += ord(c) - ord('A') + 27 20 + 21 + print("Part 1:", part_1) 22 + 23 + # Part 2 24 + part_2 = 0 25 + for a, b, c in chunks(day, 3): 26 + common = set(a) & set(b) & set(c) 27 + 28 + for c in common: 29 + if c == c.lower(): 30 + part_2 += ord(c) - ord('a') + 1 31 + else: 32 + part_2 += ord(c) - ord('A') + 27 33 + 34 + print("Part 2:", part_2)
+2 -3
2022/starter.py
··· 5 5 6 6 import advent 7 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 8 + from utils import chunks, parts, gcd, lcm, print_grid, min_max_xy 9 9 from utils import new_table, transposed, rotated, firsts, lasts 10 10 from utils import md5, sha256, VOWELS, CONSONANTS 11 11 from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW ··· 20 20 tot = 0 21 21 res = [] 22 22 23 - day = advent.Day(year=2022, day=1) 24 - 23 + day = advent.Day(year=2022, day=0) 25 24
+9
2022/utils.py
··· 67 67 for i in range(0, len(l), n): 68 68 yield l[i:i + n] 69 69 70 + def parts(l, n): 71 + """Splits l into n equal parts. Excess (if it exists) returned as the n+1-th.""" 72 + m = len(l) // n 73 + for i in range(0, n): 74 + yield l[i*m:(i+1)*m] 75 + 76 + if len(l) % n != 0: 77 + yield l[m*n:] 78 + 70 79 71 80 def all_unique(lst): 72 81 """Returns True if all items in `lst` are unique."""