Created
December 18, 2020 15:30
-
-
Save fxn/335f5736f9578cb2358ed6b488330844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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