88 lines
2.0 KiB
Plaintext
88 lines
2.0 KiB
Plaintext
sample :: #string END
|
|
123 328 51 64
|
|
45 64 387 23
|
|
6 98 215 314
|
|
* + * +
|
|
END
|
|
|
|
Operation_Kind :: enum {
|
|
Plus;
|
|
Mult;
|
|
}
|
|
|
|
Operation :: struct {
|
|
values: [..]int;
|
|
op: Operation_Kind;
|
|
}
|
|
|
|
main :: () {
|
|
content := read_entire_file("day6.input");
|
|
solve_first(content);
|
|
}
|
|
|
|
solve_first :: (input: string) {
|
|
operations := parse_input(input);
|
|
total := 0;
|
|
for operations {
|
|
result := 0;
|
|
if it.op == .Plus {
|
|
for it.values {
|
|
result += it;
|
|
}
|
|
} else {
|
|
result = 1;
|
|
for it.values {
|
|
result *= it;
|
|
}
|
|
}
|
|
total += result;
|
|
}
|
|
|
|
print("%\n", total);
|
|
}
|
|
|
|
parse_input :: (input: string) -> [..]Operation {
|
|
result: [..]Operation;
|
|
lines := split(input, #char "\n");
|
|
assert(lines.count > 0);
|
|
|
|
for line, line_index: lines {
|
|
found := true;
|
|
right := line;
|
|
op_index := 0;
|
|
while found {
|
|
defer op_index += 1;
|
|
found, left:, right = split_from_left(right, #char " ");
|
|
while left.count == 0 && found {
|
|
found, left, right = split_from_left(right, #char " ");
|
|
}
|
|
|
|
if found || left.count != 0 {
|
|
l := left;
|
|
val, success := parse_int(*l);
|
|
if !success {
|
|
if left == {
|
|
case "*"; result[op_index].op = .Mult;
|
|
case "+"; result[op_index].op = .Plus;
|
|
}
|
|
} else {
|
|
if line_index == 0 {
|
|
values: [..]int;
|
|
array_add(*values, val);
|
|
array_add(*result, .{values=values});
|
|
} else {
|
|
array_add(*result[op_index].values, val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#import "Basic";
|
|
#import "File";
|
|
#import "Print_Vars";
|
|
#import "String";
|