ns = [int(n) for n in text.splitlines()] | |||||
ans1 = sum(b > a for a, b in zip(ns, ns[1:])) | |||||
ans2 = sum(sum(w[:-1]) < sum(w[1:]) for w in zip(ns, ns[1:], ns[2:], ns[3:])) | |||||
ns = [int(n) for n in open(0)] | |||||
print(sum(b > a for a, b in zip(ns, ns[1:]))) | |||||
print(sum(sum(w[:-1]) < sum(w[1:]) for w in zip(ns, ns[1:], ns[2:], ns[3:]))) |
s1 = 0 | s1 = 0 | ||||
s2, aim = 0, 0 | s2, aim = 0, 0 | ||||
for line in text.splitlines(): | |||||
for line in open(0): | |||||
k, v = line.split() | k, v = line.split() | ||||
ds = {'forward': 1, 'down': 1j, 'up': -1j}[k] * int(v) | ds = {'forward': 1, 'down': 1j, 'up': -1j}[k] * int(v) | ||||
s1 += ds | s1 += ds | ||||
aim += ds.imag | aim += ds.imag | ||||
s2 += ds.real + aim * ds.real * 1j | s2 += ds.real + aim * ds.real * 1j | ||||
ans1 = int(s1.real * s1.imag) | |||||
ans2 = int(s2.real * s2.imag) | |||||
print(int(s1.real * s1.imag)) | |||||
print(int(s2.real * s2.imag)) |
from collections import Counter | from collections import Counter | ||||
nums = open(0).read().splitlines() | |||||
app = eps = '' | app = eps = '' | ||||
for col in zip(*text.splitlines()): | |||||
for col in zip(*nums): | |||||
n, *_, m = Counter(col).most_common() | n, *_, m = Counter(col).most_common() | ||||
app += n[0] | app += n[0] | ||||
eps += m[0] | eps += m[0] | ||||
ans1 = int(app, 2) * int(eps, 2) | |||||
print(int(app, 2) * int(eps, 2)) | |||||
nums = text.splitlines() | |||||
xs = nums[:] | xs = nums[:] | ||||
ys = nums[:] | ys = nums[:] | ||||
for idx in range(len(nums[0])): | for idx in range(len(nums[0])): | ||||
xs = [x for x in xs if x[idx] == n] | xs = [x for x in xs if x[idx] == n] | ||||
m, _ = Counter(sorted([y[idx] for y in ys])).most_common()[::-1][-1] | m, _ = Counter(sorted([y[idx] for y in ys])).most_common()[::-1][-1] | ||||
ys = [y for y in ys if y[idx] == m] | ys = [y for y in ys if y[idx] == m] | ||||
ans2 = int(xs[0], 2) * int(ys[0], 2) | |||||
print(int(xs[0], 2) * int(ys[0], 2)) |
A, *B = text.split('\n\n') | |||||
A, *B = open(0).read().split('\n\n') | |||||
calls = [int(n) for n in A.split(',')] | calls = [int(n) for n in A.split(',')] | ||||
boards = [[[int(n) for n in ln.split()] for ln in b.splitlines()] for b in B] | boards = [[[int(n) for n in ln.split()] for ln in b.splitlines()] for b in B] | ||||
pending.append(board) | pending.append(board) | ||||
boards = pending | boards = pending | ||||
ans1 = win_scores[0] | |||||
ans2 = win_scores[-1] | |||||
print(win_scores[0]) | |||||
print(win_scores[-1]) |
import re | |||||
from collections import Counter | from collections import Counter | ||||
from itertools import cycle | from itertools import cycle | ||||
from math import copysign | from math import copysign | ||||
sign = lambda n: int(copysign(1, n)) | sign = lambda n: int(copysign(1, n)) | ||||
points = {'flat': Counter(), 'diag': Counter()} | points = {'flat': Counter(), 'diag': Counter()} | ||||
for line in text.splitlines(): | |||||
for line in open(0): | |||||
x1, y1, x2, y2 = [int(n) for n in re.findall(r'\d+', line)] | x1, y1, x2, y2 = [int(n) for n in re.findall(r'\d+', line)] | ||||
xs = [*range(x1, x2, sign(x2 - x1)), x2] | xs = [*range(x1, x2, sign(x2 - x1)), x2] | ||||
ys = [*range(y1, y2, sign(y2 - y1)), y2] | ys = [*range(y1, y2, sign(y2 - y1)), y2] | ||||
points['flat'].update(zip(xs, cycle(ys))) | points['flat'].update(zip(xs, cycle(ys))) | ||||
else: | else: | ||||
points['diag'].update(zip(xs, ys)) | points['diag'].update(zip(xs, ys)) | ||||
ans1 = sum(v > 1 for v in (points['flat']).values()) | |||||
ans2 = sum(v > 1 for v in (points['flat'] + points['diag']).values()) | |||||
print(sum(v > 1 for v in (points['flat']).values())) | |||||
print(sum(v > 1 for v in (points['flat'] + points['diag']).values())) |
states = [[0, 0, 0, 0, 0, 0, 0, 0, 0]] | states = [[0, 0, 0, 0, 0, 0, 0, 0, 0]] | ||||
for n in text.split(','): | |||||
for n in open(0).read().split(','): | |||||
states[0][int(n)] += 1 | states[0][int(n)] += 1 | ||||
for day in range(256): | for day in range(256): | ||||
states.append(states[-1][1:] + states[-1][:1]) | states.append(states[-1][1:] + states[-1][:1]) | ||||
states[-1][6] += states[-2][0] | states[-1][6] += states[-2][0] | ||||
ans1 = sum(states[80]) | |||||
ans2 = sum(states[256]) | |||||
print(sum(states[80])) | |||||
print(sum(states[256])) |
ns = [int(n) for n in text.split(',')] | |||||
ns = [int(n) for n in open(0).read().split(',')] | |||||
lo, *_, hi = sorted(ns) | lo, *_, hi = sorted(ns) | ||||
def mono_min(g): | def mono_min(g): | ||||
if x > y: return y | if x > y: return y | ||||
y = x | y = x | ||||
ans1 = mono_min(sum(abs(n - m) for m in ns) for n in range(lo, hi)) | |||||
print(mono_min(sum(abs(n - m) for m in ns) for n in range(lo, hi))) | |||||
range_sum = lambda n: int(n * (n + 1) / 2) | range_sum = lambda n: int(n * (n + 1) / 2) | ||||
ans2 = mono_min(sum(range_sum(abs(n - m)) for m in ns) for n in range(lo, hi)) | |||||
print(mono_min(sum(range_sum(abs(n - m)) for m in ns) for n in range(lo, hi))) |
import re | |||||
ans1 = ans2 = 0 | ans1 = ans2 = 0 | ||||
for line in text.splitlines(): | |||||
for line in open(0): | |||||
seqs = [frozenset(seq) for seq in re.findall(r'\w+', line)] | seqs = [frozenset(seq) for seq in re.findall(r'\w+', line)] | ||||
_1,_7,_4, *pending,_8 = sorted(set(seqs), key=len) | _1,_7,_4, *pending,_8 = sorted(set(seqs), key=len) | ||||
sorter = lambda x: [len(x &_8), len(x &_4), len(x &_1)] | sorter = lambda x: [len(x &_8), len(x &_4), len(x &_1)] | ||||
ns = [_0,_1,_2,_3,_4,_5,_6,_7,_8,_9] | ns = [_0,_1,_2,_3,_4,_5,_6,_7,_8,_9] | ||||
ans1 += sum(x in {_1, _7, _4, _8} for x in seqs[-4:]) | ans1 += sum(x in {_1, _7, _4, _8} for x in seqs[-4:]) | ||||
ans2 += int(''.join(str(ns.index(x)) for x in seqs[-4:])) | ans2 += int(''.join(str(ns.index(x)) for x in seqs[-4:])) | ||||
print(ans1) | |||||
print(ans2) |
from toolkit import read_image | |||||
grid = {} | |||||
for y, line in enumerate(open(0)): | |||||
for x, char in enumerate(line[:-1]): | |||||
grid[complex(x, y)] = char | |||||
ans1 = 0 | ans1 = 0 | ||||
grid = read_image(text)[0] | |||||
for p, v in grid.items(): | for p, v in grid.items(): | ||||
if all(grid[p + s] > v for s in [1, -1, 1j, -1j] if p + s in grid): | if all(grid[p + s] > v for s in [1, -1, 1j, -1j] if p + s in grid): | ||||
ans1 += int(v) + 1 | ans1 += int(v) + 1 | ||||
print(ans1) | |||||
valid = {k for k, v in grid.items() if v != '9'} | valid = {k for k, v in grid.items() if v != '9'} | ||||
pending = valid.copy() | pending = valid.copy() | ||||
pending -= seen | pending -= seen | ||||
basin_sizes.append(len(seen)) | basin_sizes.append(len(seen)) | ||||
*_, x, y, z = sorted(basin_sizes) | *_, x, y, z = sorted(basin_sizes) | ||||
ans2 = x * y * z | |||||
print(x * y * z) |
val = lambda c: objs.index(c) % 2 | val = lambda c: objs.index(c) % 2 | ||||
ans1 = 0 | ans1 = 0 | ||||
scores = [] | scores = [] | ||||
for ln in text.splitlines(): | |||||
for ln in open(0).read().splitlines(): | |||||
stack = [] | stack = [] | ||||
for c in ln: | for c in ln: | ||||
if val(c) == 0: | if val(c) == 0: | ||||
score += fam(stack.pop()) + 1 | score += fam(stack.pop()) + 1 | ||||
scores.append(score) | scores.append(score) | ||||
ans2 = sorted(scores)[len(scores) // 2] | ans2 = sorted(scores)[len(scores) // 2] | ||||
print(ans1) | |||||
print(ans2) |
import itertools | import itertools | ||||
import toolkit | |||||
state = {k: int(v) for k, v in toolkit.read_image(text)[0].items()} | |||||
D8 = [dx + dy for dx in [1, 0, -1] for dy in [1j, 0, -1j] if dx + dy] | |||||
def flash(k, flashed_this_round): | def flash(k, flashed_this_round): | ||||
state[p] += 1 | state[p] += 1 | ||||
state = {} | |||||
for y, line in enumerate(open(0)): | |||||
for x, char in enumerate(line[:-1]): | |||||
state[complex(x, y)] = int(char) | |||||
D8 = [dx + dy for dx in [1, 0, -1] for dy in [1j, 0, -1j] if dx + dy] | |||||
ans1 = 0 | ans1 = 0 | ||||
for i in itertools.count(1): | for i in itertools.count(1): | ||||
for k in state: | for k in state: | ||||
if len({v for v in state.values()}) == 1: | if len({v for v in state.values()}) == 1: | ||||
ans2 = i | ans2 = i | ||||
break | break | ||||
print(ans1) | |||||
print(ans2) |
graph = defaultdict(set) | graph = defaultdict(set) | ||||
for ln in text.splitlines(): | |||||
a, b = ln.split('-') | |||||
for ln in open(0): | |||||
a, b = ln[:-1].split('-') | |||||
graph[a].add(b) | graph[a].add(b) | ||||
graph[b].add(a) | graph[b].add(a) | ||||
graph = dict(graph) | graph = dict(graph) | ||||
ans1 = solve(graph) | |||||
ans2 = solve(graph, True) | |||||
print(solve(graph)) | |||||
print(solve(graph, True)) |
import toolkit | |||||
dots, instructions = text.split('\n\n') | |||||
dots, instructions = open(0).read().split('\n\n') | |||||
dots = {eval(pair) for pair in dots.splitlines()} | dots = {eval(pair) for pair in dots.splitlines()} | ||||
ans1 = None | ans1 = None | ||||
for line in instructions.splitlines(): | for line in instructions.splitlines(): | ||||
dots = {(x, y) if y < zz else (x, 2 * zz - y) for x, y in dots} | dots = {(x, y) if y < zz else (x, 2 * zz - y) for x, y in dots} | ||||
if ans1 is None: | if ans1 is None: | ||||
ans1 = len(dots) | ans1 = len(dots) | ||||
ans2 = '\n' + toolkit.render({complex(*k): '#' for k in dots}) | |||||
x, *_, X = sorted(x for x, y in dots) | |||||
y, *_, Y = sorted(y for x, y in dots) | |||||
ans2 = '' | |||||
for yi in range(y, Y + 1): | |||||
for xi in range(x, X + 1): | |||||
ans2 += '#' if (xi, yi) in dots else ' ' | |||||
ans2 += '\n' | |||||
print(ans1) | |||||
print(ans2) |
def valid_z1s(z2, w, c1, c2, c3): | def valid_z1s(z2, w, c1, c2, c3): | ||||
valid = {(z2 - w - c3) // 26} | {(z2 * 26 + i) for i in range(26)} | valid = {(z2 - w - c3) // 26} | {(z2 * 26 + i) for i in range(26)} | ||||
for z1 in valid: | for z1 in valid: | ||||
if z1 < 26 ** exp: | |||||
if evolve(z1, w, c1, c2, c3) == z2: | |||||
yield z1 | |||||
if evolve(z1, w, c1, c2, c3) == z2: | |||||
yield z1 | |||||
def evolve(z, w, c1, c2, c3): | def evolve(z, w, c1, c2, c3): | ||||
return (z // c1) if (z % 26 + c2 == w) else (z * 26 + w + c3) | return (z // c1) if (z % 26 + c2 == w) else (z * 26 + w + c3) | ||||
consts = [ | |||||
(int(els[4]), int(els[5]), int(els[15])) | |||||
for block in open(0).read().split('inp w')[1:] | |||||
for els in [[ln.split()[-1] for ln in block.splitlines()]] | |||||
] | |||||
consts = [] | |||||
for block in open(0).read().split('inp w\n')[1:]: | |||||
els = [ln.split()[-1] for ln in block.splitlines()] | |||||
consts.append((int(els[3]), int(els[4]), int(els[14]))) | |||||
valid_zs = {0: {0}} | |||||
for c1, c2, c3 in consts[:7]: | |||||
valid_zs[len(valid_zs)] = { | |||||
evolve(z, w, c1, c2, c3) | |||||
for z in valid_zs[len(valid_zs) - 1] | |||||
for w in map(int, '123456789') | |||||
} | |||||
state = [('', 0)] | state = [('', 0)] | ||||
for exp, (c1, c2, c3) in list(enumerate(consts, 1))[::-1]: | |||||
print(exp, len(state)) | |||||
idx = 14 | |||||
for c1, c2, c3 in consts[::-1]: | |||||
idx -= 1 | |||||
state = [ | state = [ | ||||
(w + s, z1) | (w + s, z1) | ||||
for s, z2 in state | for s, z2 in state | ||||
for w in '123456789' | for w in '123456789' | ||||
for z1 in valid_z1s(z2, int(w), c1, c2, c3) | for z1 in valid_z1s(z2, int(w), c1, c2, c3) | ||||
if idx not in valid_zs or z1 in valid_zs[idx] | |||||
] | ] | ||||
(head, _), *body, (tail, _) = sorted(state) | (head, _), *body, (tail, _) = sorted(state) |
hashable = frozenset(state.items()) | hashable = frozenset(state.items()) | ||||
if hashable in seen: | if hashable in seen: | ||||
print(turn) | |||||
print(turn + 1) | |||||
break | break | ||||
seen.add(hashable) | seen.add(hashable) |