Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Python one-liners
- import re
- print(sum(int(x)*int(y) for x,y in re.findall(r'mul\((\d{1,3}),(\d{1,3})\)',open('input').read())))
- 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())))
- #Python
- import re
- s=open('input').read()
- pattern=r'mul\((\d{1,3}),(\d{1,3})\)'
- pattern2=r"mul\((\d{1,3}),(\d{1,3})\)|(don't\(\))|(do\(\))"
- print(sum(int(x)*int(y) for x,y in re.findall(pattern,s)))
- part2=0
- en=True
- for x, y, dont, do in re.findall(pattern2,s):
- if dont or do:
- en = do!=''
- elif en:
- part2 += int(x)*int(y)
- print(part2)
- #Julia
- s = read("input", String)
- pattern=r"mul\((\d{1,3}),(\d{1,3})\)"
- println(sum(parse(Int, x) * parse(Int, y) for (x, y) in eachmatch(pattern, s)))
- s = join([split(x, "don't()")[1] for x in split(s, "do()")], " ")
- 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