added ai c code
This commit is contained in:
parent
bd0aee71db
commit
20defa2b62
@ -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
|
||||
|
64
4/day4.c
Normal file
64
4/day4.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user