finish 06, time to get a real Makefile

This commit is contained in:
Xamora 2024-12-06 07:52:02 +01:00
parent 1afd931b84
commit 7abeab6153
11 changed files with 467 additions and 0 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
a.out
build/
*.o

27
06/_.h Normal file
View 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);

130
06/main.txt Normal file
View file

@ -0,0 +1,130 @@
......#........#..........#.................##......................#.............#..................#............#............#..
.....#............................................#.......................#.......##.................#..#...................#.#...
......#...................#............................#.....#.........#................................................#.........
...........................................#.......#..........#..#......#.........#.#.#.#................#.##........##...........
........#.........................................#.......#.....#..........#...#....##....#..#.......##................#......#...
...................#..#.....................................................#.................................................#...
....#..................#..........#........#.............#................................................................#.......
......#..............#..................#...........#......#.....#...........................#..#.#.....................#.....#...
......#...#...............................................#.....#.........................#..........#............................
......#......#..........................................#..................................#....#..#................#..##...#.....
........................................................................................#.........................................
................#..........#..........................#.##......................#..........#......................................
.............#.............................#......#..............................#....................#...........#.....#...#.....
.......#.................#..................#..........................#...........................................#..............
.......#.....................................................#..........................................#....#.#.....#......#.....
...#......#............#........#......................#...........................#.#....#..#....................#.............##
...........................##..#..#..........#....................#................................................#......#.......
.....................#...#.......................#...#..#.....#................#...........................................#....#.
.................#................#.............#...#...........................................................#..#.#............
#.....................#....................##.......#...................#..........#..........................#...................
.#..........##......#...#......#................#.....................................#.#.....#..............#....................
...#...#..#..........................................................#..#..#......................................................
..................#.#..............#...#...............................#...#....#.#.........#.........................##..........
.......#.........................#..............................................#........#....#....#.........#....................
...........#......#......#...........#........#.............................##.................................................#..
............................#...................................#......#........................................#.................
......................................##...........................................#.............#..#...........#...#.....#.......
...................................................#.................................#............................................
..........................#..........................................#..#.....#.................#.#......#........................
..................#...........#.............................#....#.#..................#................................#..........
...................#....#..#.........................................................#....#.......................................
..#..........................................................##..................................#......................#....#.#..
#.........................#.....#...............................#........................#................#.......................
......................#......#...#..........#......#..............................#...#......#.................................#..
...............#.....#............................................................................#...............................
#.....................#........#.#........................................................#.......................#.............#.
.......#...#..........#........#...........#.................#..................#....................................##.....#.....
.#......#..................................#.......................................................................#..............
.................#.....#...........................................................#..............................................
.........#....................................^......#..................#..............................#..#.............#...#.....
.............................................................#.....#....................................#.........................
....................#.................#........#........................................................#..#............#........#
.....#..........................................................#...........................#.................#....##.............
..............#.........#.................#............................#.............#......................#.#............#......
.............#.................#.........................................#...#................#.............#.....#...............
....#.....##..#......#........#............................................#..........#.......#.................................##
..........................................................................#.......................................................
.......#..........#.............................................................................................##..#.............
............................#...............#...............................................................................#.....
....#......#...............................................................................................................#......
..................................#......................#.....#............................................#.....#........#......
...............................#..........................................#.......................................................
.....#.............#...#..............#.#............#....................................................................#.......
..............................................#...............................#......##.......#................#..........#.#.....
........#.....##...............................................#.........................#........................................
.....#.........................#.#........#.............................................#.............#..............##...........
........................................#..##.........................#............#...........#.............#....................
.....................#.#................#............................#.................#.....#..............................#.....
................#.....................................#..#....................#..................................#................
...........#...................#.................#..........................................................#.......#...........#.
...........................#..................................#......#..............#............#.........#....#................#
..................................................................................................................#...............
..............#....#.#............................................................................#.....#.........#........#......
..#...........#.................#.#......#................................#..........................#...........................#
............#...........##...............................#..................................................#.....................
..............##.#..................................................................................#.........#......#......#.....
................................................................#...............#....#....##................#.....................
.........#..#.....................#........................................................#......#................#..............
...#..........#..........#..................#.......................#........................#...#................#.........#.....
...................................#..#...............................#..........#................................................
.........................#........................................................................................................
......................#.........#....................................................##............................#.....#........
.#....#.....................#.........................................................#....#........#.............................
..........................................#....................................#........................................#.........
.................................................#.......................................#........................................
#..#...............#......................#....#.............................................#................#........##.........
.........#....#........##.........#.....#.........#...............................................#..................#....#...#...
..#............................................................................#...............................................#..
.......................................................#.........#......#..........................#.#............................
.........#.............#.................##.....#.....#.........#............#.........#..................#.....#.............#...
#..........#.........#...............................................................................................#............
.......................#....................#..................................................#..................................
.#................#.................................#........................................#...........#........................
.........#........................................................................................................................
..................................................................................#..........#.....................#..............
..#...............................#.......#.....................#......................#.....#..........................#.........
......#....#..........................................................#..........................#................................
...........................................##..................................................##..................#...#..........
...............#....#...........................#........................................#..............#....................##...
#......#......................................................#..........#.........................#.........#....................
...#...................................#......#..........................................#.........................#.............#
#.....#.........#.............#.......................................#...............................#...........................
...................#.#..........................................................................#...............#.................
.......#......##..........#....................................................#..................#..........#....................
.............................................#.##..............#......................#......#..#..................#..........#...
..##...................#...................................#.........................................#...........#................
...................................................#.....................................#.......................#...............#
..........#.......##...................#...............#...............................................................#..........
.......................#............#............##.............................................#......##.................#...#...
.........#.................................................................................#............#....................#....
.......##.........#..................................................................#................#...........................
................#.............................................................................................#...................
......................#.......#....#.................................................#......#.#...............................#...
.........#.........#.#.....##.#..................................##...................................................#...........
.#......................................................#.......#......##........#..........#.........#....#......................
........#.#......................#................................................................................................
..................#.............................#.................#..................#......................#......##.#...........
................##.............................................#....#..........##.#.#....#........................................
..............#...................#.......#............................#.#.....................................................#..
............#..............................................#....#.....................#...#.......................#.....#......#..
..............#........................................................#............................................#.............
.......#.....#.........#..............#..............................#.......................#..#................#....#..#........
.......................#......#........................................#.................#..........#.............................
...........#..................#.............................#............................................................#........
......#....#......#.....................#.....................#........#.....................#...#.....#.#...................#....
#..............................................##...#.....#...........#..........................................#................
..........#..........#.#........................................#..#....#.........................................................
..................#......................................#...#.......#............................................................
...#.............................#..................................................#...........#.................................
................................##.#.............#........................#...#...............................................#...
...#...................................#.......................#........................#............#...............#....#.#.....
..............................................................##.......#...............#.........#..................#.............
....#............................#..................................#..#..#...............#..............##.......................
#...#.................##...............................#...........#...........#..............#.#.........#........#...#..........
..........................#.....#....................#..............#.............#..........#....................#.........#.....
......#.......................................................#........#.....#..........................#...#.........#...........
...............................#............#.....#...........................#...............#........#..........................
............#..#......#...#..#.....................#...............#.........#...........................................#.....#..
.......#..#..................#............#...........#..............................................................#............
....#......#.##..#......##..........#.......#............#...............#....#....................................#..........#.#.

91
06/mylib.c Normal file
View 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;
}

72
06/part1.c Normal file
View file

@ -0,0 +1,72 @@
#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 x = 0, y = 0;
for (int i = 0; inputs[i] != NULL; i++)
for (int j = 0; inputs[i][j] != '\0'; j++)
if (inputs[i][j] == '^') {
y = i;
x = j;
}
int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left
while (1) {
if (direction == 0) {
if (inputs[y - 1] == NULL)
break;
if (inputs[y - 1][x] == '#')
direction++;
else {
inputs[y][x] = 'X';
y--;
}
}
else if (direction == 1) {
if (x + 1 >= strlen(inputs[y]))
break;
if (inputs[y][x + 1] == '#')
direction++;
else {
inputs[y][x] = 'X';
x++;
}
}
else if (direction == 2) {
if (inputs[y + 1] == NULL)
break;
if (inputs[y + 1][x] == '#')
direction++;
else {
inputs[y][x] = 'X';
y++;
}
}
else if (direction == 3) {
if (x - 1 < 0)
break;
if (inputs[y][x - 1] == '#')
direction = 0;
else {
inputs[y][x] = 'X';
x--;
}
}
}
int count = 1;
for (int i = 0; inputs[i] != NULL; i++)
for (int j = 0; inputs[i][j] != '\0'; j++)
if (inputs[i][j] == 'X')
count++;
printf("count: %d\n", count);
return 0;
}

0
template/part1.sh → 06/part1.sh Normal file → Executable file
View file

78
06/part2.c Normal file
View file

@ -0,0 +1,78 @@
#include "_.h"
int its_infinite(char **inputs, int x, int y) {
int i = 0;
int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left
for (; i < 100000; i++) {
if (direction == 0) {
if (y - 1 < 0)
return 0;
if (inputs[y - 1][x] == '#')
direction++;
else
y--;
}
else if (direction == 1) {
if (x + 1 >= strlen(inputs[y]))
return 0;
if (inputs[y][x + 1] == '#')
direction++;
else
x++;
}
else if (direction == 2) {
if (inputs[y + 1] == NULL)
return 0;
if (inputs[y + 1][x] == '#')
direction++;
else
y++;
}
else if (direction == 3) {
if (x - 1 < 0)
return 0;
if (inputs[y][x - 1] == '#')
direction = 0;
else
x--;
}
}
return 1;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int x = 0, y = 0;
for (int i = 0; inputs[i] != NULL; i++)
for (int j = 0; inputs[i][j] != '\0'; j++)
if (inputs[i][j] == '^') {
y = i;
x = j;
}
int count = 0;
for (int i = 0; inputs[i] != NULL; i++) {
for (int j = 0; inputs[i][j] != '\0'; j++) {
int replace = 0;
if (inputs[i][j] == '#')
replace = 1;
inputs[i][j] = '#';
count += its_infinite(inputs, x, y);
inputs[i][j] = '.';
if (replace)
inputs[i][j] = '#';
}
}
printf("count: %d\n", count);
return 0;
}

0
template/part2.sh → 06/part2.sh Normal file → Executable file
View file

10
06/test.txt Normal file
View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

41
template/Makefile Normal file
View 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

View file

@ -0,0 +1,16 @@
#include "_.h"
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
for (int i = 0; inputs[i] != NULL; i++) {
}
return 0;
}