···11+import os, sys, re, math, copy, fileinput
22+from string import ascii_uppercase, ascii_lowercase
33+from collections import Counter, defaultdict, deque, namedtuple
44+from itertools import count, product, permutations, combinations, combinations_with_replacement
55+66+import advent
77+from utils import parse_line, parse_nums, mul, all_unique, factors, memoize, primes, resolve_mapping
88+from utils import chunks, gcd, lcm, print_grid, min_max_xy
99+from utils import new_table, transposed, rotated, firsts, lasts
1010+from utils import md5, sha256, VOWELS, CONSONANTS
1111+from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW
1212+# Itertools Functions:
1313+# product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
1414+# permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC
1515+# combinations('ABCD', 2) AB AC AD BC BD CD
1616+# combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
1717+1818+# day .lines .nlines .pars .npars .board .pboard .tboard
1919+2020+day = advent.Day(year=2022, day=2)
2121+2222+wins = {
2323+ 'A': 'Y',
2424+ 'B': 'Z',
2525+ 'C': 'X',
2626+}
2727+2828+losses = {
2929+ 'B': 'X',
3030+ 'C': 'Y',
3131+ 'A': 'Z',
3232+}
3333+3434+draw = {
3535+ 'A': 'X',
3636+ 'B': 'Y',
3737+ 'C': 'Z',
3838+}
3939+4040+score = {
4141+ 'X': 1,
4242+ 'Y': 2,
4343+ 'Z': 3,
4444+}
4545+4646+part_1 = 0
4747+for line in day:
4848+ op, us = line.split(' ')
4949+5050+ part_1 += score[us]
5151+ if wins[op] == us:
5252+ part_1 += 6
5353+ elif losses[op] == us:
5454+ part_1 += 0
5555+ else:
5656+ part_1 += 3
5757+5858+print("Part 1:", part_1)
5959+6060+part_2 = 0
6161+for line in day:
6262+ op, outcome = line.split(' ')
6363+ if outcome == 'X':
6464+ us = losses[op]
6565+ part_2 += score[us]
6666+ elif outcome == 'Y':
6767+ us = draw[op]
6868+ part_2 += score[us] + 3
6969+ else:
7070+ us = wins[op]
7171+ part_2 += score[us] + 6
7272+7373+print("Part 2:", part_2)
7474+
+2-2
2022/starter.py
···88from utils import chunks, gcd, lcm, print_grid, min_max_xy
99from utils import new_table, transposed, rotated, firsts, lasts
1010from utils import md5, sha256, VOWELS, CONSONANTS
1111-from utils import Point, DIRS, DIRS_4, DIRS_8 # N (0, 1) -> E (1, 0) -> S (0, -1) -> W (-1, 0)
1111+from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW
1212# Itertools Functions:
1313# product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
1414# permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC
1515# combinations('ABCD', 2) AB AC AD BC BD CD
1616# combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
17171818-# day .lines .nlines .paragraphs .board .pboard .tboard
1818+# day .lines .nlines .pars .npars .board .pboard .tboard
19192020tot = 0
2121res = []
+54
2022/utils.py
···469469 def neighbors_8(self):
470470 return self.neighbours_8()
471471472472+N = Point(0, 1)
473473+NE = Point(1, 1)
474474+E = Point(1, 0)
475475+SE = Point(1, -1)
476476+S = Point(0, -1)
477477+SW = Point(-1, -1)
478478+W = Point(-1, 0)
479479+NW = Point(-1, 1)
472480473481DIRS_4 = DIRS = [
474482 Point(0, 1), # north
···487495 Point(-1, 0), # W
488496 Point(-1, 1), # NW
489497]
498498+499499+class UnionFind:
500500+ """
501501+ If this comes in handy, thank you mcpower!
502502+ https://www.reddit.com/r/adventofcode/comments/a9c61w/2018_day_25_solutions/eci5kaf/
503503+ """
504504+ # n: int
505505+ # parents: List[Optional[int]]
506506+ # ranks: List[int]
507507+ # num_sets: int
508508+509509+ def __init__(self, n: int) -> None:
510510+ self.n = n
511511+ self.parents = [None] * n
512512+ self.ranks = [1] * n
513513+ self.num_sets = n
514514+515515+ def find(self, i: int) -> int:
516516+ p = self.parents[i]
517517+ if p is None:
518518+ return i
519519+ p = self.find(p)
520520+ self.parents[i] = p
521521+ return p
522522+523523+ def in_same_set(self, i: int, j: int) -> bool:
524524+ return self.find(i) == self.find(j)
525525+526526+ def merge(self, i: int, j: int) -> None:
527527+ i = self.find(i)
528528+ j = self.find(j)
529529+530530+ if i == j:
531531+ return
532532+533533+ i_rank = self.ranks[i]
534534+ j_rank = self.ranks[j]
535535+536536+ if i_rank < j_rank:
537537+ self.parents[i] = j
538538+ elif i_rank > j_rank:
539539+ self.parents[j] = i
540540+ else:
541541+ self.parents[j] = i
542542+ self.ranks[i] += 1
543543+ self.num_sets -= 1