Skip to content

Instantly share code, notes, and snippets.

@ohmypxl
Last active December 6, 2022 11:03
Show Gist options
  • Save ohmypxl/e0d38a2643f259ae4eb8df1f47a26846 to your computer and use it in GitHub Desktop.
Save ohmypxl/e0d38a2643f259ae4eb8df1f47a26846 to your computer and use it in GitHub Desktop.
Pawn AOC day 1
#include <console>
#include <file>
#include <string>
stock sanitize(buffer[])
{
new pos = strfind(buffer, "\r\n");
buffer[pos] = EOS;
return 1;
}
// https://github.com/nvkgbr/pawn_sort/blob/master/pawn_sort.inc#L70-L86
stock selectionSort(array[],n)
{
for(new x=0; x<n; x++)
{
new index_of_min = x;
for(new y = x; y < n; y++)
{
if(array[index_of_min] > array[y])
{
index_of_min = y;
}
}
new temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
}
main()
{
new inputFile[32];
format(inputFile, sizeof(inputFile), "input.txt");
solve_part_one(inputFile);
solve_part_two(inputFile);
return 1;
}
stock solve_part_one(const inputFile[])
{
if (!fexist(inputFile))
{
print("Please add your input file in scriptfiles folder");
return 0;
}
new File:f = fopen(inputFile);
new total = -99999;
if (f)
{
new bufferLine[128], sum = 0, count = 0;
while (fread(f, bufferLine, sizeof(bufferLine)))
{
sanitize(bufferLine);
if (bufferLine[0] == EOS)
{
if (sum > total)
{
total = sum;
}
sum = 0;
continue;
}
sum = sum + strval(bufferLine);
count ++;
}
}
printf("Total: %d", total);
return 1;
}
stock solve_part_two(const inputFile[])
{
if (!fexist(inputFile))
{
print("Please add your input file in scriptfiles folder");
return 0;
}
new File:f = fopen(inputFile);
// 251 is the hardcoded length
new top[251], idx = 0;
if (f)
{
new bufferLine[128], sum = 0;
while (fread(f, bufferLine, sizeof(bufferLine)))
{
sanitize(bufferLine);
if (bufferLine[0] == EOS)
{
top[idx++] = sum;
sum = 0;
continue;
}
sum = sum + strval(bufferLine);
}
}
if (idx)
{
selectionSort(top, idx+1);
new sum = top[idx] + top[idx-1] + top[idx-2];
printf("\nTop 1: %d\nTop 2: %d\nTop 3: %d", top[idx], top[idx-1], top[idx-2]);
printf("Results: %d", sum);
}
return 1;
}
@markkkkas
Copy link

chad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment