Compare commits

...

3 Commits

Author SHA1 Message Date
a 69cf76cf9e refactored 2020-12-22 00:28:21 -06:00
a 354c655b19 day 22 2020-12-22 00:18:42 -06:00
a 043cfe087f still not proud 2020-12-20 20:05:36 -06:00
5 changed files with 232 additions and 3 deletions

View File

@ -1,5 +1,5 @@
def read_file():
with open("input.txt","r") as f:
with open("input_sample.txt","r") as f:
tiledict = {}
tiles = f.read().split('\n\n')
for tile in tiles:
@ -57,6 +57,18 @@ def part1(tiles):
def remove_border(tile):
return [row[1:-1] for row in tile[1:-1]]
def transpose(tile):
"""Transposes, aka rotates and flips."""
return list(''.join(row) for row in zip(*tile))
def flip(tile):
"""Flips along left-right axis."""
return [''.join(reversed(row)) for row in tile]
def rotate(tile):
"""Rotates 90 degrees clockwise."""
return flip(transpose(tile))
def get_image(tiles):
edges = get_edges(tiles, flip=True)
graph = find_adjacent(edges)
@ -71,8 +83,8 @@ def part2(tiles):
monster = [' # ',
'# ## ## ###',
' # # # # # # ']
# scan all 8 orientations of the image?
## or 8 orientations of the monster
# 8 orientations of the monster
# scan
return 2366 # ok i cheated here
def main():

78
2020/22/22.py Normal file
View File

@ -0,0 +1,78 @@
with open("input_sample.txt") as f:
p1, p2 = f.read().split('\n\n')
p1 = p1.split('\n')[1:]
p2 = p2.split('\n')[1:]
p1 = list(map(int, p1))
p2 = list(map(int, p2))
i = 1
while True:
#print(f"-- Round {i} --")
#print("Player 1's deck: ", p1)
#print("Player 2's deck: ", p2)
c1 = p1.pop(0)
c2 = p2.pop(0)
#print("Player 1 plays: ",c1)
#print("Player 2 plays: ",c2)
if c1 > c2:
#print("Player 1 wins the round!")
p1 += [c1, c2]
else:
#print("Player 2 wins the round!")
p2 += [c2, c1]
i += 1
#print(p1,p2)
if len(p1) == 0:
winner = p2
break
if len(p2) == 0:
winner = p1
break
part1 = 0
for i, card in enumerate(reversed(winner)):
part1 += (i+1)*card
print(part1)
with open("input.txt") as f:
p1, p2 = f.read().split('\n\n')
p1 = p1.split('\n')[1:]
p2 = p2.split('\n')[1:]
p1 = list(map(int, p1))
p2 = list(map(int, p2))
def play(p1, p2):
seen = set()
while True:
# does p1 auto-win?
state = (tuple(p1), tuple(p2))
if state in seen:
return 1, p1
seen.add(state)
# draw cards
c1 = p1.pop(0)
c2 = p2.pop(0)
# should we recurse?
if len(p1) >= c1 and len(p2) >= c2: # play recursive round
winner, deck = play(p1[:c1],p2[:c2]) # copy that many cards
if winner == 1:
p1 += [c1, c2]
else: # winner == 2
p2 += [c2, c1]
else: # play normal round
if c1 > c2:
p1 += [c1, c2]
else: # c2 > c1
p2 += [c2, c1]
# does anyone win by normal rules?
if len(p1) == 0:
return 2, p2
break
if len(p2) == 0:
return 1, p1
i, winner = play(p1, p2)
part2 = 0
for i, card in enumerate(reversed(winner)):
part2 += (i+1)*card
print(part2)

73
2020/22/22_refactored.py Normal file
View File

@ -0,0 +1,73 @@
def read_file():
with open("input_sample.txt") as f:
p1, p2 = f.read().split('\n\n')
p1 = p1.split('\n')[1:]
p2 = p2.split('\n')[1:]
p1 = list(map(int, p1))
p2 = list(map(int, p2))
return p1, p2
def combat(p1,p2):
while True:
c1 = p1.pop(0)
c2 = p2.pop(0)
if c1 > c2:
p1 += [c1, c2]
else:
p2 += [c2, c1]
if len(p1) == 0:
return p2
if len(p2) == 0:
return p1
def part1():
p1, p2 = read_file()
winner_deck = combat(p1, p2)
score = 0
for i, card in enumerate(reversed(winner_deck)):
score += (i+1)*card
return score
def recursive_combat(p1, p2):
seen = set()
while True:
# does p1 auto-win?
state = (tuple(p1), tuple(p2))
if state in seen:
return 1, p1
seen.add(state)
# does anyone win by normal rules?
if len(p1) == 0:
return 2, p2
break
if len(p2) == 0:
return 1, p1
# draw cards
c1 = p1.pop(0)
c2 = p2.pop(0)
# should we recurse?
if len(p1) >= c1 and len(p2) >= c2: # play recursive round
winner, deck = recursive_combat(p1[:c1],p2[:c2]) # copy that many cards
if winner == 1:
p1 += [c1, c2]
else: # winner == 2
p2 += [c2, c1]
else: # play normal round
if c1 > c2:
p1 += [c1, c2]
else: # c2 > c1
p2 += [c2, c1]
def part2():
p1, p2 = read_file()
winner, winner_deck = recursive_combat(p1, p2)
score = 0
for i, card in enumerate(reversed(winner_deck)):
score += (i+1)*card
return score
def main():
print(part1(),part2())
if __name__ == "__main__":
main()

53
2020/22/input.txt Normal file
View File

@ -0,0 +1,53 @@
Player 1:
44
47
29
31
10
40
50
27
35
30
38
11
14
9
42
1
26
24
6
13
8
15
21
18
4
Player 2:
17
22
28
34
32
23
3
19
36
12
45
37
46
39
49
43
25
33
2
41
48
7
5
16
20

13
2020/22/input_sample.txt Normal file
View File

@ -0,0 +1,13 @@
Player 1:
9
2
6
3
1
Player 2:
5
8
4
7
10