Compare commits

..

8 Commits

  1. 49
      2022/8.py
  2. 2
      2023/3.py
  3. 43
      2023/5.py
  4. 30
      2023/6.py
  5. 67
      2023/7.py

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

@ -1,4 +1,4 @@
lines = [i for i in open("input.txt", 'r').read().splitlines()]
lines = open("input.txt", 'r').read().splitlines()
sum = 0
sum2 = 0

@ -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))
Loading…
Cancel
Save