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/02

+131 -2
+1
.gitignore
··· 5 5 search.py 6 6 advent.py 7 7 test.py 8 + *.sh
+74
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) 21 + 22 + wins = { 23 + 'A': 'Y', 24 + 'B': 'Z', 25 + 'C': 'X', 26 + } 27 + 28 + losses = { 29 + 'B': 'X', 30 + 'C': 'Y', 31 + 'A': 'Z', 32 + } 33 + 34 + draw = { 35 + 'A': 'X', 36 + 'B': 'Y', 37 + 'C': 'Z', 38 + } 39 + 40 + score = { 41 + 'X': 1, 42 + 'Y': 2, 43 + 'Z': 3, 44 + } 45 + 46 + part_1 = 0 47 + for line in day: 48 + op, us = line.split(' ') 49 + 50 + part_1 += score[us] 51 + if wins[op] == us: 52 + part_1 += 6 53 + elif losses[op] == us: 54 + part_1 += 0 55 + else: 56 + part_1 += 3 57 + 58 + print("Part 1:", part_1) 59 + 60 + part_2 = 0 61 + for line in day: 62 + op, outcome = line.split(' ') 63 + if outcome == 'X': 64 + us = losses[op] 65 + part_2 += score[us] 66 + elif outcome == 'Y': 67 + us = draw[op] 68 + part_2 += score[us] + 3 69 + else: 70 + us = wins[op] 71 + part_2 += score[us] + 6 72 + 73 + print("Part 2:", part_2) 74 +
+2 -2
2022/starter.py
··· 8 8 from utils import chunks, 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 - from utils import Point, DIRS, DIRS_4, DIRS_8 # N (0, 1) -> E (1, 0) -> S (0, -1) -> W (-1, 0) 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 14 # permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC 15 15 # combinations('ABCD', 2) AB AC AD BC BD CD 16 16 # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 17 17 18 - # day .lines .nlines .paragraphs .board .pboard .tboard 18 + # day .lines .nlines .pars .npars .board .pboard .tboard 19 19 20 20 tot = 0 21 21 res = []
+54
2022/utils.py
··· 469 469 def neighbors_8(self): 470 470 return self.neighbours_8() 471 471 472 + N = Point(0, 1) 473 + NE = Point(1, 1) 474 + E = Point(1, 0) 475 + SE = Point(1, -1) 476 + S = Point(0, -1) 477 + SW = Point(-1, -1) 478 + W = Point(-1, 0) 479 + NW = Point(-1, 1) 472 480 473 481 DIRS_4 = DIRS = [ 474 482 Point(0, 1), # north ··· 487 495 Point(-1, 0), # W 488 496 Point(-1, 1), # NW 489 497 ] 498 + 499 + class UnionFind: 500 + """ 501 + If this comes in handy, thank you mcpower! 502 + https://www.reddit.com/r/adventofcode/comments/a9c61w/2018_day_25_solutions/eci5kaf/ 503 + """ 504 + # n: int 505 + # parents: List[Optional[int]] 506 + # ranks: List[int] 507 + # num_sets: int 508 + 509 + def __init__(self, n: int) -> None: 510 + self.n = n 511 + self.parents = [None] * n 512 + self.ranks = [1] * n 513 + self.num_sets = n 514 + 515 + def find(self, i: int) -> int: 516 + p = self.parents[i] 517 + if p is None: 518 + return i 519 + p = self.find(p) 520 + self.parents[i] = p 521 + return p 522 + 523 + def in_same_set(self, i: int, j: int) -> bool: 524 + return self.find(i) == self.find(j) 525 + 526 + def merge(self, i: int, j: int) -> None: 527 + i = self.find(i) 528 + j = self.find(j) 529 + 530 + if i == j: 531 + return 532 + 533 + i_rank = self.ranks[i] 534 + j_rank = self.ranks[j] 535 + 536 + if i_rank < j_rank: 537 + self.parents[i] = j 538 + elif i_rank > j_rank: 539 + self.parents[j] = i 540 + else: 541 + self.parents[j] = i 542 + self.ranks[i] += 1 543 + self.num_sets -= 1