solve_day10 :: (test: bool) {
contents := read_entire_file("inputs/day10.txt");
lines := split(contents, "\n");
array_reverse(lines);
start_pos: Coord;
for lines {
index := find_index_from_left(it, "S");
if index != -1 {
start_pos = .{
x = index,
y = it_index,
};
break;
}
}
steps := 0;
dir_1: Coord;
pos_1: Coord = start_pos;
dir_2: Coord;
pos_2: Coord = start_pos;
found_connections := 0;
dirs := Coord.[ .{y = 1}, .{y = -1}, .{x = 1}, .{x = -1} ];
for dir: dirs {
if (start_pos.y + dir.y >= 0 && start_pos.y + dir.y < lines.count) && (start_pos.x + dir.x >= 0 && start_pos.x + dir.x < lines.count) && is_connected(dir, lines[start_pos.y + dir.y][start_pos.x + dir.x]) {
if found_connections {
dir_2 = dir;
} else {
dir_1 = dir;
}
found_connections = 1;
}
}
while (pos_1.x == start_pos.x && pos_1.y == start_pos.y) || (pos_1.x != pos_2.x || pos_1.y != pos_2.y) {
defer steps += 1;
c := *lines[pos_1.y][pos_1.x];
if c.* == {
case #char "L"; #through;
case #char "J"; #through;
case #char "|";
c.* = #char "x";
case;
c.* = #char ",";
}
c = *lines[pos_2.y][pos_2.x];
if c.* == {
case #char "L"; #through;
case #char "J"; #through;
case #char "|";
c.* = #char "x";
case;
c.* = #char ",";
}
pos_1.x += dir_1.x;
pos_1.y += dir_1.y;
pos_2.x += dir_2.x;
pos_2.y += dir_2.y;
{
next, found := next_dir(dir_1, lines[pos_1.y][pos_1.x]);
assert(found, "Failed to get next dir on %, %\n", pos_1.x, pos_1.y);
dir_1 = next;
next, found = next_dir(dir_2, lines[pos_2.y][pos_2.x]);
assert(found, "Failed to get next dir on %, %\n", pos_2.x, pos_2.y);
dir_2 = next;
}
}
c1 := *lines[pos_1.y][pos_1.x];
if c1.* == {
case #char "L"; #through;
case #char "J"; #through;
case #char "|";
c1.* = #char "x";
case;
c1.* = #char ",";
}
inside := 0;
outside := 0;
for line: lines {
for x:0..line.count - 1 {
if line[x] == #char "," || line[x] == #char "x" {
continue;
}
borders := 0;
for c: x..line.count - 1 {
if line[c] == #char "x" {
borders += 1;
}
}
if borders % 2 == 1 {
line[x] = #char "I";
inside += 1;
} else {
line[x] = #char "O";
outside += 1;
}
}
}
array_reverse(lines);
print("Part 1: %\n", steps);
print("Part 2: %\n", inside);
}
is_connected :: (dir: Coord, c: u8) -> bool {
if dir.y == 1 {
return c == #char "|" || c == #char "F" || c == #char "7";
} else if dir.y == -1 {
return c == #char "|" || c == #char "J" || c == #char "L";
} else if dir.x == 1 {
return c == #char "-" || c == #char "J" || c == #char "7";
} else if dir.x == -1 {
return c == #char "-" || c == #char "F" || c == #char "L";
}
return false;
}
next_dir :: (dir: Coord, c: u8) -> (dir: Coord, success: bool) {
dummy: Coord = ---;
if dir.x == 1 {
if c == {
case #char "J";
return .{ x = 0, y = 1 }, true;
case #char "7";
return .{ x = 0, y = -1 }, true;
case #char "-";
return dir, true;
}
} else if dir.x == -1 {
if c == {
case #char "L";
return .{ x = 0, y = 1 }, true;
case #char "F";
return .{ x = 0, y = -1 }, true;
case #char "-";
return dir, true;
}
} else if dir.y == 1 {
if c == {
case #char "F";
return .{ x = 1, y = 0 }, true;
case #char "7";
return .{ x = -1, y = 0 }, true;
case #char "|";
return dir, true;
}
} else if dir.y == -1 {
if c == {
case #char "L";
return .{ x = 1, y = 0 }, true;
case #char "J";
return .{ x = -1, y = 0 }, true;
case #char "|";
return dir, true;
}
}
return dummy, false;
}
array_reverse :: (arr: []$T) {
for 0..(arr.count - 1) / 2 {
arr[it], arr[arr.count - it - 1] = arr[arr.count - it - 1], arr[it];
}
}
#scope_file
Coord :: struct {
x: s64 = 0;
y: s64 = 0;
}