@@ -1,3 +1,3 @@ | |||
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:]))) |
@@ -1,10 +1,10 @@ | |||
s1 = 0 | |||
s2, aim = 0, 0 | |||
for line in text.splitlines(): | |||
for line in open(0): | |||
k, v = line.split() | |||
ds = {'forward': 1, 'down': 1j, 'up': -1j}[k] * int(v) | |||
s1 += ds | |||
aim += ds.imag | |||
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)) |
@@ -1,14 +1,15 @@ | |||
from collections import Counter | |||
nums = open(0).read().splitlines() | |||
app = eps = '' | |||
for col in zip(*text.splitlines()): | |||
for col in zip(*nums): | |||
n, *_, m = Counter(col).most_common() | |||
app += n[0] | |||
eps += m[0] | |||
ans1 = int(app, 2) * int(eps, 2) | |||
print(int(app, 2) * int(eps, 2)) | |||
nums = text.splitlines() | |||
xs = nums[:] | |||
ys = nums[:] | |||
for idx in range(len(nums[0])): | |||
@@ -16,4 +17,4 @@ for idx in range(len(nums[0])): | |||
xs = [x for x in xs if x[idx] == n] | |||
m, _ = Counter(sorted([y[idx] for y in ys])).most_common()[::-1][-1] | |||
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)) |
@@ -1,4 +1,4 @@ | |||
A, *B = text.split('\n\n') | |||
A, *B = open(0).read().split('\n\n') | |||
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] | |||
@@ -16,5 +16,5 @@ for n in calls: | |||
pending.append(board) | |||
boards = pending | |||
ans1 = win_scores[0] | |||
ans2 = win_scores[-1] | |||
print(win_scores[0]) | |||
print(win_scores[-1]) |
@@ -1,3 +1,4 @@ | |||
import re | |||
from collections import Counter | |||
from itertools import cycle | |||
from math import copysign | |||
@@ -5,7 +6,7 @@ from math import copysign | |||
sign = lambda n: int(copysign(1, n)) | |||
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)] | |||
xs = [*range(x1, x2, sign(x2 - x1)), x2] | |||
ys = [*range(y1, y2, sign(y2 - y1)), y2] | |||
@@ -15,5 +16,5 @@ for line in text.splitlines(): | |||
points['flat'].update(zip(xs, cycle(ys))) | |||
else: | |||
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())) |
@@ -1,10 +1,10 @@ | |||
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 | |||
for day in range(256): | |||
states.append(states[-1][1:] + states[-1][:1]) | |||
states[-1][6] += states[-2][0] | |||
ans1 = sum(states[80]) | |||
ans2 = sum(states[256]) | |||
print(sum(states[80])) | |||
print(sum(states[256])) |
@@ -1,4 +1,4 @@ | |||
ns = [int(n) for n in text.split(',')] | |||
ns = [int(n) for n in open(0).read().split(',')] | |||
lo, *_, hi = sorted(ns) | |||
def mono_min(g): | |||
@@ -7,6 +7,6 @@ def mono_min(g): | |||
if x > y: return y | |||
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) | |||
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))) |
@@ -1,5 +1,8 @@ | |||
import re | |||
ans1 = ans2 = 0 | |||
for line in text.splitlines(): | |||
for line in open(0): | |||
seqs = [frozenset(seq) for seq in re.findall(r'\w+', line)] | |||
_1,_7,_4, *pending,_8 = sorted(set(seqs), key=len) | |||
sorter = lambda x: [len(x &_8), len(x &_4), len(x &_1)] | |||
@@ -7,3 +10,5 @@ for line in text.splitlines(): | |||
ns = [_0,_1,_2,_3,_4,_5,_6,_7,_8,_9] | |||
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:])) | |||
print(ans1) | |||
print(ans2) |
@@ -1,12 +1,13 @@ | |||
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 | |||
grid = read_image(text)[0] | |||
for p, v in grid.items(): | |||
if all(grid[p + s] > v for s in [1, -1, 1j, -1j] if p + s in grid): | |||
ans1 += int(v) + 1 | |||
print(ans1) | |||
valid = {k for k, v in grid.items() if v != '9'} | |||
pending = valid.copy() | |||
@@ -20,4 +21,4 @@ while pending: | |||
pending -= seen | |||
basin_sizes.append(len(seen)) | |||
*_, x, y, z = sorted(basin_sizes) | |||
ans2 = x * y * z | |||
print(x * y * z) |
@@ -4,7 +4,7 @@ fam = lambda c: objs.index(c) // 2 | |||
val = lambda c: objs.index(c) % 2 | |||
ans1 = 0 | |||
scores = [] | |||
for ln in text.splitlines(): | |||
for ln in open(0).read().splitlines(): | |||
stack = [] | |||
for c in ln: | |||
if val(c) == 0: | |||
@@ -21,3 +21,5 @@ for ln in text.splitlines(): | |||
score += fam(stack.pop()) + 1 | |||
scores.append(score) | |||
ans2 = sorted(scores)[len(scores) // 2] | |||
print(ans1) | |||
print(ans2) |
@@ -1,9 +1,4 @@ | |||
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): | |||
@@ -13,6 +8,12 @@ def flash(k, flashed_this_round): | |||
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 | |||
for i in itertools.count(1): | |||
for k in state: | |||
@@ -31,3 +32,5 @@ for i in itertools.count(1): | |||
if len({v for v in state.values()}) == 1: | |||
ans2 = i | |||
break | |||
print(ans1) | |||
print(ans2) |
@@ -22,10 +22,10 @@ def solve(graph, part2=False): | |||
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[b].add(a) | |||
graph = dict(graph) | |||
ans1 = solve(graph) | |||
ans2 = solve(graph, True) | |||
print(solve(graph)) | |||
print(solve(graph, True)) |
@@ -1,7 +1,4 @@ | |||
import toolkit | |||
dots, instructions = text.split('\n\n') | |||
dots, instructions = open(0).read().split('\n\n') | |||
dots = {eval(pair) for pair in dots.splitlines()} | |||
ans1 = None | |||
for line in instructions.splitlines(): | |||
@@ -13,4 +10,12 @@ for line in instructions.splitlines(): | |||
dots = {(x, y) if y < zz else (x, 2 * zz - y) for x, y in dots} | |||
if ans1 is None: | |||
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) |
@@ -1,28 +1,37 @@ | |||
def valid_z1s(z2, w, c1, c2, c3): | |||
valid = {(z2 - w - c3) // 26} | {(z2 * 26 + i) for i in range(26)} | |||
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): | |||
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)] | |||
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 = [ | |||
(w + s, z1) | |||
for s, z2 in state | |||
for w in '123456789' | |||
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) |
@@ -19,6 +19,6 @@ for turn in range(1000): | |||
hashable = frozenset(state.items()) | |||
if hashable in seen: | |||
print(turn) | |||
print(turn + 1) | |||
break | |||
seen.add(hashable) |