Compare commits

...

62 Commits

Author SHA1 Message Date
kwout 0fe4c0d032 2023 day 9 part 2 solved 1 year ago
kwout 23d9e8f9bf 2023 day 9 part 1 solved 1 year ago
kwout e972ca6378 2023 day 8 part 2 solved 1 year ago
kwout 9e4cf3fbf5 2023 day 8 part 1 solved 1 year ago
kwout aa3d90d771 2023 day 7 part 2 solved 1 year ago
kwout b7de7999c8 2023 day 7 part 1 solved 1 year ago
kwout 23622c64aa 2022 day 8 part 2 solved 1 year ago
kwout 9a6368abb9 2022 day 8 part 1 solved 1 year ago
kwout aa6066acee 2023 day 5 part 2 solved (finally) 1 year ago
kwout 150bcd5f84 2023 day 6 part 2 solved 1 year ago
kwout 2aff65363f 2023 day 6 part 1 solved 1 year ago
kwout e9f829a6a8 2023 day 5 part 1 solved 1 year ago
kwout 7e8d944d0d 2023 day 4 part 2 solved 1 year ago
kwout f0df6b8c8c 2023 day 4 part 1 solved 1 year ago
kwout 6448ad02bd update motd 1 year ago
kwout 3dfbbd738a fixed stupid continue 1 year ago
kwout f54e186c76 fixed i for i in list comp. of several solutions 1 year ago
kwout ec923093d4 2023 day 3 part 2 solved 1 year ago
kwout 8bd7804a42 2023 day 3 part 1 solved 1 year ago
kwout 152016025b 2022 7b solved 1 year ago
kwout 32be41efc8 2022 day 7a solved 1 year ago
kwout f8ddc13152 2022 day 6 solved 1 year ago
kwout 0416729be3 2022 5b solved 1 year ago
kwout 97d833f25f 2022 5a solved 1 year ago
kwout e5d3042f81 2023 2b solved 1 year ago
kwout 42f4e5cd64 2023 2a solved 1 year ago
kwout 0a5c75059a minor improvement to 2022 day 4 solution 1 year ago
kwout 49b766106a 2022 4b solved 1 year ago
kwout b285cae340 2022 4a solved 1 year ago
kwout 5733eb89d7 2022 day 3b solved 1 year ago
kwout 34be3f855d 2022 3a solved 1 year ago
kwout 41d26912d2 streamlines structure and removed input data 1 year ago
kwout 7353d7b22a forgot to rename 2022 day 2 solution file 1 year ago
kwout cfbf06d309 2022 2b solved 1 year ago
kwout d8585d0e65 2022 2a solved 1 year ago
kwout e761f8a7dc update readme 1 year ago
kwout 09d24e0c2f solved 2023 day 1b and made 1a better 1 year ago
kwout f7dbd317f8 2023 1a 1 year ago
kwout 305d35409d solved 12b in 12a 2 years ago
kwout 8d69e4c7ac solved 12a 2 years ago
kwout 16aa514cd1 sovled 11b in 11a 2 years ago
kwout f83d4e8699 solved 11a 2 years ago
kwout 52b8b62b91 minor improvement to readability of 10 2 years ago
kwout 42b2aa13b5 solve 10b in 10a 2 years ago
kwout 37938613b9 solved 10a 2 years ago
kwout 949a0aa1e7 solved 9b in 9a 2 years ago
kwout 343e93b59d day 1 2022 wow 2 years ago
kwout 1ba863a06f solve 9a 2 years ago
kwout 9df970d28b solve 8a and 8b in one file 2 years ago
kwout 19a76fb3cb solved 7b in 7a 2 years ago
kwout 8a13d3912c solve 7a 2 years ago
kwout a152cec986 solve day 6 (wew) 2 years ago
kwout a17271dea2 streamlined 5 even more 2 years ago
kwout 7457198179 solved 5b in 5a 2 years ago
kwout fe768eccd9 streamlined 5a 2 years ago
kwout 13b2fdd655 streamlined 4 some 2 years ago
kwout 81def1e8f8 cleaned 5a, toying with swift in 1a, updated gitignore to not include binaries 2 years ago
kwout 215c6c6f93 solved day 5a, removed useless day 4 code 2 years ago
kwout 12d8cddddd trying to mix readme reee 2 years ago
kwout c6b5a095a9 solved 4b (in 4a solver), fixed readme 2 years ago
kwout 8fa85c75cd add readme, add sample data for day1 day2, completed solution for day4a 2 years ago
kwout 1777f5bb24 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