Skip to content

Instantly share code, notes, and snippets.

@kalineh
Created December 12, 2014 22:29
Show Gist options
  • Save kalineh/93962ac2d6e217053be6 to your computer and use it in GitHub Desktop.
Save kalineh/93962ac2d6e217053be6 to your computer and use it in GitHub Desktop.
Quora Hackathon - Archer - C++
// https://www.hackerrank.com/contests/quora-haqathon/challenges/archery
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
struct Input
{
int N;
int M;
int* R;
int* L;
};
bool is_newline(char* cursor)
{
if (*cursor == '\n')
return true;
if (*cursor == '\r')
return true;
return false;
}
bool is_whitespace(char* cursor)
{
if (*cursor == ' ')
return true;
if (*cursor == '\t')
return true;
return false;
}
char* consume_newline(char* cursor)
{
while (*cursor != '\0' && is_newline(cursor))
cursor++;
return cursor;
}
char* consume_until_newline(char* cursor)
{
while (*cursor != '\0' && !is_newline(cursor))
cursor++;
return cursor;
}
char* consume_whitespace(char* cursor)
{
while (*cursor != '\0' && is_whitespace(cursor))
cursor++;
return cursor;
}
char* consume_until_whitespace(char* cursor)
{
while (*cursor != '\0' && !is_whitespace(cursor))
cursor++;
return cursor;
}
char* parse_int_line(char* cursor, int* out)
{
*out = atol(cursor);
cursor = consume_until_newline(cursor);
cursor = consume_newline(cursor);
return cursor;
}
char* parse_ints_line(char* cursor, int** out, int count)
{
*out = (int*)malloc(sizeof(int) * count);
for (int i = 0; i < count; ++i)
{
(*out)[i] = atol(cursor);
if (i == count - 1)
{
cursor = consume_until_newline(cursor);
cursor = consume_newline(cursor);
}
else
{
cursor = consume_until_whitespace(cursor);
cursor = consume_whitespace(cursor);
}
}
return cursor;
}
char* parse_ints_lines(char* cursor, int** out, int lines, int elements)
{
*out = (int*)malloc(sizeof(int) * lines * elements);
for (int i = 0; i < lines; ++i)
{
for (int j = 0; j < elements; ++j)
{
(*out)[i * elements + j] = atol(cursor);
if (j == elements - 1)
{
cursor = consume_until_newline(cursor);
cursor = consume_newline(cursor);
}
else
{
cursor = consume_until_whitespace(cursor);
cursor = consume_whitespace(cursor);
}
}
}
cursor = consume_until_newline(cursor);
cursor = consume_newline(cursor);
return cursor;
}
int count_q(Input* input)
{
int count = 0;
for (int i = 0; i < input->M; ++i)
{
for (int j = 0; j < input->N; ++j)
{
int* lines = input->L;
int* radii = input->R;
int x1 = lines[i * 4 + 0];
int y1 = lines[i * 4 + 1];
int x2 = lines[i * 4 + 2];
int y2 = lines[i * 4 + 3];
float radius = (float)radii[j];
float l1 = sqrtf((float)(x1 * x1 + y1 * y1));
float l2 = sqrtf((float)(x2 * x2 + y2 * y2));
float a = min(l1, l2);
float b = max(l1, l2);
if (a < radius && b > radius)
++count;
}
}
return count;
}
// sample input
// 4
// 1 2 3 4
// 3
// 1 -1 4 -3
// 2 1 1 2
// 1 -2 3 -4
Input make_input(char* buffer)
{
char* cursor = buffer;
Input input;
cursor = parse_int_line(cursor, &input.N);
cursor = parse_ints_line(cursor, &input.R, input.N);
cursor = parse_int_line(cursor, &input.M);
cursor = parse_ints_lines(cursor, &input.L, input.M, 4);
return input;
}
char* read_all_stdin()
{
char* result = (char*)malloc(256);
char* cursor = result;
int index = 0;
while (!feof(stdin))
{
result[index++] = (char)fgetc(stdin);
if ((index % 256) == 0)
result = (char*)realloc(result, index + 256);
}
if ((index % 256) == 0)
result = (char*)realloc(result, index + 256);
result[index] = '\0';
return result;
}
char* read_sample_data()
{
const char* sample = "4\n\r 1 2 3 4\n\r3\n\r1 -1 4 -3\n\r2 1 1 2\n\r1 -2 3 -4\n\r";
char* buffer = (char*)malloc(strlen(sample) + 1);
memcpy(buffer, sample, strlen(sample) + 1);
return buffer;
}
int main()
{
char* buffer = read_all_stdin();
//char* buffer = read_sample_data();
Input input = make_input(buffer);
int count = count_q(&input);
printf("%d", count);
free(buffer);
free(input.R);
free(input.L);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment