mirror of
https://github.com/plasmaofthedawn/2023adventofcode.git
synced 2024-10-18 14:06:25 -05:00
Compare commits
No commits in common. "dec7e6e1171505454078da12084a4b4f78cf18db" and "d17fcb67c5f9857866f7ac685ac139d1dde1d9c2" have entirely different histories.
dec7e6e117
...
d17fcb67c5
10
Makefile
10
Makefile
@ -98,16 +98,6 @@ day10part2:
|
|||||||
@echo
|
@echo
|
||||||
./build/day10part2
|
./build/day10part2
|
||||||
|
|
||||||
day11part1:
|
|
||||||
fpc src/day11/part1.pas -obuild/day11part1
|
|
||||||
@echo
|
|
||||||
./build/day11part1
|
|
||||||
|
|
||||||
day11part2:
|
|
||||||
fpc src/day11/part2.pas -obuild/day11part2
|
|
||||||
@echo
|
|
||||||
./build/day11part2
|
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm build/*
|
rm build/*
|
@ -1,140 +0,0 @@
|
|||||||
.....#...............#......................................................................#.............................#..........#......
|
|
||||||
........................................#............................#......#...............................................................
|
|
||||||
..............................#..............#....................................#..............................................#..........
|
|
||||||
..............#........................................................................#....................................................
|
|
||||||
..#...........................................................................................#.............................................
|
|
||||||
..................#.....................................#.....#......................................#.........#.......#....................
|
|
||||||
...........#......................................#.....................#...........#................................................#......
|
|
||||||
.............................#............................................................#.....................................#...........
|
|
||||||
.....#..................#.................#.........................#......................................#................................
|
|
||||||
....................................#.................#........................#..........................................................#.
|
|
||||||
................#..............................#....................................................#.......................................
|
|
||||||
..............................................................#.........................#.......................#.....................#.....
|
|
||||||
............#...............#......................#.....................#..............................#...................................
|
|
||||||
......................................#...............................................................................#.....................
|
|
||||||
.........................................................#..................................................................................
|
|
||||||
..............................................#.............................................................................................
|
|
||||||
..#.................#.....................................................................#.....#..........#..............#......#..........
|
|
||||||
..............................................................................#.............................................................
|
|
||||||
..........................................#.........................#..............................................#........................
|
|
||||||
.....#........#.............#...........................#...........................................#.......................................
|
|
||||||
..................................#...............#.............#.................#..........................#.....................#........
|
|
||||||
.......................................................................#....................................................................
|
|
||||||
...............................................................................................................................#.........#..
|
|
||||||
#.......................................#....................................................#.........................#....................
|
|
||||||
........#...............#....................................#...........................................#..................................
|
|
||||||
...............................#............................................................................................................
|
|
||||||
.........................................................#..................................................................................
|
|
||||||
..#..........................................#........................................#.............#............................#..........
|
|
||||||
......................#............................#.................#........#.............................................................
|
|
||||||
.........#.......#...........................................................................#.....................#.........#.........#....
|
|
||||||
................................#............................................................................#..............................
|
|
||||||
.........................................................................................#..................................................
|
|
||||||
.#.........................................#.........#..........#.......................................................#...................
|
|
||||||
....................#.................#...............................................................#.....................................
|
|
||||||
............................................................................#...............................................................
|
|
||||||
........................#.........................................................................................#.................#.......
|
|
||||||
.............#.......................................................#......................................................#...............
|
|
||||||
.......#.................................#.....#..............................................................#.............................
|
|
||||||
#...............................#...............................................................#.....................#.....................
|
|
||||||
.....................................#...................#.......................#.....#.......................................#............
|
|
||||||
..........................#.................#......#...................................................................................#....
|
|
||||||
..............#.............................................................................................................................
|
|
||||||
...#..........................................................................#............................................#................
|
|
||||||
.............................................................................................#.....................#......................#.
|
|
||||||
.......................................#..........................................................................................#.........
|
|
||||||
.......................................................................................................#....................................
|
|
||||||
.....#.........#.................#.................#............#.........................#.................#...............................
|
|
||||||
............................#.....................................................#.........................................................
|
|
||||||
...........................................#................................................................................................
|
|
||||||
....................................#.......................#.........................#............................#.....#..................
|
|
||||||
............................................................................................................................................
|
|
||||||
.....................#..................#................................#......#.....................................................#.....
|
|
||||||
.......................................................#.........#...............................#.....#....................................
|
|
||||||
............#.................................................................................................................#.............
|
|
||||||
..............................................#..............#.......#.................................................#....................
|
|
||||||
................................................................................................................#...........................
|
|
||||||
.........#.............................................................................#............#.......................................
|
|
||||||
.............................#...........................................#.....#............................................................
|
|
||||||
.....#..........#.....#...........#.............#.....#.............................................................................#.......
|
|
||||||
...........................................#............................................................#.....#.............................
|
|
||||||
......................................#.........................#.........................#.....................................#...........
|
|
||||||
............................................................................................................................................
|
|
||||||
.......#.....#.....#........................................#.................................#.............................................
|
|
||||||
.........................................#................................................................#.....#......#....................
|
|
||||||
........................#.......#..................#.................................#.......................................#.......#......
|
|
||||||
..............................................#.............................................................................................
|
|
||||||
...............................................................................#............................................................
|
|
||||||
......................................................#..........#..........................................................................
|
|
||||||
...#......#.................#.............#.............................#..........................#...............................#........
|
|
||||||
.............................................................#........................#..........................#.........#................
|
|
||||||
......................................#....................................................#.............................................#..
|
|
||||||
............................................................................................................................................
|
|
||||||
...............#......#.......................#.....#.......................................................#...............................
|
|
||||||
.#......#.......................#.........................#.......#....................................#....................................
|
|
||||||
...............................................................................................#........................#......#.......#....
|
|
||||||
............#...............#............#....................#........#..........#.........................................................
|
|
||||||
...............................................................................................................#............................
|
|
||||||
..........................................................................................................#.................#...............
|
|
||||||
......................#........#.........................................................................................................#..
|
|
||||||
....................................................................................#.....#.....#....................#......................
|
|
||||||
.#.........#...............#........................................#...............................................................#.......
|
|
||||||
..................#......................................................#..........................#.......................................
|
|
||||||
.................................#.............#................................................................#.......#...................
|
|
||||||
........................#......................................................#..............#................................#............
|
|
||||||
.....#..........................................................#.....#.................#..................................................#
|
|
||||||
.....................................#......#.........#...............................................#.....................................
|
|
||||||
............................................................................................................................................
|
|
||||||
............................................................................................................................................
|
|
||||||
.................................#..........................................#................#...................#..............#...........
|
|
||||||
.......#....................#............#.............................................#...................................#................
|
|
||||||
..#................#........................................#.......#..............................#........................................
|
|
||||||
........................#...........................................................................................................#.......
|
|
||||||
...........#..........................................#.........................#......................#....................................
|
|
||||||
...........................................#................................................................................................
|
|
||||||
......#...............................................................................#.........#...........................................
|
|
||||||
............................#.....................#................#........................................#...........#...................
|
|
||||||
..................#.........................................#...............................................................................
|
|
||||||
........................#...............#................................#.......#..............................#............#..............
|
|
||||||
............#......................#.................#..................................................#..................................#
|
|
||||||
..#................................................................................................#........................................
|
|
||||||
.........................................................................................................................#..................
|
|
||||||
..................................................................#................................................#................#.......
|
|
||||||
.......#.....................................#..............................................................................................
|
|
||||||
.................#...............#......#.....................#.............................................................................
|
|
||||||
....................................................................................#..................................#....................
|
|
||||||
..............................................................................................#............#................................
|
|
||||||
....#......................#................................................................................................................
|
|
||||||
......................................................#.................................#..........................#.............#..........
|
|
||||||
..........#.................................................................................................................#...............
|
|
||||||
.................................#..........#..................................#............................................................
|
|
||||||
.......................................................................................................................................#....
|
|
||||||
.......................................#..................................................................#......#..........................
|
|
||||||
.............................#.........................................................#.......#............................................
|
|
||||||
.........#....................................#............#.................................................................#..............
|
|
||||||
...#...............................................................................#.................#......................................
|
|
||||||
..................................................................................................................................#.........
|
|
||||||
....................#.............#...................................................................................#....................#
|
|
||||||
....................................................#...........#...........#...............................................................
|
|
||||||
.........................................................................................................#..................................
|
|
||||||
..........#...............................#............................#....................................................................
|
|
||||||
.............................................................#....................#.............................#..............#............
|
|
||||||
.#...............#..........................................................................#..........................................#....
|
|
||||||
............................#...............................................................................................................
|
|
||||||
............#......................................#............#..................................................................#........
|
|
||||||
.......#...............................................................................................................#....................
|
|
||||||
..................................#.......#..........................#........#...............#..........#....................#.............
|
|
||||||
....................#....................................#............................#..........................#.......................#..
|
|
||||||
..............................................................#.............................................................................
|
|
||||||
..#............................#............................................................................#...............................
|
|
||||||
..............#.....................#...............................................................................................#.......
|
|
||||||
..........................#........................................#....................................#...........#.......................
|
|
||||||
..........................................#.................................#.................#.............................................
|
|
||||||
................................................#...........#..........#.....................................................#..............
|
|
||||||
.........#..................................................................................................................................
|
|
||||||
...................................................................................#............................#.......#...................
|
|
||||||
....#.............................#.............................#.......................................................................#...
|
|
||||||
.........................................#.........................................................#........................................
|
|
||||||
....................#...........................................................................................................#...........
|
|
||||||
..............#........................................#..................................#...................#......................#......
|
|
||||||
.........#.................#........................................#...........#........................................#..................
|
|
@ -1,10 +0,0 @@
|
|||||||
...#......
|
|
||||||
.......#..
|
|
||||||
#.........
|
|
||||||
..........
|
|
||||||
......#...
|
|
||||||
.#........
|
|
||||||
.........#
|
|
||||||
..........
|
|
||||||
.......#..
|
|
||||||
#...#.....
|
|
@ -1,113 +0,0 @@
|
|||||||
program day11part1;
|
|
||||||
uses sysutils;
|
|
||||||
|
|
||||||
|
|
||||||
function dist_between(x1, x2: int32; empty_list: array of boolean): int32;
|
|
||||||
var
|
|
||||||
i: int32;
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ swap operands so x1 is always smaller}
|
|
||||||
if (x2 < x1) then
|
|
||||||
begin
|
|
||||||
i := x2;
|
|
||||||
x2 := x1;
|
|
||||||
x1 := i;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ calculate distance }
|
|
||||||
dist_between := 0;
|
|
||||||
for i := x1 to x2 - 1 do
|
|
||||||
begin
|
|
||||||
|
|
||||||
dist_between := dist_between + 1;
|
|
||||||
|
|
||||||
{ if gap, increment by an additional 1 }
|
|
||||||
if empty_list[i] then
|
|
||||||
dist_between := dist_between + 1;
|
|
||||||
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function galaxy_dist(g1, g2: array of int64; row_list, col_list: array of boolean): int32;
|
|
||||||
begin
|
|
||||||
galaxy_dist := dist_between(g1[0], g2[0], col_list) + dist_between(g1[1], g2[1], row_list);
|
|
||||||
end;
|
|
||||||
|
|
||||||
var
|
|
||||||
file_: text;
|
|
||||||
|
|
||||||
str: string;
|
|
||||||
|
|
||||||
galaxies: array[0..1000, 0..1] of int64;
|
|
||||||
galaxy_count: int32;
|
|
||||||
|
|
||||||
column_empty: array[1..1000] of boolean;
|
|
||||||
row_empty: array[1..1000] of boolean;
|
|
||||||
|
|
||||||
i, j: int32;
|
|
||||||
count: int32;
|
|
||||||
|
|
||||||
sum: int32;
|
|
||||||
|
|
||||||
begin
|
|
||||||
{ open inp.txt for reading }
|
|
||||||
assign(file_, 'resources/day11.txt');
|
|
||||||
reset(file_);
|
|
||||||
|
|
||||||
{ set row/cols to empty }
|
|
||||||
for i := 0 to 1000 do
|
|
||||||
begin
|
|
||||||
column_empty[i] := true;
|
|
||||||
row_empty[i] := true;
|
|
||||||
end;
|
|
||||||
|
|
||||||
count := 0;
|
|
||||||
galaxy_count := 0;
|
|
||||||
|
|
||||||
{ read file }
|
|
||||||
while not EOF(file_) do
|
|
||||||
begin
|
|
||||||
|
|
||||||
count := count + 1;
|
|
||||||
|
|
||||||
readln(file_, str);
|
|
||||||
|
|
||||||
for i := 1 to length(str) do
|
|
||||||
begin
|
|
||||||
|
|
||||||
if str[i] = '#' then
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ record this galaxy }
|
|
||||||
galaxy_count := galaxy_count + 1;
|
|
||||||
|
|
||||||
galaxies[galaxy_count][0] := i;
|
|
||||||
galaxies[galaxy_count][1] := count;
|
|
||||||
|
|
||||||
{ mark rows/cols as nonempty }
|
|
||||||
row_empty[count] := false;
|
|
||||||
column_empty[i] := false;
|
|
||||||
|
|
||||||
end;
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
sum := 0;
|
|
||||||
{ for each pair }
|
|
||||||
for i := 1 to galaxy_count do
|
|
||||||
begin
|
|
||||||
for j := i + 1 to galaxy_count do
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ sum the pairwise distance }
|
|
||||||
sum := sum + galaxy_dist(galaxies[i], galaxies[j], row_empty, column_empty);
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
writeln('The sum is ', sum);
|
|
||||||
end.
|
|
||||||
|
|
@ -1,113 +0,0 @@
|
|||||||
program day11part1;
|
|
||||||
uses sysutils;
|
|
||||||
|
|
||||||
|
|
||||||
function dist_between(x1, x2: int32; empty_list: array of boolean): int64;
|
|
||||||
var
|
|
||||||
i: int32;
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ swap operands so x1 is always smaller}
|
|
||||||
if (x2 < x1) then
|
|
||||||
begin
|
|
||||||
i := x2;
|
|
||||||
x2 := x1;
|
|
||||||
x1 := i;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ calculate distance }
|
|
||||||
dist_between := 0;
|
|
||||||
for i := x1 to x2 - 1 do
|
|
||||||
begin
|
|
||||||
|
|
||||||
dist_between := dist_between + 1;
|
|
||||||
|
|
||||||
{ if gap, increment by an additional 999999 }
|
|
||||||
if empty_list[i] then
|
|
||||||
dist_between := dist_between + 999999;
|
|
||||||
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function galaxy_dist(g1, g2: array of int64; row_list, col_list: array of boolean): int64;
|
|
||||||
begin
|
|
||||||
galaxy_dist := dist_between(g1[0], g2[0], col_list) + dist_between(g1[1], g2[1], row_list);
|
|
||||||
end;
|
|
||||||
|
|
||||||
var
|
|
||||||
file_: text;
|
|
||||||
|
|
||||||
str: string;
|
|
||||||
|
|
||||||
galaxies: array[0..1000, 0..1] of int64;
|
|
||||||
galaxy_count: int32;
|
|
||||||
|
|
||||||
column_empty: array[1..1000] of boolean;
|
|
||||||
row_empty: array[1..1000] of boolean;
|
|
||||||
|
|
||||||
i, j: int32;
|
|
||||||
count: int32;
|
|
||||||
|
|
||||||
sum: int64;
|
|
||||||
|
|
||||||
begin
|
|
||||||
{ open inp.txt for reading }
|
|
||||||
assign(file_, 'resources/day11.txt');
|
|
||||||
reset(file_);
|
|
||||||
|
|
||||||
{ set row/cols to empty }
|
|
||||||
for i := 0 to 1000 do
|
|
||||||
begin
|
|
||||||
column_empty[i] := true;
|
|
||||||
row_empty[i] := true;
|
|
||||||
end;
|
|
||||||
|
|
||||||
count := 0;
|
|
||||||
galaxy_count := 0;
|
|
||||||
|
|
||||||
{ read file }
|
|
||||||
while not EOF(file_) do
|
|
||||||
begin
|
|
||||||
|
|
||||||
count := count + 1;
|
|
||||||
|
|
||||||
readln(file_, str);
|
|
||||||
|
|
||||||
for i := 1 to length(str) do
|
|
||||||
begin
|
|
||||||
|
|
||||||
if str[i] = '#' then
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ record this galaxy }
|
|
||||||
galaxy_count := galaxy_count + 1;
|
|
||||||
|
|
||||||
galaxies[galaxy_count][0] := i;
|
|
||||||
galaxies[galaxy_count][1] := count;
|
|
||||||
|
|
||||||
{ mark rows/cols as nonempty }
|
|
||||||
row_empty[count] := false;
|
|
||||||
column_empty[i] := false;
|
|
||||||
|
|
||||||
end;
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
sum := 0;
|
|
||||||
{ for each pair }
|
|
||||||
for i := 1 to galaxy_count do
|
|
||||||
begin
|
|
||||||
for j := i + 1 to galaxy_count do
|
|
||||||
begin
|
|
||||||
|
|
||||||
{ sum the pairwise distance }
|
|
||||||
sum := sum + galaxy_dist(galaxies[i], galaxies[j], row_empty, column_empty);
|
|
||||||
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
writeln('The sum is ', sum);
|
|
||||||
end.
|
|
||||||
|
|
@ -88,6 +88,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
operator shl (a: int512; b: int32) o: int512;
|
operator shl (a: int512; b: int32) o: int512;
|
||||||
var
|
var
|
||||||
i: int32;
|
i: int32;
|
||||||
|
@ -1,131 +0,0 @@
|
|||||||
|
|
||||||
{$include bigint.pas}
|
|
||||||
|
|
||||||
type
|
|
||||||
fraction = record
|
|
||||||
numerator: int512;
|
|
||||||
denominator: int512;
|
|
||||||
end;
|
|
||||||
|
|
||||||
const
|
|
||||||
{ i don't think there's a better way of doing this unfortunately }
|
|
||||||
SIMPLIFY_CUTOFF: int512 = (
|
|
||||||
values: ($00000000FFFFFFFF, $00000000FFFFFFFF, $00000000FFFFFFFF, $00000000FFFFFFFF, $00000000FFFFFFFF, $00000000FFFFFFFF, $00000000FFFFFFFF, $0000000000000000,
|
|
||||||
$0000000000000000, $0000000000000000, $0000000000000000, $0000000000000000, $0000000000000000, $0000000000000000, $0000000000000000, $0000000000000000);
|
|
||||||
);
|
|
||||||
|
|
||||||
procedure print_fraction(a: fraction);
|
|
||||||
begin
|
|
||||||
if a.denominator <> 1 then
|
|
||||||
begin
|
|
||||||
print512d(a.numerator);
|
|
||||||
write('/');
|
|
||||||
print512d(a.denominator);
|
|
||||||
end
|
|
||||||
else
|
|
||||||
print512d(a.numerator);
|
|
||||||
end;
|
|
||||||
|
|
||||||
function c_fraction(num: int512; den: int512): fraction;
|
|
||||||
begin
|
|
||||||
if den = 0 then
|
|
||||||
writeln('holy hell what are you doing');
|
|
||||||
|
|
||||||
c_fraction.numerator := num;
|
|
||||||
c_fraction.denominator := den;
|
|
||||||
|
|
||||||
end;
|
|
||||||
|
|
||||||
operator := (f: int64) out_ : fraction;
|
|
||||||
begin
|
|
||||||
out_ := c_fraction(f, 1);
|
|
||||||
end;
|
|
||||||
|
|
||||||
|
|
||||||
function gcd(a, b: int512): int512;
|
|
||||||
var
|
|
||||||
r: int512;
|
|
||||||
begin
|
|
||||||
|
|
||||||
while true do
|
|
||||||
begin
|
|
||||||
|
|
||||||
r := a mod b;
|
|
||||||
|
|
||||||
if r = 0 then
|
|
||||||
break;
|
|
||||||
|
|
||||||
a := b;
|
|
||||||
b := r;
|
|
||||||
|
|
||||||
end;
|
|
||||||
|
|
||||||
gcd := b;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function simplify(f: fraction): fraction;
|
|
||||||
var
|
|
||||||
b: int512;
|
|
||||||
neg: boolean;
|
|
||||||
|
|
||||||
begin
|
|
||||||
|
|
||||||
neg := false;
|
|
||||||
if isneg(f.numerator) then
|
|
||||||
begin
|
|
||||||
neg := not neg;
|
|
||||||
f.numerator := -f.numerator;
|
|
||||||
end;
|
|
||||||
|
|
||||||
if isneg(f.denominator) then
|
|
||||||
begin
|
|
||||||
neg := not neg;
|
|
||||||
f.denominator := -f.denominator;
|
|
||||||
end;
|
|
||||||
|
|
||||||
b := gcd(f.numerator, f.denominator);
|
|
||||||
|
|
||||||
if b <> 0 then
|
|
||||||
simplify := c_fraction(f.numerator div b, f.denominator div b)
|
|
||||||
else
|
|
||||||
simplify := f;
|
|
||||||
|
|
||||||
if neg then
|
|
||||||
simplify.numerator := -simplify.numerator;
|
|
||||||
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ add fractions }
|
|
||||||
operator + (a, b: fraction) out_: fraction;
|
|
||||||
//var
|
|
||||||
// g: int512;
|
|
||||||
begin
|
|
||||||
|
|
||||||
// print_fraction(a);
|
|
||||||
// write(' ');
|
|
||||||
// print_fraction(b);
|
|
||||||
// write(' ');
|
|
||||||
|
|
||||||
// g := gcd(a.denominator, b.denominator);
|
|
||||||
|
|
||||||
out_.denominator := a.denominator * b.denominator;
|
|
||||||
out_.numerator := a.denominator * b.numerator + b.denominator * a.numerator;
|
|
||||||
|
|
||||||
// print_fraction(out_);
|
|
||||||
// write(' ');
|
|
||||||
if abs(out_.numerator) > SIMPLIFY_CUTOFF then
|
|
||||||
out_ := simplify(out_);
|
|
||||||
// print_fraction(out_);
|
|
||||||
// writeln('ya');
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ mult fractions }
|
|
||||||
operator * (a, b: fraction) out_: fraction;
|
|
||||||
begin
|
|
||||||
out_.denominator := a.denominator * b.denominator;
|
|
||||||
out_.numerator := a.numerator * b.numerator;
|
|
||||||
|
|
||||||
if abs(out_.numerator) > SIMPLIFY_CUTOFF then
|
|
||||||
out_ := simplify(out_);
|
|
||||||
|
|
||||||
end;
|
|
@ -2,12 +2,16 @@
|
|||||||
|
|
||||||
uses sysutils, math;
|
uses sysutils, math;
|
||||||
|
|
||||||
{$include fraction.pas}
|
{$include bigint.pas}
|
||||||
|
|
||||||
const
|
const
|
||||||
MAX_POLYNOMIAL_LENGTH = 100;
|
MAX_POLYNOMIAL_LENGTH = 100;
|
||||||
|
EPSILON: double = 0;
|
||||||
type
|
type
|
||||||
|
fraction = record
|
||||||
|
numerator: int512;
|
||||||
|
denominator: int512;
|
||||||
|
end;
|
||||||
polynomial = record
|
polynomial = record
|
||||||
degree: int64;
|
degree: int64;
|
||||||
{ coefficients will be stored in reverse. ofc}
|
{ coefficients will be stored in reverse. ofc}
|
||||||
@ -15,6 +19,123 @@ type
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure print_fraction(a: fraction);
|
||||||
|
begin
|
||||||
|
if a.denominator <> 1 then
|
||||||
|
begin
|
||||||
|
print512d(a.numerator);
|
||||||
|
write('/');
|
||||||
|
print512d(a.denominator);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print512d(a.numerator);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function c_fraction(num: int512; den: int512): fraction;
|
||||||
|
begin
|
||||||
|
if den = 0 then
|
||||||
|
writeln('holy hell what are you doing');
|
||||||
|
|
||||||
|
c_fraction.numerator := num;
|
||||||
|
c_fraction.denominator := den;
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
operator := (f: int64) out_ : fraction;
|
||||||
|
begin
|
||||||
|
out_ := c_fraction(f, 1);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
function gcd(a, b: int512): int512;
|
||||||
|
var
|
||||||
|
r: int512;
|
||||||
|
begin
|
||||||
|
|
||||||
|
while true do
|
||||||
|
begin
|
||||||
|
|
||||||
|
r := a mod b;
|
||||||
|
|
||||||
|
if r = 0 then
|
||||||
|
break;
|
||||||
|
|
||||||
|
a := b;
|
||||||
|
b := r;
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
gcd := b;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function simplify(f: fraction): fraction;
|
||||||
|
var
|
||||||
|
b: int512;
|
||||||
|
neg: boolean;
|
||||||
|
|
||||||
|
begin
|
||||||
|
|
||||||
|
neg := false;
|
||||||
|
if isneg(f.numerator) then
|
||||||
|
begin
|
||||||
|
neg := not neg;
|
||||||
|
f.numerator := -f.numerator;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if isneg(f.denominator) then
|
||||||
|
begin
|
||||||
|
neg := not neg;
|
||||||
|
f.denominator := -f.denominator;
|
||||||
|
end;
|
||||||
|
|
||||||
|
b := gcd(f.numerator, f.denominator);
|
||||||
|
|
||||||
|
if b <> 0 then
|
||||||
|
simplify := c_fraction(f.numerator div b, f.denominator div b)
|
||||||
|
else
|
||||||
|
simplify := f;
|
||||||
|
|
||||||
|
if neg then
|
||||||
|
simplify.numerator := -simplify.numerator;
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ add fractions }
|
||||||
|
operator + (a, b: fraction) out_: fraction;
|
||||||
|
//var
|
||||||
|
// g: int512;
|
||||||
|
begin
|
||||||
|
|
||||||
|
// print_fraction(a);
|
||||||
|
// write(' ');
|
||||||
|
// print_fraction(b);
|
||||||
|
// write(' ');
|
||||||
|
|
||||||
|
// g := gcd(a.denominator, b.denominator);
|
||||||
|
|
||||||
|
out_.denominator := a.denominator * b.denominator;
|
||||||
|
out_.numerator := a.denominator * b.numerator + b.denominator * a.numerator;
|
||||||
|
|
||||||
|
// print_fraction(out_);
|
||||||
|
// write(' ');
|
||||||
|
if abs(out_.numerator) > $7FFFFFFFFFFFFFFF then
|
||||||
|
out_ := simplify(out_);
|
||||||
|
// print_fraction(out_);
|
||||||
|
// writeln('ya');
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ mult fractions }
|
||||||
|
operator * (a, b: fraction) out_: fraction;
|
||||||
|
begin
|
||||||
|
out_.denominator := a.denominator * b.denominator;
|
||||||
|
out_.numerator := a.numerator * b.numerator;
|
||||||
|
|
||||||
|
//if out_.numerator > (1 shl 60) then
|
||||||
|
// out_ := simplify(out_);
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure fill_array_1(var a: array of fraction; l: int64 = MAX_POLYNOMIAL_LENGTH);
|
procedure fill_array_1(var a: array of fraction; l: int64 = MAX_POLYNOMIAL_LENGTH);
|
||||||
var
|
var
|
||||||
i: int64;
|
i: int64;
|
||||||
|
Loading…
Reference in New Issue
Block a user