Compare commits

...

62 Commits

Author SHA1 Message Date
kwout e9c1221994 2023 day 9 part 2 solved 10 months ago
kwout 3825cced4a 2023 day 9 part 1 solved 10 months ago
kwout a96ab83aef 2023 day 8 part 2 solved 10 months ago
kwout 0b04a87f1a 2023 day 8 part 1 solved 10 months ago
kwout 26d54e9557 2023 day 7 part 2 solved 10 months ago
kwout 447cc032f2 2023 day 7 part 1 solved 10 months ago
kwout e2bf6fc1fa 2022 day 8 part 2 solved 10 months ago
kwout 29ddbf602b 2022 day 8 part 1 solved 10 months ago
kwout c41a7663db 2023 day 5 part 2 solved (finally) 10 months ago
kwout 9a30188716 2023 day 6 part 2 solved 10 months ago
kwout 3f761b5586 2023 day 6 part 1 solved 10 months ago
kwout 7f8921fae8 2023 day 5 part 1 solved 10 months ago
kwout 1818e0b773 2023 day 4 part 2 solved 10 months ago
kwout cc8585049f 2023 day 4 part 1 solved 10 months ago
kwout 6c0e8370b3 update motd 10 months ago
kwout e59f354f1b fixed stupid continue 10 months ago
kwout 3dbe20be20 fixed i for i in list comp. of several solutions 10 months ago
kwout 6e9bd17ca1 2023 day 3 part 2 solved 10 months ago
kwout 36d01e775f 2023 day 3 part 1 solved 10 months ago
kwout 84f03efbc5 2022 7b solved 10 months ago
kwout 002b7d7897 2022 day 7a solved 10 months ago
kwout f967f4ef7b 2022 day 6 solved 10 months ago
kwout a6d6b45d4b 2022 5b solved 10 months ago
kwout 78e8805895 2022 5a solved 10 months ago
kwout b96edf7ad8 2023 2b solved 10 months ago
kwout 7d30023c33 2023 2a solved 10 months ago
kwout b2081b8315 minor improvement to 2022 day 4 solution 10 months ago
kwout baee78e55b 2022 4b solved 10 months ago
kwout 0f156acdbf 2022 4a solved 10 months ago
kwout 650a7651ab 2022 day 3b solved 10 months ago
kwout e0c1596ed8 2022 3a solved 10 months ago
kwout ad9339bac7 streamlines structure and removed input data 10 months ago
kwout 2f65f1d54f forgot to rename 2022 day 2 solution file 10 months ago
kwout 8dd1d6c006 2022 2b solved 10 months ago
kwout 40ce3e8e19 2022 2a solved 10 months ago
kwout d25bd9fbd4 update readme 10 months ago
kwout 74ff1d77ab solved 2023 day 1b and made 1a better 10 months ago
kwout 01e799b65c 2023 1a 10 months ago
kwout 462dc84ecf solved 12b in 12a 2 years ago
kwout 779efe0689 solved 12a 2 years ago
kwout 834dc8fb52 sovled 11b in 11a 2 years ago
kwout 5357c5da19 solved 11a 2 years ago
kwout ddfb07ba29 minor improvement to readability of 10 2 years ago
kwout 09f06dc867 solve 10b in 10a 2 years ago
kwout 5475101d7e solved 10a 2 years ago
kwout d38e9fa85b solved 9b in 9a 2 years ago
kwout 35d86b41b7 day 1 2022 wow 2 years ago
kwout 1d668e3192 solve 9a 2 years ago
kwout 7ced6f10a7 solve 8a and 8b in one file 2 years ago
kwout a1c971e81c solved 7b in 7a 2 years ago
kwout 56a986ad90 solve 7a 2 years ago
kwout 5f75458893 solve day 6 (wew) 2 years ago
kwout 8800495b02 streamlined 5 even more 2 years ago
kwout 8a808d947d solved 5b in 5a 2 years ago
kwout d0b013f1d3 streamlined 5a 2 years ago
kwout fa8818b168 streamlined 4 some 2 years ago
kwout d42b803336 cleaned 5a, toying with swift in 1a, updated gitignore to not include binaries 2 years ago
kwout 6cd6caa420 solved day 5a, removed useless day 4 code 2 years ago
kwout 1e87a2eb67 trying to mix readme reee 2 years ago
kwout 535589c45a solved 4b (in 4a solver), fixed readme 2 years ago
kwout c51e2f1a3a add readme, add sample data for day1 day2, completed solution for day4a 2 years ago
kwout 9d359082d3 added vim swap to gitignore 2 years ago
  1. 8
      .gitignore
  2. 35
      2021/10.py
  3. 39
      2021/11.py
  4. 30
      2021/12.py
  5. 0
      2021/1a.py
  6. 15
      2021/1a.swift
  7. 0
      2021/1b.py
  8. 0
      2021/2a.py
  9. 0
      2021/2b.py
  10. 0
      2021/3a.py
  11. 37
      2021/3b.py
  12. 42
      2021/4.py
  13. 37
      2021/5.py
  14. 29
      2021/6.py
  15. 19
      2021/7.py
  16. 61
      2021/8.py
  17. 43
      2021/9.py
  18. 14
      2022/1.py
  19. 36
      2022/2.py
  20. 35
      2022/3.py
  21. 18
      2022/4.py
  22. 30
      2022/5.py
  23. 9
      2022/6.py
  24. 36
      2022/7.py
  25. 49
      2022/8.py
  26. 20
      2023/1a.py
  27. 32
      2023/1b.py
  28. 25
      2023/2.py
  29. 53
      2023/3.py
  30. 19
      2023/4.py
  31. 43
      2023/5.py
  32. 30
      2023/6.py
  33. 67
      2023/7.py
  34. 28
      2023/8.py
  35. 12
      2023/9.py
  36. 14
      README.md

8
.gitignore vendored

@ -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)

@ -0,0 +1,14 @@
my solutions for the [Advent of Code](https://adventofcode.com/)
in progress:\
2021: 12/25\
2022: 8/25\
2023: 9/25
planned:\
2020\
2019\
2018\
2017\
2016\
2015
Loading…
Cancel
Save