mirror of
https://github.com/plasmaofthedawn/2023adventofcode.git
synced 2024-10-18 10:26:24 -05:00
Compare commits
2 Commits
d17fcb67c5
...
dec7e6e117
Author | SHA1 | Date | |
---|---|---|---|
dec7e6e117 | |||
9366b8c03c |
10
Makefile
10
Makefile
@ -98,6 +98,16 @@ day10part2:
|
||||
@echo
|
||||
./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:
|
||||
rm build/*
|
140
resources/day11.txt
Normal file
140
resources/day11.txt
Normal file
@ -0,0 +1,140 @@
|
||||
.....#...............#......................................................................#.............................#..........#......
|
||||
........................................#............................#......#...............................................................
|
||||
..............................#..............#....................................#..............................................#..........
|
||||
..............#........................................................................#....................................................
|
||||
..#...........................................................................................#.............................................
|
||||
..................#.....................................#.....#......................................#.........#.......#....................
|
||||
...........#......................................#.....................#...........#................................................#......
|
||||
.............................#............................................................#.....................................#...........
|
||||
.....#..................#.................#.........................#......................................#................................
|
||||
....................................#.................#........................#..........................................................#.
|
||||
................#..............................#....................................................#.......................................
|
||||
..............................................................#.........................#.......................#.....................#.....
|
||||
............#...............#......................#.....................#..............................#...................................
|
||||
......................................#...............................................................................#.....................
|
||||
.........................................................#..................................................................................
|
||||
..............................................#.............................................................................................
|
||||
..#.................#.....................................................................#.....#..........#..............#......#..........
|
||||
..............................................................................#.............................................................
|
||||
..........................................#.........................#..............................................#........................
|
||||
.....#........#.............#...........................#...........................................#.......................................
|
||||
..................................#...............#.............#.................#..........................#.....................#........
|
||||
.......................................................................#....................................................................
|
||||
...............................................................................................................................#.........#..
|
||||
#.......................................#....................................................#.........................#....................
|
||||
........#...............#....................................#...........................................#..................................
|
||||
...............................#............................................................................................................
|
||||
.........................................................#..................................................................................
|
||||
..#..........................................#........................................#.............#............................#..........
|
||||
......................#............................#.................#........#.............................................................
|
||||
.........#.......#...........................................................................#.....................#.........#.........#....
|
||||
................................#............................................................................#..............................
|
||||
.........................................................................................#..................................................
|
||||
.#.........................................#.........#..........#.......................................................#...................
|
||||
....................#.................#...............................................................#.....................................
|
||||
............................................................................#...............................................................
|
||||
........................#.........................................................................................#.................#.......
|
||||
.............#.......................................................#......................................................#...............
|
||||
.......#.................................#.....#..............................................................#.............................
|
||||
#...............................#...............................................................#.....................#.....................
|
||||
.....................................#...................#.......................#.....#.......................................#............
|
||||
..........................#.................#......#...................................................................................#....
|
||||
..............#.............................................................................................................................
|
||||
...#..........................................................................#............................................#................
|
||||
.............................................................................................#.....................#......................#.
|
||||
.......................................#..........................................................................................#.........
|
||||
.......................................................................................................#....................................
|
||||
.....#.........#.................#.................#............#.........................#.................#...............................
|
||||
............................#.....................................................#.........................................................
|
||||
...........................................#................................................................................................
|
||||
....................................#.......................#.........................#............................#.....#..................
|
||||
............................................................................................................................................
|
||||
.....................#..................#................................#......#.....................................................#.....
|
||||
.......................................................#.........#...............................#.....#....................................
|
||||
............#.................................................................................................................#.............
|
||||
..............................................#..............#.......#.................................................#....................
|
||||
................................................................................................................#...........................
|
||||
.........#.............................................................................#............#.......................................
|
||||
.............................#...........................................#.....#............................................................
|
||||
.....#..........#.....#...........#.............#.....#.............................................................................#.......
|
||||
...........................................#............................................................#.....#.............................
|
||||
......................................#.........................#.........................#.....................................#...........
|
||||
............................................................................................................................................
|
||||
.......#.....#.....#........................................#.................................#.............................................
|
||||
.........................................#................................................................#.....#......#....................
|
||||
........................#.......#..................#.................................#.......................................#.......#......
|
||||
..............................................#.............................................................................................
|
||||
...............................................................................#............................................................
|
||||
......................................................#..........#..........................................................................
|
||||
...#......#.................#.............#.............................#..........................#...............................#........
|
||||
.............................................................#........................#..........................#.........#................
|
||||
......................................#....................................................#.............................................#..
|
||||
............................................................................................................................................
|
||||
...............#......#.......................#.....#.......................................................#...............................
|
||||
.#......#.......................#.........................#.......#....................................#....................................
|
||||
...............................................................................................#........................#......#.......#....
|
||||
............#...............#............#....................#........#..........#.........................................................
|
||||
...............................................................................................................#............................
|
||||
..........................................................................................................#.................#...............
|
||||
......................#........#.........................................................................................................#..
|
||||
....................................................................................#.....#.....#....................#......................
|
||||
.#.........#...............#........................................#...............................................................#.......
|
||||
..................#......................................................#..........................#.......................................
|
||||
.................................#.............#................................................................#.......#...................
|
||||
........................#......................................................#..............#................................#............
|
||||
.....#..........................................................#.....#.................#..................................................#
|
||||
.....................................#......#.........#...............................................#.....................................
|
||||
............................................................................................................................................
|
||||
............................................................................................................................................
|
||||
.................................#..........................................#................#...................#..............#...........
|
||||
.......#....................#............#.............................................#...................................#................
|
||||
..#................#........................................#.......#..............................#........................................
|
||||
........................#...........................................................................................................#.......
|
||||
...........#..........................................#.........................#......................#....................................
|
||||
...........................................#................................................................................................
|
||||
......#...............................................................................#.........#...........................................
|
||||
............................#.....................#................#........................................#...........#...................
|
||||
..................#.........................................#...............................................................................
|
||||
........................#...............#................................#.......#..............................#............#..............
|
||||
............#......................#.................#..................................................#..................................#
|
||||
..#................................................................................................#........................................
|
||||
.........................................................................................................................#..................
|
||||
..................................................................#................................................#................#.......
|
||||
.......#.....................................#..............................................................................................
|
||||
.................#...............#......#.....................#.............................................................................
|
||||
....................................................................................#..................................#....................
|
||||
..............................................................................................#............#................................
|
||||
....#......................#................................................................................................................
|
||||
......................................................#.................................#..........................#.............#..........
|
||||
..........#.................................................................................................................#...............
|
||||
.................................#..........#..................................#............................................................
|
||||
.......................................................................................................................................#....
|
||||
.......................................#..................................................................#......#..........................
|
||||
.............................#.........................................................#.......#............................................
|
||||
.........#....................................#............#.................................................................#..............
|
||||
...#...............................................................................#.................#......................................
|
||||
..................................................................................................................................#.........
|
||||
....................#.............#...................................................................................#....................#
|
||||
....................................................#...........#...........#...............................................................
|
||||
.........................................................................................................#..................................
|
||||
..........#...............................#............................#....................................................................
|
||||
.............................................................#....................#.............................#..............#............
|
||||
.#...............#..........................................................................#..........................................#....
|
||||
............................#...............................................................................................................
|
||||
............#......................................#............#..................................................................#........
|
||||
.......#...............................................................................................................#....................
|
||||
..................................#.......#..........................#........#...............#..........#....................#.............
|
||||
....................#....................................#............................#..........................#.......................#..
|
||||
..............................................................#.............................................................................
|
||||
..#............................#............................................................................#...............................
|
||||
..............#.....................#...............................................................................................#.......
|
||||
..........................#........................................#....................................#...........#.......................
|
||||
..........................................#.................................#.................#.............................................
|
||||
................................................#...........#..........#.....................................................#..............
|
||||
.........#..................................................................................................................................
|
||||
...................................................................................#............................#.......#...................
|
||||
....#.............................#.............................#.......................................................................#...
|
||||
.........................................#.........................................................#........................................
|
||||
....................#...........................................................................................................#...........
|
||||
..............#........................................#..................................#...................#......................#......
|
||||
.........#.................#........................................#...........#........................................#..................
|
10
resources/day11sample.txt
Normal file
10
resources/day11sample.txt
Normal file
@ -0,0 +1,10 @@
|
||||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
113
src/day11/part1.pas
Normal file
113
src/day11/part1.pas
Normal file
@ -0,0 +1,113 @@
|
||||
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.
|
||||
|
113
src/day11/part2.pas
Normal file
113
src/day11/part2.pas
Normal file
@ -0,0 +1,113 @@
|
||||
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,7 +88,6 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
operator shl (a: int512; b: int32) o: int512;
|
||||
var
|
||||
i: int32;
|
||||
|
131
src/day9/fraction.pas
Normal file
131
src/day9/fraction.pas
Normal file
@ -0,0 +1,131 @@
|
||||
|
||||
{$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,16 +2,12 @@
|
||||
|
||||
uses sysutils, math;
|
||||
|
||||
{$include bigint.pas}
|
||||
{$include fraction.pas}
|
||||
|
||||
const
|
||||
MAX_POLYNOMIAL_LENGTH = 100;
|
||||
EPSILON: double = 0;
|
||||
|
||||
type
|
||||
fraction = record
|
||||
numerator: int512;
|
||||
denominator: int512;
|
||||
end;
|
||||
polynomial = record
|
||||
degree: int64;
|
||||
{ coefficients will be stored in reverse. ofc}
|
||||
@ -19,123 +15,6 @@ type
|
||||
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);
|
||||
var
|
||||
i: int64;
|
||||
|
Loading…
Reference in New Issue
Block a user