Advertisement
Guest User

Day3 AoC

a guest
Dec 4th, 2022
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | Source Code | 0 0
  1. import timeit
  2.  
  3. def convertCharToInt(c):
  4.     return ord(c) - 96 if c.islower() else ord(c) - 38
  5.  
  6. def part1():
  7.     with(open("2022\day3i.txt", 'r')) as f:
  8.         duplicates = []
  9.         for line in f:
  10.             current = []
  11.             left = line[:len(line) // 2]
  12.             right = line[len(line) // 2 :].replace('\n', '')
  13.             for c in left:
  14.                 if c in right and c not in current:
  15.                     current.append(c)
  16.                     right = right.replace(c, '', 1)
  17.             duplicates += current
  18.         return sum([convertCharToInt(c) for c in duplicates])
  19.  
  20. def part2():
  21.     with(open("2022\day3i.txt", 'r')) as f:
  22.  
  23.         keys = []
  24.         lines = f.readlines()
  25.         for i in range(0, len(lines), 3):
  26.             current = []
  27.             line = lines[i].replace('\n', '')
  28.             for c in line:
  29.                 if c in lines[i + 1] and c in lines[i + 2] and c not in current:
  30.                     keys.append(c)
  31.                     current.append(c)
  32.        
  33.     return sum([convertCharToInt(i) for i in keys])
  34.  
  35. start = timeit.default_timer()
  36. print(f"Result of part 1: {part1()}")
  37. stop = timeit.default_timer()
  38. execution_time = stop - start
  39. print("Part 1 Executed in "+str(execution_time))
  40.  
  41.  
  42. start = timeit.default_timer()
  43. print(f"Result of part 2: {part2()}")
  44. stop = timeit.default_timer()
  45. execution_time = stop - start
  46. print("Part 2 Executed in "+str(execution_time))
  47.  
  48. True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement