first, with working snowflakes.c but it doesn't pass the time limit, it's brute force.
This commit is contained in:
1
README.md
Normal file
1
README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
This repo is to document my progress working through the book "Algorithmic Thinking".
|
||||||
30
food_line.c
Normal file
30
food_line.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_LINES 100
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i, j, num_lines, num_new_people;
|
||||||
|
int lines[MAX_LINES];
|
||||||
|
scanf("%d%d", &num_lines, &num_new_people);
|
||||||
|
for (i = 0; i < num_lines; i++)
|
||||||
|
{
|
||||||
|
scanf("%d", &lines[i]);
|
||||||
|
}
|
||||||
|
int smallest;
|
||||||
|
int smallest_index = -1;
|
||||||
|
for (i = 0; i < num_new_people; i++)
|
||||||
|
{
|
||||||
|
smallest = 101;
|
||||||
|
for (j = 0; j < num_lines; j++)
|
||||||
|
{
|
||||||
|
if (lines[j] < smallest) {
|
||||||
|
smallest = lines[j];
|
||||||
|
smallest_index = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d\n", smallest);
|
||||||
|
lines[smallest_index] += 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
6
snowflakes/Makefile
Normal file
6
snowflakes/Makefile
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
build:
|
||||||
|
clang snowflakes.c -o snowflakes
|
||||||
|
run:
|
||||||
|
./snowflakes < snowflakes.txt
|
||||||
|
go:
|
||||||
|
clang snowflakes.c -o snowflakes && ./snowflakes < snowflakes.txt
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct Node * next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Node nodes[5];
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
nodes[i].value = i + 42;
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
nodes[i - 1].next = &nodes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
printf("node at %d has value %d\n", i, nodes[i].value);
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
printf("node at %d has value %d\n", i - 1, nodes[i - 1].value);
|
||||||
|
printf(
|
||||||
|
"that node's 'next' is %p and, as expected, its 'next' has value %d\n",
|
||||||
|
nodes[i - 1].next, nodes[i - 1].next->value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct Node * next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Node nodes[5];
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
Node curr_node = { i };
|
||||||
|
nodes[i] = curr_node;
|
||||||
|
printf("Node in ARR current after insertion: %d\n", nodes[i].value);
|
||||||
|
printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
printf("Node in ARR: %d\n", nodes[i].value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct Node * next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Node *nodes[5];
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
Node * curr_node = malloc(sizeof(Node));
|
||||||
|
curr_node->value = i;
|
||||||
|
nodes[i] = curr_node;
|
||||||
|
printf("Node in ARR current after insertion: %d\n", nodes[i]->value);
|
||||||
|
printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
printf("Node in ARR: %d\n", nodes[i]->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define ARRAY_LEN 1047505
|
||||||
|
|
||||||
|
typedef struct Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct Node * next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
Node * make_node(int value)
|
||||||
|
{
|
||||||
|
Node * node = malloc(sizeof(Node));
|
||||||
|
node->value = value;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Node * nodes[ARRAY_LEN];
|
||||||
|
for (int i = 0; i < ARRAY_LEN; i++)
|
||||||
|
{
|
||||||
|
nodes[i] = make_node(i);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < ARRAY_LEN; i++)
|
||||||
|
{
|
||||||
|
printf("Node in ARR: %d\n", nodes[i]->value);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
snowflakes/snowflakes
Executable file
BIN
snowflakes/snowflakes
Executable file
Binary file not shown.
87
snowflakes/snowflakes.c
Normal file
87
snowflakes/snowflakes.c
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n"
|
||||||
|
#define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n"
|
||||||
|
#define LEN_SNOWFLAKE 6
|
||||||
|
|
||||||
|
int get_num_snowflakes_from_stdin(void)
|
||||||
|
{
|
||||||
|
int num_snowflakes;
|
||||||
|
scanf("%d", &num_snowflakes);
|
||||||
|
return num_snowflakes;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool they_are_equal_right(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset)
|
||||||
|
{
|
||||||
|
int sf1_tip, sf2_tip, sf2_index;
|
||||||
|
for (int i = 0; i < LEN_SNOWFLAKE; i++)
|
||||||
|
{
|
||||||
|
sf1_tip = sf1[i];
|
||||||
|
|
||||||
|
sf2_index = (i + sf2_offset) % LEN_SNOWFLAKE;
|
||||||
|
sf2_tip = sf2[sf2_index];
|
||||||
|
|
||||||
|
if (sf1_tip != sf2_tip)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool they_are_equal_left(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset)
|
||||||
|
{
|
||||||
|
int sf1_tip, sf2_tip, sf2_index;
|
||||||
|
for (int i = 0; i < LEN_SNOWFLAKE; i++)
|
||||||
|
{
|
||||||
|
sf1_tip = sf1[i];
|
||||||
|
|
||||||
|
sf2_index = sf2_offset - i;
|
||||||
|
if (sf2_index < 0)
|
||||||
|
sf2_index = sf2_index + LEN_SNOWFLAKE;
|
||||||
|
sf2_tip = sf2[sf2_index];
|
||||||
|
|
||||||
|
if (sf1_tip != sf2_tip)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE])
|
||||||
|
{
|
||||||
|
for (int o = 0; o < LEN_SNOWFLAKE; o++)
|
||||||
|
{
|
||||||
|
if (they_are_equal_right(sf1, sf2, o))
|
||||||
|
return true;
|
||||||
|
if (they_are_equal_left(sf1, sf2, o))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void populate_snowflakes_from_stdin(int snowflakes[][LEN_SNOWFLAKE], int num_snowflakes)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < num_snowflakes; i++)
|
||||||
|
for (int j = 0; j < LEN_SNOWFLAKE; j++)
|
||||||
|
scanf("%d", &snowflakes[i][j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// TODO: refactor to only compare similar snowflakes:
|
||||||
|
// - the tips add up to the same amount
|
||||||
|
// - try to skip any array (hash map) slots with one or fewer elements stored
|
||||||
|
int num_snowflakes = get_num_snowflakes_from_stdin();
|
||||||
|
int snowflakes[num_snowflakes][LEN_SNOWFLAKE];
|
||||||
|
populate_snowflakes_from_stdin(snowflakes, num_snowflakes);
|
||||||
|
|
||||||
|
for (int i = 0; i < num_snowflakes; i++)
|
||||||
|
for (int j = i + 1; j < num_snowflakes; j++)
|
||||||
|
if (they_are_equal(snowflakes[i], snowflakes[j]))
|
||||||
|
{
|
||||||
|
printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(STRING_SNOWFLAKES_ARE_UNIQUE);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
snowflakes/snowflakes.md
Normal file
25
snowflakes/snowflakes.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
Two snowflakes are identical if they are the same,
|
||||||
|
if we can make them the same by moving rightward
|
||||||
|
through one of the snowflakes (moving clockwise),
|
||||||
|
or if we can make them the same by moving leftward
|
||||||
|
through one of the snowflakes (moving counterclockwise).
|
||||||
|
|
||||||
|
# Input
|
||||||
|
|
||||||
|
The first line of input is an integer n,
|
||||||
|
the number of snowflakes that we’ll be processing.
|
||||||
|
The value n will be between 1 and 100,000.
|
||||||
|
|
||||||
|
Each of the following n lines represents one snowflake:
|
||||||
|
each line has six integers, where each integer is
|
||||||
|
at least 0 and at most 10,000,000.
|
||||||
|
|
||||||
|
# Output
|
||||||
|
Our output will be a single line of text:
|
||||||
|
|
||||||
|
If there are no identical snowflakes, output exactly
|
||||||
|
"No two snowflakes are alike."
|
||||||
|
If there are at least two identical snowflakes, output exactly
|
||||||
|
"Twin snowflakes found."
|
||||||
|
|
||||||
|
The time limit for solving the test cases is one second.
|
||||||
6
snowflakes/snowflakes.txt
Normal file
6
snowflakes/snowflakes.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
4
|
||||||
|
6 5 4 3 2 1
|
||||||
|
15 12 12 12 12 12
|
||||||
|
4 5 6 1 2 3
|
||||||
|
1 1 1 1 1 1
|
||||||
|
|
||||||
Reference in New Issue
Block a user