finish 08, visualiser in part 2
This commit is contained in:
parent
0d7009b01e
commit
febcd39546
41
08/Makefile
Normal file
41
08/Makefile
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
CC := gcc
|
||||||
|
LD := $(CC)
|
||||||
|
LIBS := -lm
|
||||||
|
LDFLAGS :=
|
||||||
|
CFLAGS := -Wall -Wextra -Wno-override-init -std=c2x -include_.h
|
||||||
|
SRC := part1.c mylib.c
|
||||||
|
ifeq ($(PART),2)
|
||||||
|
SRC := part2.c mylib.c
|
||||||
|
endif
|
||||||
|
OBJ_DIR := build
|
||||||
|
OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRC)))
|
||||||
|
DIR := .
|
||||||
|
RAW_NAME:= a.out
|
||||||
|
NAME := $(RAW_NAME)
|
||||||
|
|
||||||
|
ifeq ($(RELEASE),1)
|
||||||
|
CFLAGS += -O3
|
||||||
|
LDFLAGS += -O3 -s
|
||||||
|
else
|
||||||
|
CFLAGS += -O0 -g3
|
||||||
|
LDFLAGS += -O0 -g
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
$(LD) $(LDFLAGS) -o $(DIR)/$(NAME) $(OBJ) $(LIBS)
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: %.c
|
||||||
|
@mkdir -p $(@D)
|
||||||
|
$(CC) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -rf $(OBJ_DIR)
|
||||||
|
@rm -rf $(RAW_NAME)
|
||||||
|
|
||||||
|
re:
|
||||||
|
@make --no-print clean
|
||||||
|
@make --no-print all
|
||||||
|
|
||||||
|
.PHONY: all clean re
|
27
08/_.h
Normal file
27
08/_.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <wctype.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||||
|
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
|
||||||
|
#define ABS(X) ((X) >= 0 ? (X) : -(X))
|
||||||
|
|
||||||
|
char** str_split(char* a_str, const char a_delim);
|
||||||
|
|
||||||
|
int str_index(char *str, char c);
|
||||||
|
|
||||||
|
int number_of_element(char *str, char c);
|
||||||
|
|
||||||
|
long gmc(long a, long b);
|
||||||
|
long lmc(long a, long b);
|
||||||
|
long lmcs(int array[], int n);
|
||||||
|
|
||||||
|
long strslen(char **strs);
|
50
08/main.txt
Normal file
50
08/main.txt
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
..........W......8..............................4.
|
||||||
|
............W...................F............1.L..
|
||||||
|
......o.................................A.........
|
||||||
|
................Z.........................A.......
|
||||||
|
................u.................................
|
||||||
|
.........8.......g...........................F....
|
||||||
|
.............2..8.......F......................1..
|
||||||
|
g....G............................................
|
||||||
|
.o..2.g...........Z..W.......................4b.1.
|
||||||
|
.................v...Z....c.....B...1.......f...b.
|
||||||
|
...uG.c............O.............Z................
|
||||||
|
..G..........c8....................5..4...........
|
||||||
|
...c.....G.g..........x..........B5............b..
|
||||||
|
.S.....o..v.......................................
|
||||||
|
............................BV....................
|
||||||
|
..........u............x.............B......0.....
|
||||||
|
...3....C..........I............V6..............f.
|
||||||
|
........S2......C.......................5.........
|
||||||
|
................a....v............................
|
||||||
|
..y...2..............i.......k.................4..
|
||||||
|
..........I........Yv......5...........f..........
|
||||||
|
.....3....o...................x...................
|
||||||
|
..........3.........S.........k...................
|
||||||
|
...y.......C.......d.I......X......fV.............
|
||||||
|
.....S............................d...............
|
||||||
|
.....O........I.......................iV.....A....
|
||||||
|
.y.................x.............k.ai0...F........
|
||||||
|
.......Y..............................a...........
|
||||||
|
........z.........................0...............
|
||||||
|
..O.3..............................0.......a......
|
||||||
|
.....z............................X...............
|
||||||
|
......z................................l..........
|
||||||
|
......L.......U........d.....X....................
|
||||||
|
......7z..........d...............................
|
||||||
|
O........7.....................K..................
|
||||||
|
.....................X...6........k...............
|
||||||
|
...L........Y...................s.................
|
||||||
|
.7...D................................l.9.........
|
||||||
|
..D...........................w..................i
|
||||||
|
......D.............6............s................
|
||||||
|
.................w..........6.9............s......
|
||||||
|
..............................s...................
|
||||||
|
........7.........................................
|
||||||
|
L...Y..........................U..................
|
||||||
|
.....................9..........l.........K.......
|
||||||
|
.......................9..............U...........
|
||||||
|
...D..............................................
|
||||||
|
.................................................K
|
||||||
|
..........y......................U.............l..
|
||||||
|
..................w...................K...........
|
91
08/mylib.c
Normal file
91
08/mylib.c
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
#include "_.h"
|
||||||
|
|
||||||
|
// NOT MY CODE
|
||||||
|
// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c
|
||||||
|
char** str_split(char* str, const char a_delim)
|
||||||
|
{
|
||||||
|
char *a_str = strdup(str);
|
||||||
|
char** result = 0;
|
||||||
|
size_t count = 0;
|
||||||
|
char* tmp = a_str;
|
||||||
|
char* last_comma = 0;
|
||||||
|
char delim[2];
|
||||||
|
delim[0] = a_delim;
|
||||||
|
delim[1] = 0;
|
||||||
|
|
||||||
|
while (*tmp)
|
||||||
|
{
|
||||||
|
if (a_delim == *tmp)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
last_comma = tmp;
|
||||||
|
}
|
||||||
|
tmp++;
|
||||||
|
}
|
||||||
|
|
||||||
|
count += last_comma < (a_str + strlen(a_str) - 1);
|
||||||
|
|
||||||
|
count++;
|
||||||
|
|
||||||
|
result = malloc(sizeof(char*) * count);
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
size_t idx = 0;
|
||||||
|
char* token = strtok(a_str, delim);
|
||||||
|
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
//assert(idx < count);
|
||||||
|
*(result + idx++) = strdup(token);
|
||||||
|
token = strtok(0, delim);
|
||||||
|
}
|
||||||
|
//assert(idx == count - 1);
|
||||||
|
*(result + idx) = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(a_str);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int str_index(char *str, char c) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (str[i])
|
||||||
|
if (str[i++] == c)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int number_of_element(char *str, char c) {
|
||||||
|
int nbr = 0;
|
||||||
|
for (int i = 0; str[i] != '\0'; i++)
|
||||||
|
if (str[i] == c)
|
||||||
|
nbr++;
|
||||||
|
return nbr;
|
||||||
|
}
|
||||||
|
|
||||||
|
long gmc(long a, long b) {
|
||||||
|
if (b == 0)
|
||||||
|
return a;
|
||||||
|
return gmc(b, a % b);
|
||||||
|
}
|
||||||
|
|
||||||
|
long lmc(long a, long b) {
|
||||||
|
return (a / gmc(a, b)) * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
long lmcs(int array[], int n) {
|
||||||
|
long result = array[0];
|
||||||
|
for (int i = 1; i < n; i++)
|
||||||
|
result = lmc(result, array[i]);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
long strslen(char **strs) {
|
||||||
|
long count = 0;
|
||||||
|
while (strs[count])
|
||||||
|
count++;
|
||||||
|
return count;
|
||||||
|
}
|
58
08/part1.c
Normal file
58
08/part1.c
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#include "_.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
char *input = argv[1];
|
||||||
|
char **inputs = str_split(strdup(input), '\n');
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
int max_y = strslen(inputs);
|
||||||
|
int max_x = strlen(inputs[0]);
|
||||||
|
int signals[4096][2] = { [0 ... 4095] = {-1, -1} };
|
||||||
|
for (int y = 0; inputs[y] != NULL; y++) {
|
||||||
|
for (int x = 0; inputs[y][x] != '\0'; x++) {
|
||||||
|
|
||||||
|
if (isalpha(inputs[y][x]) || isdigit(inputs[y][x])) {
|
||||||
|
for (int i = 0; inputs[i] != NULL; i++) {
|
||||||
|
for (int j = 0; inputs[i][j] != '\0'; j++) {
|
||||||
|
if (i == y && j == x)
|
||||||
|
continue;
|
||||||
|
if (inputs[i][j] != inputs[y][x])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int signal_y = i - y + i;
|
||||||
|
int signal_x = j - x + j;
|
||||||
|
|
||||||
|
if (signal_y >= max_y ||
|
||||||
|
signal_y < 0 ||
|
||||||
|
signal_x >= max_x ||
|
||||||
|
signal_x < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int continu = 0;
|
||||||
|
for (int v = 0; signals[v][0] != -1; v++) {
|
||||||
|
if (signals[v][0] == signal_y &&
|
||||||
|
signals[v][1] == signal_x) {
|
||||||
|
continu = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (continu == 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
signals[count][0] = signal_y;
|
||||||
|
signals[count][1] = signal_x;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("count: %d\n", count);
|
||||||
|
return 0;
|
||||||
|
}
|
77
08/part2.c
Normal file
77
08/part2.c
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
#include "_.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
char *input = argv[1];
|
||||||
|
char **inputs = str_split(strdup(input), '\n');
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
int max_y = strslen(inputs);
|
||||||
|
int max_x = strlen(inputs[0]);
|
||||||
|
char display[max_y][max_x];
|
||||||
|
for (int i = 0; i < max_y; i++)
|
||||||
|
for (int j = 0; j < max_x; j++)
|
||||||
|
display[i][j] = '.';
|
||||||
|
int signals[4096][2] = { [0 ... 4095] = {-1, -1} };
|
||||||
|
for (int y = 0; inputs[y] != NULL; y++) {
|
||||||
|
for (int x = 0; inputs[y][x] != '\0'; x++) {
|
||||||
|
|
||||||
|
if (!isalpha(inputs[y][x]) && !isdigit(inputs[y][x]))
|
||||||
|
continue;
|
||||||
|
for (int i = 0; inputs[i] != NULL; i++) {
|
||||||
|
for (int j = 0; inputs[i][j] != '\0'; j++) {
|
||||||
|
|
||||||
|
if (!isalpha(inputs[i][j]) && !isdigit(inputs[i][j]))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
|
||||||
|
for (int w = 0; w < 1000; w++) {
|
||||||
|
if (i == y && j == x)
|
||||||
|
break;
|
||||||
|
if (inputs[i][j] != inputs[y][x])
|
||||||
|
break;
|
||||||
|
|
||||||
|
int signal_y = ((i - y) * w + i);
|
||||||
|
int signal_x = ((j - x) * w + j);
|
||||||
|
|
||||||
|
if (signal_y >= max_y ||
|
||||||
|
signal_y < 0 ||
|
||||||
|
signal_x >= max_x ||
|
||||||
|
signal_x < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int continu = 0;
|
||||||
|
for (int v = 0; signals[v][0] != -1; v++) {
|
||||||
|
if (signals[v][0] == signal_y &&
|
||||||
|
signals[v][1] == signal_x) {
|
||||||
|
continu = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (continu == 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
display[signal_y][signal_x] = '#';
|
||||||
|
signals[count][0] = signal_y;
|
||||||
|
signals[count][1] = signal_x;
|
||||||
|
count+=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < max_y; i++) {
|
||||||
|
for (int j = 0; j < max_x; j++) {
|
||||||
|
printf("%c", display[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf("count: %d\n", count);
|
||||||
|
return 0;
|
||||||
|
}
|
12
08/test.txt
Normal file
12
08/test.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............
|
Loading…
Reference in a new issue