This commit is contained in:
m 2023-12-23 00:05:29 -05:00
parent 9354548658
commit 4154f45f8f
4 changed files with 1651 additions and 0 deletions

View File

@ -118,6 +118,16 @@ day12part2:
@echo @echo
./build/day12part2 ./build/day12part2
day13part1:
fpc src/day13/part1.pas -obuild/day13part1
@echo
./build/day13part1
day13part2:
fpc src/day13/part2.pas -obuild/day13part2
@echo
./build/day13part2
clean: clean:
rm build/* rm build/*

1350
resources/day13.txt Normal file

File diff suppressed because it is too large Load Diff

142
src/day13/part1.pas Normal file
View File

@ -0,0 +1,142 @@
{$mode objfpc}
{$RANGECHECKS ON}
program day12part1;
uses sysutils;
type
Tmap = record
map: array[1..100] of string;
height: int32;
width: int32;
end;
function max(a, b: int32): int32;
begin
max := a;
if b > a then
max := b;
end;
{ check for mirrors that lay vertically (column mirror) }
function check_v_mirror(map: Tmap; col: int32): boolean;
var
i, j: int32;
begin
check_v_mirror := true;
for j := 1 to map.height do
begin
for i := max(1, 2 * col - map.width + 1) to col do
begin
// writeln(i, ' ', col*2 - i);
if map.map[j, i] <> map.map[j, col * 2 - i + 1] then
begin
check_v_mirror := false;
break
end;
end;
if not check_v_mirror then
break;
end;
check_v_mirror := check_v_mirror;
end;
{ check for mirrors that lay vertically (column mirror) }
function check_h_mirror(map: Tmap; row: int32): boolean;
var
i, j: int32;
begin
check_h_mirror := true;
for j := 1 to map.width do
begin
for i := max(1, 2 * row - map.height + 1) to row do
begin
//writeln(i, ' ', row*2 - i + 1);
if map.map[i, j] <> map.map[row * 2 - i + 1, j] then
begin
check_h_mirror := false;
break
end;
end;
if not check_h_mirror then
break;
end;
end;
function get_mirror(map: Tmap): int32;
var
i: int32;
begin
{ check vertical ones first }
for i := 1 to map.width - 1 do
begin
if check_v_mirror(map, i) then
begin
get_mirror := i;
exit;
end;
end;
{ check horizontal ones now }
for i := 1 to map.height - 1 do
begin
if check_h_mirror(map, i) then
begin
get_mirror := i shl 6 + i shl 5 + i shl 2;
exit;
end;
end;
end;
var
map: Tmap;
file_: text;
s: string;
total, map_count: int32;
begin
assign(file_, 'resources/day13.txt');
reset(file_);
total := 0;
map_count := 0;
while not EOF(file_) do
begin
readln(file_, s);
writeln(s);
if length(s) = 0 then
begin
// come back to this
total := total + get_mirror(map);
map_count := 0;
end
else
begin
map_count := map_count + 1;
map.map[map_count] := s;
map.height := map_count;
map.width := length(s);
end;
end;
writeln(total);
end.

149
src/day13/part2.pas Normal file
View File

@ -0,0 +1,149 @@
{$mode objfpc}
{$RANGECHECKS ON}
program day12part1;
uses sysutils;
type
Tmap = record
map: array[1..100] of string;
height: int32;
width: int32;
end;
function max(a, b: int32): int32;
begin
max := a;
if b > a then
max := b;
end;
{ check for mirrors that lay vertically (column mirror) }
function check_v_mirror(map: Tmap; col: int32): boolean;
var
i, j: int32;
errors: int32;
begin
errors := 0;
for j := 1 to map.height do
begin
for i := max(1, 2 * col - map.width + 1) to col do
begin
// writeln(i, ' ', col*2 - i);
if map.map[j, i] <> map.map[j, col * 2 - i + 1] then
begin
errors := errors + 1;
if errors > 1 then
break;
end;
end;
if errors > 1 then
break;
end;
check_v_mirror := errors = 1;
end;
{ check for mirrors that lay vertically (column mirror) }
function check_h_mirror(map: Tmap; row: int32): boolean;
var
i, j: int32;
errors: int32;
begin
errors := 0;
for j := 1 to map.width do
begin
for i := max(1, 2 * row - map.height + 1) to row do
begin
//writeln(i, ' ', row*2 - i + 1);
if map.map[i, j] <> map.map[row * 2 - i + 1, j] then
begin
errors := errors + 1;
if errors > 1 then
break;
end;
end;
if errors > 1 then
break;
end;
check_h_mirror := errors = 1;
end;
function get_mirror(map: Tmap): int32;
var
i: int32;
begin
{ check vertical ones first }
for i := 1 to map.width - 1 do
begin
if check_v_mirror(map, i) then
begin
get_mirror := i;
exit;
end;
end;
{ check horizontal ones now }
for i := 1 to map.height - 1 do
begin
if check_h_mirror(map, i) then
begin
get_mirror := i shl 6 + i shl 5 + i shl 2;
exit;
end;
end;
if get_mirror = 0 then
writeln('oh fuck');
end;
var
map: Tmap;
file_: text;
s: string;
total, map_count: int32;
begin
assign(file_, 'resources/day13.txt');
reset(file_);
total := 0;
map_count := 0;
while not EOF(file_) do
begin
readln(file_, s);
if length(s) = 0 then
begin
total := total + get_mirror(map);
map_count := 0;
end
else
begin
map_count := map_count + 1;
map.map[map_count] := s;
map.height := map_count;
map.width := length(s);
end;
end;
writeln(total);
end.