def do(inp, line): w = line.split(" ") if w[0] == "swap": if w[1] == "position": x = int(w[2]) y = int(w[-1]) elif w[1] == "letter": x = inp.index(w[2]) y = inp.index(w[-1]) t = inp[x] inp[x] = inp[y] inp[y] = t elif w[0] == "reverse": [lo, hi] = lib.rints(line) s = inp[:lo] + inp[lo:hi + 1][::-1] + inp[hi + 1:] inp = s elif w[0] == "rotate": if "step" in line: n = lib.rints(line)[0] if w[1] == "left": s = inp[n:] + inp[:n] else: s = inp[-n:] + inp[:-n] else: n = inp.index(w[-1]) if n >= 4: n += 2 else: n += 1 n %= len(inp) s = inp[-n:] + inp[:-n] inp = s elif w[0] == "move": [x, y] = lib.rints(line) c = inp.pop(x) inp.insert(y, c) return inp
def totlen(s): i = 0 inp = False last = 0 totalen = 0 while (i < len(s)): if s[i] == '(' and not inp: inp = True last = i elif s[i] == ')' and inp: inp = False m = lib.rints(s[last:i]) if args.part == 1: totalen += m[0] * m[1] else: totalen += m[1] * totlen(s[i + 1:i + m[0] + 1]) i += m[0] elif not inp: totalen += 1 i += 1 return totalen
def solve2(line): # print(line) parens = [] match = dict() for i, c in enumerate(line): if c == "(": parens.append(i) elif c == ")": match[parens[-1]] = i parens.pop() if len(match) > 0: mini = min(match.keys()) return solve2(line[:mini] + str(solve2(line[mini + 1:match[mini]])) + line[match[mini] + 1:]) else: m = re.search(r"(\d+) \+ (\d+)", line) if not m: return reduce(lambda a, b: a * b, lib.rints(line)) else: skip = len(m.group(0)) start = line.index(m.group(0)) thissum = int(m.group(1)) + int(m.group(2)) return solve2(line[:start] + str(thissum) + line[start + skip:])
import numpy as np di = lib.di4 dj = lib.dj4 parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] XX = 30000000 if args.part == 2 else 2020 for li, line in enumerate(lines): last = defaultdict(list) nums = lib.rints(line) spoken = [-1] * XX for i in range(XX): if i < len(nums): spoken[i] = nums[i] last[spoken[i]].append(i) elif len(last[spoken[i - 1]]) == 1: spoken[i] = 0 last[0].append(i) else: spoken[i] = last[spoken[i - 1]][-1] - last[spoken[i - 1]][-2] last[spoken[i]].append(i) print(spoken[-1])
parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] rules = [] YT = False NT = False totalinv = 0 allns = [] myt = [] for li, line in enumerate(lines): if ":" in line: r = lib.rints(line) rules.append((r[0], r[1], r[2], r[3])) elif line.startswith("aa"): myt = lib.rints(line) elif line.startswith("bb"): ns = lib.rints(line) allns.append(ns) if args.part == 1: for i, ns in enumerate(allns): for n in ns: valid = False for r in rules: if r[0] <= n <= r[1] or r[2] <= n <= r[3]: valid = True if not valid:
from advent_of_code import lib parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = f.read() if args.part == 1: c = 0 for line in lines.splitlines(): if not line.strip(): continue nums = lib.rints(line) v = True for p in it.permutations(nums): if p[0] + p[1] <= p[2]: v = False if v: c += 1 print(c) else: c = 0 nums = [] for line in lines.splitlines(): if not line.strip(): continue nums.append(lib.rints(line)) for i in range(len(nums[0])):
di = lib.di4 dj = lib.dj4 parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] ranges = [] alls = [] for li, line in enumerate(lines): x = lib.rints(line) ranges.append((x[0], x[1])) alls.append((x[0], '1s')) alls.append((x[1], '2e')) MAX = 4294967295 #MAX = 9 alls = sorted(alls) last = -1 o = 0 tot = 0 for i, e in enumerate(alls): if e[1] == '1s': o += 1 if o == 1: if args.part == 1:
parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] tlo = 17 thi = 61 unprocessed = set(range(len(lines))) bots = defaultdict(set) for li, line in enumerate(lines): nums = lib.rints(line) if len(nums) == 2: bots[nums[1]].add(nums[0]) unprocessed.remove(li) outputs = defaultdict(set) while (len(unprocessed)): rest = list(unprocessed) for ri in rest: nums = lib.rints(lines[ri]) if len(bots[nums[0]]) >= 2: unprocessed.remove(ri) lo = min(bots[nums[0]]) hi = max(bots[nums[0]]) bots[nums[0]].remove(lo) bots[nums[0]].remove(hi)
import math from hashlib import md5 from advent_of_code import lib from copy import deepcopy di = lib.di4 dj = lib.dj4 parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] inp = lib.rints(lines[0])[0] if args.part == 1: z = int("0" + lib.bin1(inp)[1:], 2) print(2 * z + 1) elif args.part == 2: surv = [0] for i in range(2, inp + 1): kill = i // 2 if surv[-1] + 1 < kill: surv.append((surv[-1] + 1) % i) else: surv.append((surv[-1] + 2) % i) print(surv[-1] + 1)
import argparse import re from collections import defaultdict import itertools as it import math from hashlib import md5 from advent_of_code import lib from copy import deepcopy di = lib.di4 dj = lib.dj4 parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("part", type=int) args = parser.parse_args() f = open(args.input, "r") lines = [line for line in f.read().splitlines() if line.strip()] tot = 0 tot2 = 0 for li, line in enumerate(lines): [a, b, c] = lib.rints(line) tot += 2 * (a*b + a*c + b*c) + min(a*b, a*c, b*c) tot2 += 2 * (a + b + c - max([a, b, c])) + a*b*c print(tot) print(tot2)
for line in lines.splitlines(): if not line: continue # pgr(g) gg = deepcopy(g) x = line.split(" ") if len(x) == 2: m, n = x[1].split("x") n = int(n) m = int(m) for i in range(n): for j in range(m): gg[i][j] = True else: if x[1] == "row": i = lib.rints(x[-3])[0] dj = int(x[-1]) for j in range(mm): pj = (j + dj) % mm gg[i][pj] = g[i][j] else: j = lib.rints(x[-3])[0] di = int(x[-1]) for i in range(nn): pi = (i + di) % nn gg[pi][j] = g[i][j] g = gg #pgr(g) sol = 0 for i in range(nn):