diff --git a/01/01.py b/01/01.py index 504c075..c86445f 100644 --- a/01/01.py +++ b/01/01.py @@ -1,14 +1,14 @@ -def part1(): +def read_file(): 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 b in expenses: if a + b == 2020: return a*b -def part2(): - with open('input.txt','r') as f: - expenses = [int(x) for x in f] +def part2(expenses): for a in expenses: for b in expenses: for c in expenses: @@ -16,8 +16,9 @@ def part2(): return a*b*c def main(): - print(f'Part 1: {part1()}') - print(f'Part 2: {part2()}') + expenses = read_file() + print(f'Part 1: {part1(expenses)}') + print(f'Part 2: {part2(expenses)}') if __name__ == "__main__": main() \ No newline at end of file diff --git a/02_re.py b/02_re.py new file mode 100644 index 0000000..9b0c4a8 --- /dev/null +++ b/02_re.py @@ -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=}') \ No newline at end of file