-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.py
156 lines (122 loc) · 3.84 KB
/
day4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import copy
import numpy as np
import pandas as pd
import matplotlib
def getManAdjacentIndices(row, col, arr):
m = len(arr)
n = len(arr[0])
adjacent_indices = []
if row > 0:
adjacent_indices.append((row - 1, col))
if row+1 < m:
adjacent_indices.append((row + 1, col))
if col > 0:
adjacent_indices.append((row, col - 1))
if col+1 < n:
adjacent_indices.append((row, col + 1))
return adjacent_indices
def getAdjacentIndicies(x, y, arr):
position = (x, y)
adj = []
for dx in range(-1, 2):
for dy in range(-1, 2):
rangeX = range(0, arr.shape[0]) # X bounds
rangeY = range(0, arr.shape[1]) # Y bounds
(newX, newY) = (position[0] + dx, position[1] + dy) # adjacent cell
if (newX in rangeX) and (newY in rangeY) and (dx, dy) != (0, 0):
adj.append((newX, newY))
return adj
def extractKeys(string):
alphaRemoved = re.sub("[^0-9]", "", string)
num = int(alphaRemoved)
numRemoved = re.sub(r"\d+", "", string).replace(" ", "")
return [numRemoved, num]
def lazyAdd(dict, key, num):
if key in dict:
dict[key] += num
else:
dict[key] = num
def safeIndex(arr, row, col):
if row > -1 and row < len(arr):
slice = arr[row]
# print(slice)
if col > -1 and col < len(slice):
return slice[col]
else:
return "."
else:
return "."
import re
def getNumTotal(cardMatches, card):
# print(card)
total = 1
matches = cardMatches[card]
totals = [getNumTotal(cardMatches, card+index+1) for index in range(matches)]
total += sum(totals)
return total
if __name__ == '__main__':
dataFile = open("day4.txt", "r")
data = dataFile.read()
data = data.split('\n')
data = data[:-1]
runningSum = 0
cardMatches = {}
for index, dataPoint in enumerate(data):
split1 = dataPoint.split(":")
split2 = split1[-1].split("|")
table = split2[0].split(' ')
hand = split2[-1].split(' ')
intTable = []
for card in table:
try:
intTable.append(int(card))
except:
pass
intHand = []
for card in hand:
try:
intHand.append(int(card))
except:
pass
totalWorth = 0
matches = 0
for card in hand:
if card in table and not (card == " ") and not (card == ''):
if totalWorth == 0:
totalWorth = 1
else:
totalWorth *= 2
matches += 1
cardMatches.update({index + 1: matches})
runningSum += totalWorth
print(cardMatches) # part 1
runningCards = 0
for card in list(cardMatches.keys()):
print(card)
# runningCards += 1
runningCards += getNumTotal(cardMatches, card)
print(runningCards) # part 2
# # matchesToProcess = copy.deepcopy(cardMatches)
# allMatches = list(cardMatches.keys())
# newMatches = list(cardMatches.keys())
# processed = False
# while processed == False:
# matchesToProcess = newMatches
# newMatches = []
# nonZeroProcessed = False
# print("hhhhhhhhhhhhhhhhhhh")
# for card in matchesToProcess:
# print("----------")
# print(card)
# print(cardMatches[card])
# start = card
# if not (cardMatches[card] == 0):
# nonZeroProcessed = True
# for index in range(cardMatches[card]):
# print(start+index+1)
# newMatches.append(start+index+1)
# allMatches.append(start+index+1)
# if nonZeroProcessed == False:
# processed = True
# print(len(allMatches))
# print(runningSum)