Skip to content

Instantly share code, notes, and snippets.

@fxn
Created Dec 18, 2020
Embed
What would you like to do?
import nre, strutils
proc eval(ex: string): int =
if ex.contains('('):
eval(ex.replace(re"\([^()]+\)", proc (match: string): string =
$eval(match[1 .. ^2])))
elif ex.contains('+'):
eval(ex.replace(re"(\d+) \+ (\d+)", proc (rm: RegexMatch): string =
$(parseInt(rm.captures[0]) + parseInt(rm.captures[1]))))
elif ex.contains('*'):
eval(ex.replace(re"(\d+) \* (\d+)", proc (rm: RegexMatch): string =
$(parseInt(rm.captures[0]) * parseInt(rm.captures[1]))))
else:
parseInt(ex)
var sum = 0
for line in stdin.lines:
sum += eval(line)
echo sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment