Guest User

Untitled

a guest
Dec 1st, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. import Cocoa
  2.  
  3. let text = """
  4. 1abc2
  5. pqr3stu8vwx
  6. a1b2c3d4e5f
  7. treb7uchet
  8. """
  9.  
  10. func getNumber(s: String) -> Int{
  11. let digits = s.filter{ $0.isNumber }
  12. if let firstDigit = digits.first {
  13. if let lastDigit = digits.last {
  14. return Int("\(firstDigit)\(lastDigit)") ?? 0
  15. }
  16. }
  17. return 0
  18. }
  19. let fileURL = Bundle.main.url(forResource: "input_1", withExtension: "txt")
  20. let content = try String(contentsOf: fileURL!, encoding: .utf8)
  21.  
  22. var number = 0
  23. for line in content.split(separator: "\n") {
  24. number += getNumber(s: String(line))
  25. }
  26.  
  27. print(number)
  28.  
Advertisement
Add Comment
Please, Sign In to add comment