Advertisement
Guest User

Day 1 part 2 code (not working)

a guest
Dec 8th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #file1 = open('./1/input.txt', 'r')
  2. #lines = file1.readlines()
  3. lines = ["oneghj","neighthreeb", "2sevenjddn2hbk5f6nine"]
  4.  
  5. words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
  6.  
  7.  
  8. sum = 0
  9.  
  10. for line in lines:
  11.     count = 0
  12.     nums = []
  13.     indeces = []
  14.     # find the words, take down the index of the first letter and the number value
  15.     for w in range(len(words)):
  16.         i = line.find(words[w])
  17.         if i > -1:
  18.             indeces.append(i)
  19.             nums.append(str(w+1))
  20.     # find the numbers, take down their indeces and values
  21.     for l in range(len(line)):
  22.         c = line[l]
  23.         if c.isdigit():
  24.             #print(l)
  25.             indeces.append(l)
  26.             nums.append(c)
  27.     # find the lowest index and max index (first and last number, if if their the same)
  28.     mn = min(indeces)
  29.     mx = max(indeces)
  30.     print(nums[indeces.index(mn)] + nums[indeces.index(mx)])
  31.     # concatenate the strings then add to the total
  32.     sum += int(nums[indeces.index(mn)] + nums[indeces.index(mx)])
  33.    
  34. print(sum)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement