-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
142 lines (120 loc) · 4.27 KB
/
day3.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
# Day 3, 2023
# Bug/Requirement: you have to add a BLANK row of dots above and below the text file (day3.txt)
# ...for this to work.
# Number of dots must equal to standard line length.
input = """..........
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
.........."""
with open("day3.txt") as f:
input = f.read()
# take whole input as one line, if next character is a number add to variable
WholeList = []
# Returns dictionary with key Position and value Number
def GetPos():
temp = ""
count = 0
for i in input:
if i.isdigit():
# Count position
count += 1
# print("int")
# Add digit to string
temp = temp+i
else:
# Count position
count += 1
# print("dot")
# Store digit string to return
wholenum = temp
if wholenum.isdigit():
# Store number by a position (i.e., rightmost digit position)
sublist = [int(wholenum),count-1]
WholeList.append(sublist)
# Reset string
temp = ""
return WholeList
def GetLineLength(Input):
count = 0
for i in Input:
if i == "\n":
return count
else:
pass
count += 1
def CheckSides(thing):
SideList = {}
for i in thing:
WL = len(str(i[0]))
LeftPos = i[1]-WL-1
RightPos = i[1]
# See if either side of number word is permissible
if (input[LeftPos] == ".") and (input[RightPos] == "."):
# if either side has a dot, return true
# print(True,thing[i],"dots")
pass
elif (input[LeftPos] == "\n") and (input[RightPos] == "."):
# print(True,thing[i],"right dot")
pass
elif (input[LeftPos] == ".") and (input[RightPos] == "\n"):
# print(True,thing[i],"left dot")
pass
else:
SideList[i[1]]=i[0]
return(SideList)
# print(CheckSides(GetPos()))
# See if every other character that is not on side passes
def EverythingElse(thing):
temp = {}
temp2 = {}
for i in thing:
LL = GetLineLength(input)+1
WL = len(str(i[0]))
WL = int(WL)+2
checklist = [".","\n",1,2,3,4,5,6,7,8,9,0]
for num in range(WL):
# We want a number as a position to match the input position
upperstart = i[1]-LL-WL+num+1
lowerstart = i[1]+LL-WL+num+1
# print(input[upperstart],input[lowerstart])
# if upperstart >= 0 and lowerstart <= len(input):
if input[upperstart] in checklist and input[lowerstart] in checklist:
pass
else:
temp[i[1]] = i[0]
# print("adding "+str(temp))
temp2.update(temp)
temp.clear()
return(temp2)
def SumPartNumbers():
# Create list of invalid numbers
test1=CheckSides(Dict)
test2=EverythingElse(Dict)
print(test1,"Check sides \n")
print(test2,"Check outer but not sides \n")
# print(test1.intersection(test2))
# SumParts = sum((test1.intersection(test2)))
# print(SumParts)
# print(sum(c))
newDict = []
for item in Dict:
newDict.append(item[0])
GrandTotalSum = sum(newDict)
verycool = test1|test2
print("Test1: ",test1,"Test2: ",test2,"Final: ",verycool)
CollectValues = verycool.values()
# for item in newDict:
# remove()
SumValues = sum(CollectValues)
print(str(GrandTotalSum) + " - " + str(SumValues) + " = " + str(GrandTotalSum-SumValues))
print("it's the second value above ^ : " + str(SumValues))
Dict = GetPos()
SumPartNumbers()