Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3afc4a7

Browse files
committed
Various improvements
1 parent23f1159 commit3afc4a7

File tree

7 files changed

+50
-75
lines changed

7 files changed

+50
-75
lines changed

‎2020/02-Password Philosophy.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,20 @@ def words(s: str):
3939
"expected": ["2","1"],
4040
}
4141

42-
test="WD"
42+
test="real"
4343
input_file=os.path.join(
4444
os.path.dirname(__file__),
4545
"Inputs",
46-
os.path.basename(__file__).replace(".py",".WD.txt"),
46+
os.path.basename(__file__).replace(".py",".txt"),
4747
)
4848
test_data[test]= {
4949
"input":open(input_file,"r+").read(),
50-
"expected": ["Unknown","Unknown"],
51-
}
52-
53-
test="Twitter"
54-
input_file=os.path.join(
55-
os.path.dirname(__file__),
56-
"Inputs",
57-
os.path.basename(__file__).replace(".py",".Twitter.txt"),
58-
)
59-
test_data[test]= {
60-
"input":open(input_file,"r+").read(),
61-
"expected": ["447","Unknown"],
50+
"expected": ["447","249"],
6251
}
6352

6453
# -------------------------------- Control program execution ------------------------- #
6554

66-
case_to_test="Twitter"
55+
case_to_test="real"
6756
part_to_test=2
6857

6958
# -------------------------------- Initialize some variables ------------------------- #

‎2020/04-Passport Processing.py

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -106,68 +106,52 @@ def words(s: str):
106106

107107
# -------------------------------- Actual code execution ----------------------------- #
108108

109-
required_fields= ["byr","iyr","eyr","hgt","hcl","ecl","pid"]
109+
110+
classPassport:
111+
required_fields= ["byr","iyr","eyr","hgt","hcl","ecl","pid"]
112+
113+
validations= {
114+
"byr":lambdayear:yearinrange(1920,2002+1),
115+
"iyr":lambdayear:yearinrange(2010,2020+1),
116+
"eyr":lambdayear:yearinrange(2020,2030+1),
117+
"hgt":lambdadata: (
118+
data[-2:]=="cm"andint(data[:-2])inrange(150,193+1)
119+
)
120+
or (data[-2:]=="in"andint(data[:-2])inrange(59,76+1)),
121+
"hcl":lambdadata:re.match("^#[0-9a-f]{6}$",data),
122+
"ecl":lambdadata:datain ["amb","blu","brn","gry","grn","hzl","oth"],
123+
"pid":lambdadata:re.match("^[0-9]{9}$",data),
124+
}
125+
126+
def__init__(self,data):
127+
self.fields=defaultdict(str)
128+
forelementindata.split():
129+
ifelement[:3]in ("byr","iyr","eyr"):
130+
try:
131+
self.fields[element[:3]]=int(element[4:])
132+
except:
133+
self.fields[element[:3]]=element[4:]
134+
else:
135+
self.fields[element[:3]]=element[4:]
136+
137+
defhas_required_data(self):
138+
returnall([xinself.fieldsforxinself.required_fields])
139+
140+
defis_valid(self):
141+
returnall([self.validations[x](self.fields[x])forxinself.required_fields])
142+
110143

111144
passports= []
112-
i=0
113-
forstringinpuzzle_input.split("\n"):
114-
iflen(passports)>=i:
115-
passports.append("")
116-
ifstring=="":
117-
i=i+1
118-
else:
119-
passports[i]=passports[i]+" "+string
145+
forstringinpuzzle_input.split("\n\n"):
146+
passports.append(Passport(string))
120147

121148
valid_passports=0
122149

123150
ifpart_to_test==1:
124-
forpassportinpassports:
125-
ifall([x+":"inpassportforxinrequired_fields]):
126-
valid_passports=valid_passports+1
127-
151+
valid_passports=sum([1forxinpassportsifx.has_required_data()])
128152

129153
else:
130-
forpassportinpassports:
131-
ifall([x+":"inpassportforxinrequired_fields]):
132-
fields=passport.split(" ")
133-
score=0
134-
forfieldinfields:
135-
data=field.split(":")
136-
ifdata[0]=="byr":
137-
year=int(data[1])
138-
ifyear>=1920andyear<=2002:
139-
score=score+1
140-
elifdata[0]=="iyr":
141-
year=int(data[1])
142-
ifyear>=2010andyear<=2020:
143-
score=score+1
144-
elifdata[0]=="eyr":
145-
year=int(data[1])
146-
ifyear>=2020andyear<=2030:
147-
score=score+1
148-
elifdata[0]=="hgt":
149-
size=ints(data[1])[0]
150-
ifdata[1][-2:]=="cm":
151-
ifsize>=150andsize<=193:
152-
score=score+1
153-
elifdata[1][-2:]=="in":
154-
ifsize>=59andsize<=76:
155-
score=score+1
156-
elifdata[0]=="hcl":
157-
ifre.match("#[0-9a-f]{6}",data[1])andlen(data[1])==7:
158-
score=score+1
159-
print(data[0],passport)
160-
elifdata[0]=="ecl":
161-
ifdata[1]in ["amb","blu","brn","gry","grn","hzl","oth"]:
162-
score=score+1
163-
print(data[0],passport)
164-
elifdata[0]=="pid":
165-
ifre.match("[0-9]{9}",data[1])andlen(data[1])==9:
166-
score=score+1
167-
print(data[0],passport)
168-
print(passport,score)
169-
ifscore==7:
170-
valid_passports=valid_passports+1
154+
valid_passports=sum([1forxinpassportsifx.is_valid()])
171155

172156
puzzle_actual_result=valid_passports
173157

‎2020/07-Handy Haversacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def words(s: str):
145145
gold_contains["total"]+=gold_contains[combination["out"]]
146146
delgold_contains[combination["out"]]
147147

148-
print(sum(gold_contains.values()),gold_contains)
148+
# print(sum(gold_contains.values()), gold_contains)
149149

150150
puzzle_actual_result=gold_contains["total"]
151151

‎2020/08-Handheld Halting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def run(self):
9292

9393
defrun_once(self):
9494
instr=self.instructions[self.current_line]
95-
print("Before",self.current_line,self.accumulator,instr)
95+
#print("Before", self.current_line, self.accumulator, instr)
9696
self.operations[instr[0]](instr)
9797

9898
defnop(self,instr):

‎2020/09-Encoding Error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def words(s: str):
109109
ifnumber_sum<invalid_number:
110110
forbinrange(1,len(numbers)-a):
111111
number_sum+=numbers[a+b]
112-
print(a,b,number_sum,invalid_number)
112+
#print(a, b, number_sum, invalid_number)
113113
ifnumber_sum==invalid_number:
114114
puzzle_actual_result=min(numbers[a :a+b+1])+max(
115115
numbers[a :a+b+1]

‎2020/10-Adapter Array.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def words(s: str):
9393
)
9494
test_data[test]= {
9595
"input":open(input_file,"r+").read(),
96-
"expected": ["Unknown","Unknown"],
96+
"expected": ["2240","99214346656768"],
9797
}
9898

9999

@@ -142,7 +142,7 @@ def words(s: str):
142142
forjoltageinjoltages:
143143
edges[joltage]= [xforxinjoltagesifx<joltageandx>=joltage-3]
144144

145-
print(edges)
145+
#print(edges)
146146

147147
@lru_cache(maxsize=len(joltages))
148148
defcount_paths(position):
@@ -162,3 +162,5 @@ def count_paths(position):
162162
print("Expected result : "+str(puzzle_expected_result))
163163
print("Actual result : "+str(puzzle_actual_result))
164164
# Date created: 2020-12-10 06:00:02.437611
165+
# Part 1: 2020-12-10 06:04:42
166+
# Part 2: 2020-12-10 06:14:12

‎2020/grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def text_to_dots(self, text, ignore_terrain=""):
8686
The dots will have x - y * 1j as coordinates
8787
8888
:param string text: The text to convert
89-
:param sequence ignore_terrain:The gridtoconvert
89+
:param sequence ignore_terrain:Types of terraintoignore (useful for walls)
9090
"""
9191
self.dots= {}
9292

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp