mirror of
https://github.com/SrIzan10/adventofcode.git
synced 2026-06-06 00:46:56 +00:00
chore: move 2022 to main repo
This commit is contained in:
21
2022/1/main.py
Normal file
21
2022/1/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
input = open('input.txt', 'r').read().split('\n')
|
||||
|
||||
def part1():
|
||||
calList = []
|
||||
sum = 0
|
||||
for i in input:
|
||||
if i == '':
|
||||
calList.append(sum)
|
||||
sum = 0
|
||||
continue
|
||||
sum += int(i)
|
||||
|
||||
calList.sort(reverse=True)
|
||||
return calList
|
||||
|
||||
def part2():
|
||||
threeHighest = part1()[:3]
|
||||
return sum(threeHighest)
|
||||
|
||||
print('Part1:', part1()[0])
|
||||
print('Part2:', part2())
|
||||
105
2022/2/main.py
Normal file
105
2022/2/main.py
Normal file
@@ -0,0 +1,105 @@
|
||||
input = open('input.txt', 'r').read().split('\n')
|
||||
|
||||
# play
|
||||
p = {
|
||||
'A': 1,
|
||||
'B': 2,
|
||||
'C': 3,
|
||||
'X': 1,
|
||||
'Y': 2,
|
||||
'Z': 3
|
||||
}
|
||||
playerInv = {
|
||||
1: 'A',
|
||||
2: 'B',
|
||||
3: 'C'
|
||||
}
|
||||
opponentInv = {
|
||||
1: 'X',
|
||||
2: 'Y',
|
||||
3: 'Z'
|
||||
}
|
||||
|
||||
def part1(input=input,isPart2=False):
|
||||
results = []
|
||||
sum = 0
|
||||
|
||||
for i in input:
|
||||
for pointsKey in p:
|
||||
i = i.replace(pointsKey, str(p[pointsKey]))
|
||||
i = i.split()
|
||||
|
||||
opponent = int(i[0])
|
||||
player = int(i[1])
|
||||
|
||||
if isPart2:
|
||||
opponent = int(i[1])
|
||||
player = int(i[0])
|
||||
|
||||
winner = rockpaperscissors(opponent, player)
|
||||
finalPoints = 0
|
||||
match winner:
|
||||
case 'player':
|
||||
finalPoints = player + 6
|
||||
case 'tie':
|
||||
finalPoints = player + 3
|
||||
case 'opponent':
|
||||
finalPoints = player
|
||||
|
||||
results.append({ 'opponent': opponent, 'player': player, 'winner': winner, 'finalPoints': finalPoints })
|
||||
|
||||
for r in results:
|
||||
sum += r['finalPoints']
|
||||
|
||||
return { 'results': results, 'sum': sum }
|
||||
|
||||
|
||||
def part2():
|
||||
results = part1()['results']
|
||||
loss = { "A": "C", "B": "A", "C": "B" }
|
||||
wins = { "A": "B", "B": "C", "C": "A" }
|
||||
sum = 0
|
||||
simInput = []
|
||||
|
||||
# left hand is the player, right hand is the opponent
|
||||
for r in results:
|
||||
player = r['player']
|
||||
opponent = r['opponent']
|
||||
# inverted values for better treatment
|
||||
player = playerInv[player]
|
||||
opponent = opponentInv[opponent]
|
||||
|
||||
if opponent == 'X':
|
||||
player = loss[player]
|
||||
elif opponent == 'Y':
|
||||
player = player
|
||||
elif opponent == 'Z':
|
||||
player = wins[player]
|
||||
|
||||
simInput.append(f"{player} {opponent}")
|
||||
|
||||
calc = part1(simInput, True)
|
||||
return calc['sum']
|
||||
|
||||
def rockpaperscissors(opponent, player):
|
||||
opponent = int(opponent)
|
||||
player = int(player)
|
||||
if opponent == player:
|
||||
return "tie"
|
||||
elif opponent == 1 and player == 2:
|
||||
return "player"
|
||||
elif opponent == 2 and player == 1:
|
||||
return "opponent"
|
||||
elif opponent == 1 and player == 3:
|
||||
return "opponent"
|
||||
elif opponent == 3 and player == 1:
|
||||
return "player"
|
||||
elif opponent == 2 and player == 3:
|
||||
return "player"
|
||||
elif opponent == 3 and player == 2:
|
||||
return "opponent"
|
||||
else:
|
||||
raise ValueError("Make sure opponent and player have right numbers.")
|
||||
|
||||
print('Part1:', part1()['sum'])
|
||||
print('Part2:', part2())
|
||||
33
2022/README.md
Normal file
33
2022/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# advent of code 2022
|
||||
|
||||
probably my first python "project"
|
||||
|
||||
will keep it as zero-dependency as possible
|
||||
|
||||
*nearly* zero github copilot was used
|
||||
|
||||
# checklist
|
||||
- [x] day 1
|
||||
- [x] day 2
|
||||
- [ ] day 3
|
||||
- [ ] day 4
|
||||
- [ ] day 5
|
||||
- [ ] day 6
|
||||
- [ ] day 7
|
||||
- [ ] day 8
|
||||
- [ ] day 9
|
||||
- [ ] day 10
|
||||
- [ ] day 11
|
||||
- [ ] day 12
|
||||
- [ ] day 13
|
||||
- [ ] day 14
|
||||
- [ ] day 15
|
||||
- [ ] day 16
|
||||
- [ ] day 17
|
||||
- [ ] day 18
|
||||
- [ ] day 19
|
||||
- [ ] day 20
|
||||
- [ ] day 21
|
||||
- [ ] day 22
|
||||
- [ ] day 23
|
||||
- [ ] day 24
|
||||
10
2022/template.py
Normal file
10
2022/template.py
Normal file
@@ -0,0 +1,10 @@
|
||||
input = open('input.txt', 'r').read().split('\n')
|
||||
|
||||
def part1():
|
||||
return
|
||||
|
||||
def part2():
|
||||
return
|
||||
|
||||
print('Part1:', part1())
|
||||
print('Part2:', part2())
|
||||
Reference in New Issue
Block a user