finish day 1

This commit is contained in:
2026-06-21 19:24:31 -04:00
commit 1bd2014c32
3 changed files with 4778 additions and 0 deletions

117
day1.jai Normal file
View File

@@ -0,0 +1,117 @@
sample :: #string end
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82
end
solve_first :: (input: string) {
value := 50;
time_equal_zero := 0;
for line: split(input, "\n") {
if !line || line.count == 0 continue;
direction := line[0];
amount_str := slice(line, 1);
amount := parse_int(*amount_str);
if direction == {
case "L";
value -= amount;
case "R";
value += amount;
}
while value < 100 {
value += 100;
}
while 100 <= value {
value -= 100;
}
if value == 0 {
time_equal_zero += 1;
}
}
print("%\n", time_equal_zero);
}
solve_second :: (input: string, starting_value := 50, debug_logging := false) {
value := starting_value;
turns := 0;
for line: split(input, "\n") {
started_at_zero := value == 0;
previous_value := value;
value_before_modifications: s64;
turns_for_line := 0;
if !line || line.count == 0 continue;
direction := line[0];
amount_str := slice(line, 1);
amount := parse_int(*amount_str);
if direction == {
case "L"; {
value -= amount;
value_before_modifications = value;
}
case "R"; {
value += amount;
value_before_modifications = value;
}
}
previous_started_at_zero := started_at_zero;
while value < 0 {
value += 100;
if started_at_zero {
started_at_zero = false;
} else {
turns += 1;
turns_for_line += 1;
}
}
equaled_one_hundred := value % 100 == 0 && value != 0;
while 100 <= value {
turns += 1;
turns_for_line += 1;
value -= 100;
}
if value == 0 {
if !equaled_one_hundred {
turns += 1;
turns_for_line += 1;
}
}
if (amount >= 100 || amount <= -100 || it_index == 101) && debug_logging {
print("Line % (%): Went from % to %. % turns.\n", it_index, line, previous_value, value, turns_for_line);
print_vars(previous_value, value_before_modifications, value, turns, turns_for_line, equaled_one_hundred, previous_started_at_zero, started_at_zero);
}
}
print("%\n", turns);
}
main :: () {
content := read_entire_file("day1.input");
input := "L866";
solve_second(input, starting_value = 48);
// solve_first(content);
solve_second(content);
}
slice :: inline (s: string, index: s64) -> string {
return slice(s, index, s.count - index);
}
#import "Basic";
#import "File";
#import "String";
#import "Print_Vars";