Compare commits
62 Commits
0fe4c0d032
...
e9c1221994
36 changed files with 965 additions and 0 deletions
@ -0,0 +1,8 @@ |
||||
* |
||||
!*.* |
||||
!*/ |
||||
|
||||
*.swp |
||||
*.swo |
||||
*.txt |
||||
.DS_Store |
@ -0,0 +1,35 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
|
||||
di = { |
||||
('(', ')', 3, 1), |
||||
('[', ']', 57, 2), |
||||
('{', '}', 1197, 3), |
||||
('<', '>', 25137, 4) |
||||
} |
||||
score1 = 0 |
||||
score2 = [] |
||||
for l in lines: |
||||
stack = [] |
||||
valid = True |
||||
for c in l: |
||||
if c == '(' or c == '[' or c == '{' or c == '<': |
||||
stack.append(c) |
||||
if c == ')' or c == ']' or c == '}' or c == '>': |
||||
match = [x for x in di if x[1] == c][0] |
||||
if stack.pop() != match[0]: |
||||
score1 += match[2] |
||||
valid = False |
||||
break |
||||
if valid: |
||||
if stack == []: |
||||
break |
||||
score = 0 |
||||
while stack != []: |
||||
cha = stack.pop() |
||||
score = score * 5 + [x for x in di if x[0] == cha][0][3] |
||||
score2.append(score) |
||||
|
||||
print(score1) |
||||
score2.sort() |
||||
print(score2[(int)(len(score2)/2)]) |
@ -0,0 +1,39 @@ |
||||
lines = [ [int(j) for j in list(i)] for i in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
flashes = 0 |
||||
i = 0 |
||||
found = False |
||||
while i < 99 or not found: |
||||
lines = [ [x+1 for x in y] for y in lines] |
||||
flashed = True |
||||
while flashed: |
||||
flashed = False |
||||
for x in range(10): |
||||
for y in range(10): |
||||
if lines[x][y] > 9: |
||||
if(i<100): |
||||
flashes += 1 |
||||
lines[x][y] = 0 |
||||
flashed = True |
||||
if x != 0 and lines[x-1][y] != 0: |
||||
lines[x-1][y] += 1 |
||||
if x != 0 and y != 9 and lines[x-1][y+1] != 0: |
||||
lines[x-1][y+1] += 1 |
||||
if y != 9 and lines[x][y+1] != 0: |
||||
lines[x][y+1] += 1 |
||||
if x != 9 and y != 9 and lines[x+1][y+1] != 0: |
||||
lines[x+1][y+1] += 1 |
||||
if x != 9 and lines[x+1][y] != 0: |
||||
lines[x+1][y] += 1 |
||||
if x != 9 and y != 0 and lines[x+1][y-1] != 0: |
||||
lines[x+1][y-1] += 1 |
||||
if y != 0 and lines[x][y-1] != 0: |
||||
lines[x][y-1] += 1 |
||||
if x != 0 and y != 0 and lines[x-1][y-1] != 0: |
||||
lines[x-1][y-1] += 1 |
||||
i += 1 |
||||
if lines.count([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == 10: |
||||
print(i) |
||||
found = True |
||||
|
||||
print(flashes) |
@ -0,0 +1,30 @@ |
||||
lines = [ i.split('-') for i in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
paths = [] |
||||
|
||||
di = {} |
||||
for l in lines: |
||||
if l[0] not in di: |
||||
di[l[0]] = [] |
||||
di[l[0]].append(l[1]) |
||||
if l[1] not in di: |
||||
di[l[1]] = [] |
||||
di[l[1]].append(l[0]) |
||||
|
||||
|
||||
def travel(stack, node): |
||||
stack.append(node) |
||||
if node == 'end': |
||||
paths.append(stack.copy()) |
||||
for n in di[node]: |
||||
#if n not in stack or n.isupper(): |
||||
if (not(n in stack and any(stack.count(i) > 1 for i in [j for j in stack if j.islower()])) or n.isupper()) and n != 'start' and stack[-1] != 'end': |
||||
travel(stack,n) |
||||
stack.pop() |
||||
|
||||
for start in di['start']: |
||||
stack = ['start'] |
||||
travel(stack, start) |
||||
|
||||
print(len([i for i in paths if not any(i.count(j) > 1 for j in i if j.islower())])) |
||||
print(len(paths)) |
@ -0,0 +1,15 @@ |
||||
import Foundation |
||||
|
||||
let contents = try! String(contentsOfFile: "input.txt") |
||||
var lines = [Int]() |
||||
for l in contents.split(separator:"\n") { |
||||
lines.append(Int(l)!) |
||||
} |
||||
|
||||
var increases = 0 |
||||
for i in 1..<lines.count { |
||||
if lines[i] > lines[i-1] { |
||||
increases += 1 |
||||
} |
||||
} |
||||
print(increases) |
@ -0,0 +1,37 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
leng = len(lines) |
||||
bits = len(lines[0]) |
||||
|
||||
def fun(bol): |
||||
targetCandidates = list(range(0,leng)) |
||||
rating = 0 |
||||
target = True |
||||
for i in range(0, bits): |
||||
count = 0 |
||||
vacate = [] |
||||
for j in targetCandidates: |
||||
if len(targetCandidates) == 1: |
||||
o2 = int(lines[j], 2) |
||||
break |
||||
elif i != 0 and bool(int(lines[j][i-1])) != target : |
||||
vacate.append(j) |
||||
elif bool(int(lines[j][i])): |
||||
count += 1 |
||||
targetCandidates = [i for i in targetCandidates if i not in vacate] |
||||
if count >= len(targetCandidates)/2 and bol: |
||||
target = True |
||||
elif count < len(targetCandidates)/2 and not bol: |
||||
target = True |
||||
else: |
||||
target = False |
||||
if len(targetCandidates) == 2: |
||||
if bool(int(lines[targetCandidates[0]][i])) == target: |
||||
return int(lines[targetCandidates[0]], 2) |
||||
else: |
||||
return int(lines[targetCandidates[1]], 2) |
||||
elif len(targetCandidates) == 1: |
||||
return int(lines[targetCandidates[0]], 2) |
||||
|
||||
|
||||
print(fun(True)*fun(False)) |
||||
|
@ -0,0 +1,42 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
draw = [int(i) for i in lines[0].split(",")] |
||||
boards = [] |
||||
for s in range(2, len(lines),6): |
||||
boards.append([[int(j) for j in lines[i].split()] for i in range(s,s+5)]) |
||||
|
||||
winsOn = [-1] * len(boards) |
||||
|
||||
for b in range(0, len(boards)): |
||||
for i in range(0, 5): |
||||
latestH = 0 |
||||
latestV = 0 |
||||
for j in range(0, 5): |
||||
if boards[b][i][j] not in draw or boards[b][j][i] not in draw: |
||||
break |
||||
drawIndex = draw.index(boards[b][i][j]) |
||||
if drawIndex > latestH: |
||||
latestH = drawIndex |
||||
drawIndex = draw.index(boards[b][j][i]) |
||||
if drawIndex > latestV: |
||||
latestV = drawIndex |
||||
if winsOn[b] == -1 or winsOn[b] > latestH: |
||||
winsOn[b] = latestH |
||||
if winsOn[b] == -1 or winsOn[b] > latestV: |
||||
winsOn[b] = latestV |
||||
|
||||
summa = 0 |
||||
summaLose = 0 |
||||
winBoard = winsOn.index(min(winsOn)) |
||||
loseBoard = winsOn.index(max(winsOn)) |
||||
for row in boards[winBoard]: |
||||
for n in row: |
||||
if n not in draw[:winsOn[winBoard]+1]: |
||||
summa += n |
||||
|
||||
for row in boards[loseBoard]: |
||||
for n in row: |
||||
if n not in draw[:winsOn[loseBoard]+1]: |
||||
summaLose += n |
||||
|
||||
print(draw[winsOn[winBoard]]*summa) |
||||
print(draw[winsOn[loseBoard]]*summaLose) |
@ -0,0 +1,37 @@ |
||||
lines = [] |
||||
for l in open("input.txt", 'r').read().splitlines(): |
||||
p = l.split(" -> ") |
||||
q = [] |
||||
q.append(p[0].split(",")) |
||||
q.append(p[1].split(",")) |
||||
lines.append([[int(q[0][0]),int(q[0][1])],[int(q[1][0]), int(q[1][1])]]) |
||||
grid = [[0 for i in range(1000)] for j in range(1000)] |
||||
gridAll = [[0 for i in range(1000)] for j in range(1000)] |
||||
overlaps = 0 |
||||
overlapsAll = 0 |
||||
|
||||
for l in lines: |
||||
xDir = 0 |
||||
yDir = 0 |
||||
if l[0][0] > l[1][0]: |
||||
xDir = -1 |
||||
elif l[0][0] < l[1][0]: |
||||
xDir = 1 |
||||
if l[0][1] > l[1][1]: |
||||
yDir = -1 |
||||
elif l[0][1] < l[1][1]: |
||||
yDir = 1 |
||||
for i in range(0, max([abs(l[1][0]-l[0][0]),abs(l[1][1]-l[0][1])])+1): |
||||
x = l[0][0]+i*xDir |
||||
y = l[0][1]+i*yDir |
||||
if xDir == 0 or yDir == 0: |
||||
grid[x][y] +=1 |
||||
if grid[x][y] == 2: |
||||
overlaps +=1 |
||||
gridAll[x][y] += 1 |
||||
if gridAll[x][y] == 2: |
||||
overlapsAll += 1 |
||||
|
||||
|
||||
print(overlaps) |
||||
print(overlapsAll) |
@ -0,0 +1,29 @@ |
||||
fish = [int(i) for i in open("input.txt", 'r').read().split(",")] |
||||
fishies = {} |
||||
newFishies = {} |
||||
|
||||
for i in range(9): |
||||
fishies[i] = 0 |
||||
newFishies[i] = 0 |
||||
|
||||
for f in fish: |
||||
fishies[f] += 1 |
||||
|
||||
for i in range(int(input("days: "))): |
||||
tempFish = fishies.copy() |
||||
fishies[6] = fishies[0]+newFishies[0] |
||||
for j in range(6): |
||||
fishies[j] = tempFish[j+1] |
||||
foo = tempFish[0] |
||||
tempFish = newFishies.copy() |
||||
newFishies[8] = foo + newFishies[0] |
||||
for j in range(8): |
||||
newFishies[j] = tempFish[j+1] |
||||
|
||||
summa = 0 |
||||
for x in fishies: |
||||
summa += fishies[x] |
||||
for x in newFishies: |
||||
summa += newFishies[x] |
||||
|
||||
print(summa) |
@ -0,0 +1,19 @@ |
||||
positions = [int(i) for i in open("input.txt", 'r').read().split(",")] |
||||
|
||||
width = max(positions) |
||||
part = input("a or b? ") |
||||
bestFuel = len(positions)*width if part == "a" else len(positions)* int(((width ** 2 + width) / 2)) |
||||
bestPos = 0 |
||||
|
||||
for i in range(bestFuel): |
||||
fuelUsed = 0 |
||||
for pos in positions: |
||||
mov = abs(i - pos) |
||||
fuelUsed += mov if part == "a" else int(((mov ** 2 + mov) / 2)) |
||||
if fuelUsed > bestFuel: |
||||
break |
||||
bestFuel = fuelUsed |
||||
bestPos = i |
||||
|
||||
print(bestFuel) |
||||
print(bestPos) |
@ -0,0 +1,61 @@ |
||||
entries = open("input.txt", 'r').read().splitlines() |
||||
|
||||
summa = 0 |
||||
count = 0 |
||||
for e in entries: |
||||
digits = e.split(" | ")[0].split() |
||||
output = e.split(" | ")[1].split() |
||||
dic = [set() for i in range(10)] |
||||
counts = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0} |
||||
for s in digits: |
||||
if len(s) == 4: |
||||
dic[4].update(set(s)) |
||||
elif len(s) == 2: |
||||
dic[1].update(set(s)) |
||||
elif len(s) == 3: |
||||
dic[7].update(set(s)) |
||||
elif len(s) == 7: |
||||
dic[8].update(set(s)) |
||||
for l in s: |
||||
counts[l] += 1 |
||||
for l in counts: |
||||
if counts[l] == 9: |
||||
for i in range(10): |
||||
if i not in [1,2,4,7,8]: |
||||
dic[i].add(l) |
||||
elif counts[l] == 6: |
||||
for i in range(10): |
||||
if i not in [1,2,3,4,7,8]: |
||||
dic[i].add(l) |
||||
elif counts[l] == 4: |
||||
for i in range(10): |
||||
if i not in [1,4,7,8,3,5,9]: |
||||
dic[i].add(l) |
||||
elif l in dic[7] and l not in dic[4]: |
||||
for i in range(10): |
||||
if i not in [1,4,7,8]: |
||||
dic[i].add(l) |
||||
elif counts[l] == 8: |
||||
for i in range(10): |
||||
if i not in [1,4,7,8,5,6]: |
||||
dic[i].add(l) |
||||
elif counts[l] == 7 and l in dic[4]: |
||||
for i in range(10): |
||||
if i not in [1,4,7,8,0]: |
||||
dic[i].add(l) |
||||
else: |
||||
for i in range(10): |
||||
if i not in [1,4,7,8,]: |
||||
dic[i].add(l) |
||||
|
||||
for o in range(4): |
||||
for d in dic: |
||||
if set(output[o]) == d: |
||||
index = dic.index(d) |
||||
summa += index*10**(3-o) |
||||
if index in [1,4,7,8]: |
||||
count += 1 |
||||
|
||||
print(count) |
||||
print(summa) |
||||
|
@ -0,0 +1,43 @@ |
||||
lines = [ [int(j) for j in list(i)] for i in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
summa = 0 |
||||
lowList = [] |
||||
for x in range(len(lines)): |
||||
for y in range(len(lines[x])): |
||||
v = lines[x][y] |
||||
l = 9 if x == 0 else lines[x-1][y] |
||||
r = 9 if x == len(lines)-1 else lines[x+1][y] |
||||
u = 9 if y == 0 else lines[x][y-1] |
||||
d = 9 if y == len(lines[x])-1 else lines[x][y+1] |
||||
if v < l and v < r and v < u and v < d: |
||||
summa += 1 + v |
||||
lowList.append((x,y)) |
||||
|
||||
for p in lowList: |
||||
size = 0 |
||||
|
||||
|
||||
print(summa) |
||||
explored = [] |
||||
|
||||
def explore(x,y): |
||||
if lines[x][y] == 9 or (x,y) in explored: |
||||
return 0 |
||||
explored.append((x,y)) |
||||
size = 1 |
||||
if y != 0: |
||||
size += explore(x,y-1) |
||||
if y != len(lines[x]) - 1: |
||||
size += explore(x,y+1) |
||||
if x != 0: |
||||
size += explore(x-1,y) |
||||
if x != len(lines) - 1: |
||||
size += explore(x+1,y) |
||||
return size |
||||
|
||||
sizes = [0]*len(lowList) |
||||
for i in range(len(lowList)): |
||||
sizes[i] = explore(lowList[i][0],lowList[i][1]) |
||||
|
||||
sizes.sort() |
||||
print(sizes[-1] * sizes[-2] * sizes[-3]) |
@ -0,0 +1,14 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
elves = [] |
||||
summa = 0 |
||||
for l in lines: |
||||
if l == "": |
||||
elves.append(summa) |
||||
summa = 0 |
||||
else: |
||||
summa += int(l) |
||||
|
||||
top3 = sorted(elves)[-3:] |
||||
print(top3) |
||||
print(sum(top3)) |
@ -0,0 +1,36 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
sum = 0 |
||||
sum2 = 0 |
||||
|
||||
for l in lines: |
||||
match(l): |
||||
case "A X": |
||||
sum += 4 |
||||
sum2 += 3 |
||||
case "A Y": |
||||
sum += 8 |
||||
sum2 += 4 |
||||
case "A Z": |
||||
sum += 3 |
||||
sum2 += 8 |
||||
case "B X": |
||||
sum += 1 |
||||
sum2 += 1 |
||||
case "B Y": |
||||
sum += 5 |
||||
sum2 += 5 |
||||
case "B Z": |
||||
sum += 9 |
||||
sum2 += 9 |
||||
case "C X": |
||||
sum += 7 |
||||
sum2 += 2 |
||||
case "C Y": |
||||
sum += 2 |
||||
sum2 += 6 |
||||
case "C Z": |
||||
sum += 6 |
||||
sum2 += 7 |
||||
print(sum) |
||||
print(sum2) |
@ -0,0 +1,35 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
sum = 0 |
||||
sum2 = 0 |
||||
|
||||
for i in range(len(lines)): |
||||
l = lines[i] |
||||
found = False |
||||
for c in l[:int(len(l)/2)]: |
||||
for d in l[int(len(l)/2):]: |
||||
if c == d: |
||||
sum += letters.index(c)+1 |
||||
found = True |
||||
break |
||||
if found: |
||||
break |
||||
|
||||
found = False |
||||
if i % 3 == 0: |
||||
for c in l: |
||||
for d in lines[i+1]: |
||||
if c == d: |
||||
for e in lines[i+2]: |
||||
if d == e: |
||||
sum2 += letters.index(c)+1 |
||||
found = True |
||||
break |
||||
if found: |
||||
break |
||||
if found: |
||||
break |
||||
|
||||
print(sum) |
||||
print(sum2) |
@ -0,0 +1,18 @@ |
||||
pairs = [[[int(n) for n in m.split("-")] for m in l.split(",")] for l in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
sum = 0 |
||||
sum2 = 0 |
||||
|
||||
for p in pairs: |
||||
if p[0][0] <= p[1][0] and p[0][1] >= p[1][1]: |
||||
sum += 1 |
||||
elif p[0][0] >= p[1][0] and p[0][1] <= p[1][1]: |
||||
sum += 1 |
||||
|
||||
if p[0][0] <= p[1][1] and p[0][1] >= p[1][0]: |
||||
sum2 += 1 |
||||
elif p[1][0] <= p[0][1] and p[1][1] >= p[0][0]: |
||||
sum2 += 1 |
||||
|
||||
print(sum) |
||||
print(sum2) |
@ -0,0 +1,30 @@ |
||||
from copy import deepcopy |
||||
|
||||
data = open("input.txt", 'r').read() |
||||
|
||||
stacks, instructions = data.split("\n\n") |
||||
stacks = stacks.splitlines() |
||||
instructions = [i.split(" ") for i in instructions.splitlines()] |
||||
s = [] |
||||
|
||||
for i in range(int(stacks[-1][-2])): |
||||
s.append([]) |
||||
for j in range(len(stacks)-1): |
||||
crate = stacks[-j-2][i*4+1] |
||||
if crate != " ": |
||||
s[i].append(crate) |
||||
|
||||
s2 = deepcopy(s) |
||||
|
||||
for i in instructions: |
||||
destination = int(i[5])-1 |
||||
origin = int(i[3])-1 |
||||
quantity = int(i[1]) |
||||
s2[destination] += s2[origin][-quantity:] |
||||
for j in range(quantity): |
||||
s[destination].append(s[origin].pop()) |
||||
s2[origin].pop() |
||||
|
||||
|
||||
print("".join([t.pop() for t in s])) |
||||
print("".join([t.pop() for t in s2])) |
@ -0,0 +1,9 @@ |
||||
line = open("input.txt", 'r').read() |
||||
|
||||
def findFirstValidSequence(length): |
||||
for i in range(len(line)-length): |
||||
if len(set(line[i:i+length])) == length: |
||||
return(i+length) |
||||
|
||||
print(findFirstValidSequence(4)) |
||||
print(findFirstValidSequence(14)) |
@ -0,0 +1,36 @@ |
||||
lines = [[l.split(" ") for l in m.splitlines()] for m in open("input.txt", 'r').read().split("$ ")][1:] |
||||
|
||||
path = ["/"] |
||||
sizes = {"/":0} |
||||
sum = 0 |
||||
|
||||
for l in lines: |
||||
if l[0][0] == "cd": |
||||
if l[0][1] == "/": |
||||
path = ["/"] |
||||
elif l[0][1] == "..": |
||||
path.pop() |
||||
else: |
||||
path.append(l[0][1]+"/") |
||||
elif l[0][0] == "ls": |
||||
for i in range(1,len(l)): |
||||
if l[i][0] == "dir": |
||||
sizes["".join(path) + l[i][1]+"/"] = 0 |
||||
else: |
||||
sofar = "" |
||||
for p in path: |
||||
sofar += p |
||||
sizes[sofar] += int(l[i][0]) |
||||
|
||||
min = sizes["/"] + 30000000 - 70000000 |
||||
smallest = sizes["/"] |
||||
|
||||
for s in sizes: |
||||
size = sizes[s] |
||||
if size <= 100000: |
||||
sum += size |
||||
if size >= min and size < smallest: |
||||
smallest = size |
||||
|
||||
print(sum) |
||||
print(smallest) |
@ -0,0 +1,49 @@ |
||||
matrix = [[int(i) for i in j] for j in open("input.txt", 'r').read().splitlines()] |
||||
visible = set() |
||||
highScore = 0 |
||||
highScoreCoord = () |
||||
|
||||
def walk(line): |
||||
vis = [0] |
||||
tallest = line[0] |
||||
for i in range(1, len(line)): |
||||
if line[i] > tallest: |
||||
vis.append(i) |
||||
tallest = line[i] |
||||
return vis |
||||
|
||||
def findFirst(line, height): |
||||
for i in range(len(line)): |
||||
if line[i] >= height: |
||||
return i+1 |
||||
return len(line) |
||||
|
||||
for i in range(len(matrix)): |
||||
for coord in walk(matrix[i]): |
||||
visible.add((i, coord)) |
||||
for coord in walk(matrix[i][::-1]): |
||||
visible.add((i, len(matrix) - (coord) - 1)) |
||||
|
||||
|
||||
for i in range(len(matrix[0])): |
||||
col = [matrix[j][i] for j in range(len(matrix[0]))] |
||||
for coord in walk(col): |
||||
visible.add((coord, i)) |
||||
for coord in walk(col[::-1]): |
||||
visible.add((len(matrix[0]) - (coord) - 1, i)) |
||||
|
||||
for i in range(len(matrix)): |
||||
for j in range(len(matrix[0])): |
||||
score = 0 |
||||
left = findFirst(matrix[i][:j][::-1], matrix[i][j]) |
||||
right = findFirst(matrix[i][j+1:], matrix[i][j]) |
||||
col = [matrix[x][j] for x in range(len(matrix[0]))] |
||||
up = findFirst(col[:i][::-1], matrix[i][j]) |
||||
down = findFirst(col[i+1:], matrix[i][j]) |
||||
score = up*down*left*right |
||||
if score > highScore: |
||||
highScore = score |
||||
highScoreCoord = (i,j) |
||||
|
||||
print(len(visible)) |
||||
print(highScore) |
@ -0,0 +1,20 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
sum = 0 |
||||
|
||||
for l in lines: |
||||
first = "" |
||||
firstFound = False |
||||
last = "" |
||||
lastFound = False |
||||
for i in range(len(l)): |
||||
if not firstFound and l[i].isnumeric(): |
||||
first = l[i] |
||||
firstFound = True |
||||
if not lastFound and l[-i-1].isnumeric(): |
||||
last = l[-i-1] |
||||
lastFound = True |
||||
if firstFound and lastFound: |
||||
break |
||||
sum += int(first + last) |
||||
print(sum) |
@ -0,0 +1,32 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
sum = 0 |
||||
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] |
||||
|
||||
for l in lines: |
||||
first = "" |
||||
firstFound = False |
||||
last = "" |
||||
lastFound = False |
||||
n = len(l) |
||||
for i in range(n): |
||||
if not firstFound and l[i].isnumeric(): |
||||
first = l[i] |
||||
firstFound = True |
||||
if not lastFound and l[-i-1].isnumeric(): |
||||
last = l[-i-1] |
||||
lastFound = True |
||||
for j in range(len(words)): |
||||
m = len(words[j]) |
||||
if not firstFound and l[i:i+m] == words[j]: |
||||
first = str(j) |
||||
firstFound = True |
||||
if not lastFound and l[-(i+m):n-i] == words[j]: |
||||
last = str(j) |
||||
lastFound = True |
||||
if firstFound and lastFound: |
||||
break |
||||
s = first + last |
||||
sum += int(s) |
||||
|
||||
print(sum) |
@ -0,0 +1,25 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
sum = 0 |
||||
sum2 = 0 |
||||
|
||||
for l in lines: |
||||
sets = [[j.split(" ") for j in i.split(', ')] for i in l.split(": ")[1].split('; ')] |
||||
highest = {} |
||||
valid = True |
||||
for set in sets: |
||||
for cubes in set: |
||||
cubes[0] = int(cubes[0]) |
||||
if (cubes[1] == "red" and cubes[0] > 12) or (cubes[1] == "green" and cubes[0] > 13) or (cubes[1] == "blue" and cubes[0] > 14): |
||||
valid = False |
||||
if cubes[1] not in highest or cubes[0] > highest[cubes[1]]: |
||||
highest[cubes[1]] = cubes[0] |
||||
if valid: |
||||
sum += int(l.split(": ")[0].split(" ")[1]) |
||||
add = 1 |
||||
for h in highest: |
||||
add *= highest[h] |
||||
sum2 += add |
||||
|
||||
print(sum) |
||||
print(sum2) |
@ -0,0 +1,53 @@ |
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
|
||||
sum = 0 |
||||
sum2 = 0 |
||||
nonqual = ['0','1','2','3','4','5','6','7','8','9','.'] |
||||
gears = {} |
||||
|
||||
def checkValid(x,y): |
||||
if lines[x][y] == "*": |
||||
coord = str(x) + "," + str(y) |
||||
if coord not in gears: |
||||
gears[coord] = [int(num)] |
||||
else: |
||||
gears[coord].append(int(num)) |
||||
return True |
||||
if lines[x][y] not in nonqual: |
||||
return True |
||||
|
||||
for i in range(len(lines)): |
||||
columns = len(lines[i]) |
||||
j = 0 |
||||
while j < columns: |
||||
if lines[i][j].isnumeric(): |
||||
num = "" |
||||
valid = False |
||||
for k in range(len(lines[i])): |
||||
if j+k >= columns: |
||||
break |
||||
if lines[i][j+k].isnumeric(): |
||||
num += lines[i][j+k] |
||||
else: |
||||
break |
||||
lennum = len(num) |
||||
for x in range(0 if j == 0 else j-1, columns if j + lennum == columns else j+lennum+1): |
||||
if i != 0 and checkValid(i-1, x): |
||||
valid = True |
||||
if i != len(lines)-1 and checkValid(i+1, x): |
||||
valid = True |
||||
if j != 0 and checkValid(i, j-1): |
||||
valid = True |
||||
if j+lennum != columns and checkValid(i, j+lennum): |
||||
valid = True |
||||
if valid: |
||||
sum += int(num) |
||||
j += lennum |
||||
j += 1 |
||||
|
||||
for coord in gears: |
||||
if len(gears[coord]) == 2: |
||||
sum2 += gears[coord][0] * gears[coord][1] |
||||
|
||||
print(sum) |
||||
print(sum2) |
@ -0,0 +1,19 @@ |
||||
lines = [[int(m[0][-1]), 1, [[int(o) for o in n.split(" ") if o!= ""] for n in m[1].split(" | ")]] for m in [l.split(": ") for l in open("input.txt", "r").read().splitlines()]] |
||||
#structure: [[id, #ofcopies, [[winning numbers], [card numbers]]]] |
||||
|
||||
summa = 0 |
||||
|
||||
for i in range(len(lines)): |
||||
hits = len(set(lines[i][2][0]).intersection(lines[i][2][1])) |
||||
summa += pow(2, hits-1) if hits > 0 else 0 |
||||
for j in range(hits): |
||||
lines[i+j+1][1] += lines[i][1] |
||||
|
||||
|
||||
summa2 = sum([l[1] for l in lines]) |
||||
|
||||
print(summa) |
||||
print(summa2) |
||||
|
||||
#cursed one line solution to part 1 |
||||
#print(sum([int(pow(2, len(set(l[1][0]).intersection(l[1][1]))-1)) for l in [(int(m[0][-1]), [[int(o) for o in n.split(" ") if o!= ""] for n in m[1].split(" | ")]) for m in [l.split(": ") for l in open("input.txt", "r").read().splitlines()]]])) |
@ -0,0 +1,43 @@ |
||||
data = open("input.txt", 'r').read().split("\n\n") |
||||
seeds = [int(i) for i in data[0][7:].split()] |
||||
maps = [[[int(i) for i in numb.split()] for numb in block.splitlines()[1:]] for block in data[1:]] |
||||
#[destinationStart, sourceStart, length] |
||||
|
||||
seeds2 = [] |
||||
#[(start, end)] |
||||
for i in range(0, len(seeds), 2): |
||||
seeds2.append((seeds[i], seeds[i]+seeds[i+1]-1)) |
||||
|
||||
for m in maps: |
||||
newSeeds = list(seeds) |
||||
newSeeds2 = list() |
||||
for target, source, length in m: |
||||
for i in range(len(seeds)): |
||||
seed = seeds[i] |
||||
if seed in range(source, source+length): |
||||
newSeeds[i] = seed + (target - source) |
||||
|
||||
while seeds2: |
||||
rangeStart, rangeEnd = seeds2.pop() |
||||
for r, mapStart, length in m: |
||||
mapEnd = mapStart + length -1 |
||||
delta = r - mapStart |
||||
if mapEnd < rangeStart or mapStart > rangeEnd: |
||||
continue |
||||
if rangeStart < mapStart: |
||||
seeds2.append((rangeStart, mapStart - 1)) |
||||
rangeStart = mapStart |
||||
if rangeStart < mapStart: |
||||
seeds2.append((rangeStart, mapStart - 1)) |
||||
rangeStart = mapStart |
||||
newSeeds2.append((rangeStart + delta, rangeEnd + delta)) |
||||
break |
||||
else: |
||||
newSeeds2.append((rangeStart, rangeEnd)) |
||||
|
||||
|
||||
seeds = newSeeds |
||||
seeds2 = newSeeds2 |
||||
|
||||
print(min(newSeeds)) |
||||
print(min(seeds2)[0]) |
@ -0,0 +1,30 @@ |
||||
from math import pow, ceil, floor |
||||
|
||||
lines = open("input.txt", 'r').read().splitlines() |
||||
cols = [l.split() for l in lines] |
||||
races = [(int(cols[0][i]), int(cols[1][i])) for i in range(1,len(cols[0]))] |
||||
#[(time, record)] |
||||
realRace = (int(lines[0].split(": ")[1].replace(" ","")), int(lines[1].split(": ")[1].replace(" ",""))) |
||||
|
||||
#d = t * v |
||||
#charge + t = time |
||||
#t = time - charge |
||||
#v = charge |
||||
#d = (time - charge) * charge |
||||
#d = -charge^2 + time*charge |
||||
|
||||
summa = 1 |
||||
|
||||
def findWays(race): |
||||
a = -1 |
||||
b = race[0] |
||||
c = -race[1] |
||||
rangeStart = floor((-b + pow((pow(b,2)-(4*a*c)),0.5)) / (2*a)) + 1 |
||||
rangeEnd = ceil((-b - pow((pow(b,2)-(4*a*c)),0.5)) / (2*a)) - 1 |
||||
return rangeEnd-rangeStart+1 |
||||
|
||||
for r in races: |
||||
summa *= findWays(r) |
||||
|
||||
print(summa) |
||||
print(findWays(realRace)) |
@ -0,0 +1,67 @@ |
||||
hands = [l.split() for l in open("input.txt", 'r').read().splitlines()] |
||||
sort1 = [[] for i in range(7)] |
||||
sort2 = [[] for i in range(7)] |
||||
|
||||
for hand in hands: |
||||
handSet = {} |
||||
for card in set(hand[0]): |
||||
handSet[card] = hand[0].count(card) |
||||
handSetLen = len(handSet) |
||||
jCount = hand[0].count('J') |
||||
|
||||
if handSetLen == 1: |
||||
sort1[0].append(hand) # 5oak |
||||
sort2[0].append(hand) |
||||
elif handSetLen == 2: |
||||
for card in handSet: |
||||
if handSet[card] == 4: |
||||
sort1[1].append(hand) # 4oak |
||||
sort2[0 if jCount > 0 else 1].append(hand) |
||||
break |
||||
elif handSet[card] == 3: |
||||
sort1[2].append(hand) # fh |
||||
sort2[0 if jCount > 0 else 2].append(hand) |
||||
break |
||||
elif handSetLen == 3: |
||||
for card in handSet: |
||||
if handSet[card] == 3: |
||||
sort1[3].append(hand) # 3oak |
||||
sort2[1 if jCount > 0 else 3].append(hand) |
||||
break |
||||
elif handSet[card] == 2: |
||||
sort1[4].append(hand) # 2pair |
||||
sort2[(2 if jCount == 1 else 1) if jCount > 0 else 4].append(hand) |
||||
break |
||||
elif handSetLen == 4: |
||||
for card in handSet: |
||||
if handSet[card] == 2: |
||||
sort1[5].append(hand) # 1pair |
||||
sort2[3 if jCount > 0 else 5].append(hand) |
||||
break |
||||
elif handSetLen == 5: |
||||
for card in handSet: |
||||
sort1[6].append(hand) # highcard |
||||
sort2[5 if jCount > 0 else 6].append(hand) |
||||
|
||||
|
||||
cards1 = "AKQJT98765432"[::-1] |
||||
cards2 = "AKQT98765432J"[::-1] |
||||
|
||||
def findScore(cards, hands): |
||||
score = 0 |
||||
scoreIndex = 1 |
||||
for i in range(7): |
||||
sortTemp = {} |
||||
for hand in hands[-i-1]: |
||||
converted = "" |
||||
for c in hand[0]: |
||||
converted += hex(cards.find(c))[2:] |
||||
sortTemp[int('0x' + converted, 16)] = int(hand[1]) |
||||
|
||||
for index in sorted(sortTemp): |
||||
score += scoreIndex * sortTemp[index] |
||||
scoreIndex += 1 |
||||
return score |
||||
|
||||
print(findScore(cards1, sort1)) |
||||
print(findScore(cards2, sort2)) |
@ -0,0 +1,28 @@ |
||||
from math import lcm |
||||
|
||||
input = open("input.txt", 'r').read().splitlines() |
||||
instructions = input[0] |
||||
nodes = {l.split()[0]: (l.split()[2][1:-1], l.split()[3][:-1]) for l in input[2:]} |
||||
|
||||
steps = 0 |
||||
node = 'AAA' |
||||
|
||||
while node != 'ZZZ': |
||||
node = nodes[node][0] if instructions[steps % len(instructions)] == 'L' else nodes[node][1] |
||||
steps += 1 |
||||
|
||||
|
||||
nodeGhost = [n for n in nodes if n[-1] == 'A'] |
||||
multiples = [] |
||||
|
||||
for n in nodeGhost: |
||||
stepsGhost = 0 |
||||
nodeTemp = n |
||||
while nodeTemp[-1] != 'Z': |
||||
nodeTemp = nodes[nodeTemp][0] if instructions[stepsGhost % len(instructions)] == 'L' else nodes[nodeTemp][1] |
||||
stepsGhost += 1 |
||||
multiples.append(stepsGhost) |
||||
|
||||
|
||||
print(steps) |
||||
print(lcm(*multiples)) |
@ -0,0 +1,12 @@ |
||||
input = [[int(i) for i in l.split()] for l in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
def diff(l): |
||||
newList = [l[i+1] - l[i] for i in range(len(l)-1)] |
||||
return l[-1] + diff(newList) if set(newList) != {0} else l[-1] |
||||
|
||||
|
||||
score = sum(diff(l) for l in input) |
||||
score2 = sum(diff(l[::-1]) for l in input) |
||||
|
||||
print(score) |
||||
print(score2) |
Loading…
Reference in new issue