Skip to content

Commit ea7ad5a

Browse files
committed
slow, but works
1 parent cb8aaf6 commit ea7ad5a

File tree

8 files changed

+341
-0
lines changed

8 files changed

+341
-0
lines changed

day-4/input.txt

Lines changed: 208 additions & 0 deletions
Large diffs are not rendered by default.

day-4/part-one.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Scratchcards:
3+
4+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
5+
6+
Each card # has [winning numbers] | [your numbers]
7+
Goal: which of the numbers I have appear in the winning numbers?
8+
9+
My card 1 numbers contain 4 winning nubmers:
10+
48, 83, 17, and 86
11+
12+
meaning I get 2^(matches-1) or 8 points
13+
14+
sum up the points of all cards
15+
"""
16+
17+
# parse input
18+
19+
with open("input.txt") as f:
20+
lines = f.readlines()
21+
lines = [line.strip() for line in lines]
22+
23+
winners = []
24+
mine = []
25+
for line in lines:
26+
line = line.split(" | ")
27+
# process winners
28+
t = line[0]
29+
t = t.strip().split(": ")
30+
t = t[1].strip().split(" ")
31+
t = [n for n in t if n != ""]
32+
w = [int(n) for n in t]
33+
winners.append(w)
34+
35+
# process mine
36+
m = line[1]
37+
m = m.split(" ")
38+
m = [n.strip() for n in m]
39+
m = [n for n in m if n != ""]
40+
m = [int(n) for n in m]
41+
mine.append(m)
42+
43+
print("WINNERS:")
44+
for w in winners:
45+
print(w)
46+
47+
print("\n\n MINE:")
48+
for m in mine:
49+
print(m)
50+
51+
s = 0
52+
for i, card in enumerate(mine):
53+
print("\n\n card {}".format(i+1))
54+
matches = 0
55+
for n in card:
56+
if n in winners[i]:
57+
matches += 1
58+
print("found {} in winners, matches is now: {} ".format(n, matches))
59+
if matches > 0:
60+
s += 2 ** (matches - 1)
61+
print("sum is now {}".format(s))
62+
63+
print("SUM: {}".format(s))

day-4/part-two.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
now, scratchcard causes you to win more scratchcards, a number equal to the # of winning numbers
3+
4+
you win copies of the next scratchcards in the list - so if you have 4 winning numbers, you get 1 copy of the next 4 cards
5+
"""
6+
7+
# parse input
8+
9+
with open("input.txt") as f:
10+
lines = f.readlines()
11+
lines = [line.strip() for line in lines]
12+
13+
winners = []
14+
mine = []
15+
for line in lines:
16+
line = line.split(" | ")
17+
# process winners
18+
t = line[0]
19+
t = t.strip().split(": ")
20+
t = t[1].strip().split(" ")
21+
t = [n for n in t if n != ""]
22+
w = [int(n) for n in t]
23+
winners.append(w)
24+
25+
# process mine
26+
m = line[1]
27+
m = m.split(" ")
28+
m = [n.strip() for n in m]
29+
m = [n for n in m if n != ""]
30+
m = [int(n) for n in m]
31+
mine.append(m)
32+
33+
# indexed hash of cards and the winning numbers
34+
all_cards = {}
35+
for i in range(0, len(winners)):
36+
all_cards[i] = (winners[i], mine[i])
37+
print(all_cards)
38+
39+
40+
# the number of times to compute card i (1 indexed)
41+
# I really hope this works and I don't need recursion.... it's too soon
42+
compute = {}
43+
for i in range(0, len(mine)):
44+
compute[i] = 1
45+
46+
s = 0
47+
for i, card in enumerate(mine):
48+
print("I need to process card {} {} times".format(i+1, compute[i]))
49+
for num_times in range(0, compute[i]):
50+
matches = 0
51+
for n in card:
52+
if n in winners[i]:
53+
matches += 1
54+
if matches > 0:
55+
s += matches
56+
compute[i] = compute[i]-1
57+
for j in range(0, matches):
58+
if i + j + 1 in compute:
59+
compute[i + j + 1 ] = compute[i + j + 1 ] + 1
60+
else:
61+
compute[i + j + 1] = 1
62+
63+
s += len(mine)
64+
print(s)

day-4/small.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
2+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
3+
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
4+
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
5+
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
6+
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

template/input.txt

Whitespace-only changes.

template/part-one.py

Whitespace-only changes.

template/part-two.py

Whitespace-only changes.

template/small.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)