source code migrating from gitlab

This commit is contained in:
2026-05-15 15:56:53 -04:00
commit 6e5063620d
11 changed files with 1296 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
module main;
import std::io;
fn int main(String[] args) {
if (args.len < 2) {
io::printn("Usage: HelloWorld <filename>");
return -1;
}
String filename = args[1];
int? answer = getSum(filename);
if (catch error = answer) {
io::printfn("error opening file: %s", error);
return -1;
}
io::printfn("%d", answer);
return 0;
}
fn int? getSum(String filename) {
File? file = file::open(filename, "r")!;
defer (void)file.close();
int sum = 0;
while (try line = io::treadline(&file)) {
char first = getFirst(line);
char last = getLast(line);
String digits = {first, last};
int? number = digits.to_int();
if (catch error = number) {
io::printfn("Error converting to number: %s", error);
continue;
}
sum += number;
}
return sum;
}
fn char getLast(String line) {
char last;
int length = line.len - 1;
for (int i=length; i>=0; i--) {
if (ascii::is_digit(line[i])) {
last = line[i];
break;
}
}
return last;
}
fn char getFirst(String line) {
char first;
foreach (ch : line) {
if (ascii::is_digit(ch)) {
first = ch;
break;
}
}
return first;
}