python with regex is interestingly 3.5x slower

This commit is contained in:
a 2020-12-02 12:00:53 -06:00
parent e1171830b9
commit e5946ebc42
2 changed files with 16 additions and 7 deletions

View file

@ -1,14 +1,14 @@
def part1(): def read_file():
with open('input.txt','r') as f: with open('input.txt','r') as f:
expenses = [int(x) for x in f] return [int(x) for x in f]
def part1(expenses):
for a in expenses: for a in expenses:
for b in expenses: for b in expenses:
if a + b == 2020: if a + b == 2020:
return a*b return a*b
def part2(): def part2(expenses):
with open('input.txt','r') as f:
expenses = [int(x) for x in f]
for a in expenses: for a in expenses:
for b in expenses: for b in expenses:
for c in expenses: for c in expenses:
@ -16,8 +16,9 @@ def part2():
return a*b*c return a*b*c
def main(): def main():
print(f'Part 1: {part1()}') expenses = read_file()
print(f'Part 2: {part2()}') print(f'Part 1: {part1(expenses)}')
print(f'Part 2: {part2(expenses)}')
if __name__ == "__main__": if __name__ == "__main__":
main() main()

8
02_re.py Normal file
View file

@ -0,0 +1,8 @@
import re
with open("input.txt", "r") as f:
regex = re.compile(r'(\d+)-(\d+) (\w): (\w+)')
passwords = [regex.match(x).groups() for x in f]
part1 = sum([int(a) <= w.count(c) <= int(b) for (a,b,c,w) in passwords])
print(f'{part1=}')
part2 = sum([ (c == w[int(a)-1]) != (c == w[int(b)-1]) for (a,b,c,w) in passwords])
print(f'{part2=}')