From 20defa2b62df7b3adf07df6f49f1644e0fc57e52 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Thu, 8 Dec 2022 01:46:35 +0100 Subject: [PATCH] added ai c code --- 4/README.md | 4 +- 4/{day4-by-ai.c => day4-by-ai.s} | 0 4/day4.c | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) rename 4/{day4-by-ai.c => day4-by-ai.s} (100%) create mode 100644 4/day4.c diff --git a/4/README.md b/4/README.md index ac4e27f..3939bb2 100644 --- a/4/README.md +++ b/4/README.md @@ -5,7 +5,7 @@ Today's language: **gnu x86-64** (and alternatively **C**) Today I wanted to use OpenAI to solve the tasks using *gnu* **x86-64 assembly**, which I have never used before (I did some small things in [6502-asssembly](https://github.com/MatthiasQuintern/6502), so it wasn't all new). -The prompt can be seen in prompt.txt. +The prompt can be seen in `prompt.md`, the original file in `day4-by-ai.s`. It did a lot of things right, however it also did a lot of things wrong: - It did generate x86 (32-bit) code because I did not specify x86-64. So I changed the `int $0x80` system calls to use `syscall` (which also meant I had to change all the registers and [syscall numbers](https://filippo.io/linux-syscall-table/)) - It did get the argument order for `sscanf` wrong, even when I specifically asked, it told me that the buffer to read comes last @@ -18,7 +18,7 @@ It was still a good starting point and provided lots of help. At the end of the ~~evening~~ next morning I had successfully coded the task in x86-64 assembly! :smiley: I also gave it the exact same prompt again but said I wanted it in **C**. -It instantly produced compile-able c code that gave the right answer... +It instantly produced compile-able c `day4-by-ai.c` code that gave the right answer... ```shell # for the assembly program make asm diff --git a/4/day4-by-ai.c b/4/day4-by-ai.s similarity index 100% rename from 4/day4-by-ai.c rename to 4/day4-by-ai.s diff --git a/4/day4.c b/4/day4.c new file mode 100644 index 0000000..650f7b0 --- /dev/null +++ b/4/day4.c @@ -0,0 +1,64 @@ +#include +#include +#include + +// Structure to represent a range in the file +struct range { + int lower; + int upper; +}; + +// Function to parse a line from the file and return the two ranges in the line +void parse_line(const char *line, struct range *range1, struct range *range2) { + // Parse the two ranges in the line + sscanf(line, "%d-%d,%d-%d", &range1->lower, &range1->upper, &range2->lower, &range2->upper); +} + +int main(int argc, char *argv[]) { + // Check if a filename was provided + if (argc < 2) { + fprintf(stderr, "Error: No filename provided\n"); + return 1; + } + + // Open the file for reading + FILE *fp = fopen(argv[1], "r"); + if (fp == NULL) { + fprintf(stderr, "Error: Unable to open file for reading\n"); + return 1; + } + + // Read the file line by line + char line[256]; + int contain_count = 0; + int overlap_count = 0; + while (fgets(line, sizeof(line), fp) != NULL) { + // Parse the two ranges in the line + struct range range1, range2; + parse_line(line, &range1, &range2); + + // Check if one range contains the other + if (range1.lower <= range2.lower && range1.upper >= range2.upper) { + contain_count++; + } + else if (range2.lower <= range1.lower && range2.upper >= range1.upper) { + contain_count++; + } + + // Check if one range overlaps with the other + else if (range1.lower == range2.lower || + range1.upper == range2.upper || + (range1.lower > range2.lower && range1.lower <= range2.upper) || + (range1.upper < range2.upper && range1.upper >= range2.lower)) { // had to fix range1.upper < range2.upper comparison + overlap_count++; + } + } + + // Close the file + fclose(fp); + + // Print the results + printf("The file contains %d lines where one of the two ranges contains each other\n", contain_count); + printf("The file contains %d lines where the two ranges overlap each other\n", overlap_count); + return 0; +}