finish day 6
This commit is contained in:
103
day6.jai
103
day6.jai
@@ -17,30 +17,103 @@ Operation :: struct {
|
|||||||
|
|
||||||
main :: () {
|
main :: () {
|
||||||
content := read_entire_file("day6.input");
|
content := read_entire_file("day6.input");
|
||||||
solve_first(content);
|
// solve_first(content);
|
||||||
|
solve_second(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
solve_second :: (input: string) {
|
||||||
|
lines := split(input, "\n");
|
||||||
|
num_values_per_operation := lines.count - 1;
|
||||||
|
in_progress_values: [..]string;
|
||||||
|
operations: [..]Operation;
|
||||||
|
for < char_index: 0..lines[0].count - 1 {
|
||||||
|
sb: String_Builder;
|
||||||
|
for line_index: 0..lines.count - 1 {
|
||||||
|
if lines[line_index].count == 0 continue;
|
||||||
|
c := lines[line_index][char_index];
|
||||||
|
if c == {
|
||||||
|
case #char " "; continue;
|
||||||
|
case #char "+"; {
|
||||||
|
op: Operation;
|
||||||
|
for s: in_progress_values {
|
||||||
|
value, success := parse_int(*s);
|
||||||
|
if success {
|
||||||
|
array_add(*op.values, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s := builder_to_string(*sb);
|
||||||
|
value, success := parse_int(*s);
|
||||||
|
if success {
|
||||||
|
array_add(*op.values, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
array_reset_keeping_memory(*in_progress_values);
|
||||||
|
op.op = .Plus;
|
||||||
|
array_add(*operations, op);
|
||||||
|
}
|
||||||
|
case #char "*"; {
|
||||||
|
op: Operation;
|
||||||
|
for s: in_progress_values {
|
||||||
|
parse_me := s;
|
||||||
|
value, success := parse_int(*parse_me);
|
||||||
|
if success {
|
||||||
|
array_add(*op.values, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s := builder_to_string(*sb);
|
||||||
|
value, success := parse_int(*s);
|
||||||
|
if success {
|
||||||
|
array_add(*op.values, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
array_reset_keeping_memory(*in_progress_values);
|
||||||
|
op.op = .Mult;
|
||||||
|
array_add(*operations, op);
|
||||||
|
}
|
||||||
|
case; {
|
||||||
|
append(*sb, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if builder_string_length(*sb) != 0 {
|
||||||
|
array_add(*in_progress_values, builder_to_string(*sb));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
total := 0;
|
||||||
|
for operations total += evaluate(it);
|
||||||
|
|
||||||
|
print("%\n", total);
|
||||||
}
|
}
|
||||||
|
|
||||||
solve_first :: (input: string) {
|
solve_first :: (input: string) {
|
||||||
operations := parse_input(input);
|
operations := parse_input(input);
|
||||||
total := 0;
|
total := 0;
|
||||||
for operations {
|
for operations {
|
||||||
result := 0;
|
total += evaluate(it);
|
||||||
if it.op == .Plus {
|
|
||||||
for it.values {
|
|
||||||
result += it;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = 1;
|
|
||||||
for it.values {
|
|
||||||
result *= it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
total += result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print("%\n", total);
|
print("%\n", total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evaluate :: (operation: Operation) -> int {
|
||||||
|
result := 0;
|
||||||
|
if operation.op == .Plus {
|
||||||
|
for operation.values {
|
||||||
|
result += it;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = 1;
|
||||||
|
for operation.values {
|
||||||
|
result *= it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
parse_input :: (input: string) -> [..]Operation {
|
parse_input :: (input: string) -> [..]Operation {
|
||||||
result: [..]Operation;
|
result: [..]Operation;
|
||||||
lines := split(input, #char "\n");
|
lines := split(input, #char "\n");
|
||||||
@@ -81,6 +154,10 @@ parse_input :: (input: string) -> [..]Operation {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
to_string :: (c: *u8) -> string {
|
||||||
|
return .{data=c, count=1};
|
||||||
|
}
|
||||||
|
|
||||||
#import "Basic";
|
#import "Basic";
|
||||||
#import "File";
|
#import "File";
|
||||||
#import "Print_Vars";
|
#import "Print_Vars";
|
||||||
|
|||||||
Reference in New Issue
Block a user