Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- def convertCharToInt(c):
- return ord(c) - 96 if c.islower() else ord(c) - 38
- def part1():
- with(open("2022\day3i.txt", 'r')) as f:
- duplicates = []
- for line in f:
- current = []
- left = line[:len(line) // 2]
- right = line[len(line) // 2 :].replace('\n', '')
- for c in left:
- if c in right and c not in current:
- current.append(c)
- right = right.replace(c, '', 1)
- duplicates += current
- return sum([convertCharToInt(c) for c in duplicates])
- def part2():
- with(open("2022\day3i.txt", 'r')) as f:
- keys = []
- lines = f.readlines()
- for i in range(0, len(lines), 3):
- current = []
- line = lines[i].replace('\n', '')
- for c in line:
- if c in lines[i + 1] and c in lines[i + 2] and c not in current:
- keys.append(c)
- current.append(c)
- return sum([convertCharToInt(i) for i in keys])
- start = timeit.default_timer()
- print(f"Result of part 1: {part1()}")
- stop = timeit.default_timer()
- execution_time = stop - start
- print("Part 1 Executed in "+str(execution_time))
- start = timeit.default_timer()
- print(f"Result of part 2: {part2()}")
- stop = timeit.default_timer()
- execution_time = stop - start
- print("Part 2 Executed in "+str(execution_time))
- True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement