Skip to content

Instantly share code, notes, and snippets.

@fxn
Created December 18, 2020 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxn/335f5736f9578cb2358ed6b488330844 to your computer and use it in GitHub Desktop.
Save fxn/335f5736f9578cb2358ed6b488330844 to your computer and use it in GitHub Desktop.
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