Advertisement
icarussiano

day 3 Python/Julia

Dec 3rd, 2024 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #Python one-liners
  2. import re
  3. print(sum(int(x)*int(y) for x,y in re.findall(r'mul\((\d{1,3}),(\d{1,3})\)',open('input').read())))
  4. print((en := True) and sum(int(x) * int(y) * en if not (do or dont) else (en := do!='') * 0 for x, y, dont, do in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)|(don't\(\))|(do\(\))", open('input').read())))
  5.  
  6. #Python
  7. import re
  8. s=open('input').read()
  9. pattern=r'mul\((\d{1,3}),(\d{1,3})\)'
  10. pattern2=r"mul\((\d{1,3}),(\d{1,3})\)|(don't\(\))|(do\(\))"
  11. print(sum(int(x)*int(y) for x,y in re.findall(pattern,s)))
  12. part2=0
  13. en=True
  14. for x, y, dont, do in re.findall(pattern2,s):
  15.     if dont or do:
  16.         en = do!=''
  17.     elif en:
  18.         part2 += int(x)*int(y)
  19. print(part2)
  20.  
  21. #Julia
  22. s = read("input", String)
  23. pattern=r"mul\((\d{1,3}),(\d{1,3})\)"
  24. println(sum(parse(Int, x) * parse(Int, y) for (x, y) in eachmatch(pattern, s)))
  25. s = join([split(x, "don't()")[1] for x in split(s, "do()")], " ")
  26. println(sum(parse(Int, g[1]) * parse(Int, g[2]) for g in eachmatch(pattern, s)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement