renamed files, fixes to readme
This commit is contained in:
parent
8ca7e9a6f0
commit
dc2a1ef806
27
4/README.md
27
4/README.md
@ -1,28 +1,29 @@
|
|||||||
# [Day 4](https://adventofcode.com/2022/day/4)
|
# [Day 4](https://adventofcode.com/2022/day/4)
|
||||||
:gift::gift::gift::gift:
|
:gift::gift::gift::gift:
|
||||||
Today's language: **gnu x86-64** (and alternatively **c**)
|
|
||||||
|
|
||||||
Today I wanted to use OpenAI to solve the tasks using gnu x86-64, which I have never used before
|
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).
|
(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.txt.
|
||||||
It did a lot of things right, however:
|
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 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 line to read comes last
|
- It did get the argument order for `sscanf` wrong, even when I specifically asked, it told me that the buffer to read comes last
|
||||||
- It did not correctly implement the logic to test the condition
|
- It did not correctly implement the logic to test for containment of the two ranges
|
||||||
- It sometimes me wrong syscall numbers (they were just wrong, not even x86)
|
- It sometimes gave me wrong syscall numbers (they were just wrong, not even x86)
|
||||||
- It told me the `read` syscall can read until a `newline` by passing `newline` as last arg
|
- It told me the `read` syscall can read until a newline by passing `\n` as last arg
|
||||||
|
|
||||||
So I had to fix all that. I tried to exclusively use the AI to get answers to my questions, however I did sometimes revert to the internet.
|
So I had to fix all that. I tried to exclusively use the AI to get answers to my questions, however I did sometimes revert to the internet.
|
||||||
It was still a good starting point and provided lots of help.
|
It was still a good starting point and provided lots of help.\n
|
||||||
At the end of the ~~evening~~ next morning I had coded the task in x86-64 assembly.
|
At the end of the ~~evening~~ next morning I had successfully coded the task in x86-64 assembly! :smiley:
|
||||||
|
|
||||||
To test scanf, I gave it the exact same prompt but said I wanted it in *c*.
|
I also gave it the exact same prompt again but said I wanted it in **C**.
|
||||||
It instantly produced compilable c code that gave the right answer...
|
It instantly produced compile-able c code that gave the right answer...
|
||||||
```shell
|
```shell
|
||||||
# for the assembly program
|
# for the assembly program
|
||||||
make asm
|
make asm
|
||||||
./day4
|
./day4
|
||||||
# for the c promgram
|
# for the c promgram
|
||||||
make c
|
make c
|
||||||
./day4
|
./day4 "section-pairs.txt"
|
||||||
```
|
```
|
||||||
|
@ -1,71 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file was generated by OpenAI and was not modified apart from this comment. The prompt was:
|
|
||||||
* consider a text file like this:
|
|
||||||
* 22-65,22-66
|
|
||||||
* 91-94,63-91
|
|
||||||
* 6-88,5-31
|
|
||||||
* 85-86,64-86
|
|
||||||
* 40-65,40-65
|
|
||||||
* 25-82,24-94
|
|
||||||
*
|
|
||||||
* write c program that:
|
|
||||||
* - reads a text file like this line by line and count how often one of the two ranges contains the other.
|
|
||||||
* - accepts the filename via command line arg
|
|
||||||
* - outputs the result using printf with a suitable string
|
|
||||||
* A range r1 contains the other r2 when the r1.lower <= r2.lower and r1.upper >= r2.upper
|
|
||||||
* the program needs to output how often that is the case in the entire file
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 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 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) ||
|
|
||||||
(range2.lower <= range1.lower && range2.upper >= range1.upper)) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close the file
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
// Print the result
|
|
||||||
printf("The file contains %d lines where one of the two ranges contains the other\n", count);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
12
4/prompt.md
12
4/prompt.md
@ -1,4 +1,4 @@
|
|||||||
# original prompt
|
# Here is what I prompted OpenAI with to get day4-by-ai.c and day4.by.ai.s:
|
||||||
consider a text file like this:
|
consider a text file like this:
|
||||||
22-65,22-66
|
22-65,22-66
|
||||||
91-94,63-91
|
91-94,63-91
|
||||||
@ -14,13 +14,3 @@ write an gnu-x86 assembly program that:
|
|||||||
A range r1 contains the other r2 when the r1.lower <= r2.lower and r1.upper >= r2.upper
|
A range r1 contains the other r2 when the r1.lower <= r2.lower and r1.upper >= r2.upper
|
||||||
the program needs to output how often that is the case in the entire file
|
the program needs to output how often that is the case in the entire file
|
||||||
|
|
||||||
# add task 2 to c code:
|
|
||||||
can you add another check to the c code:
|
|
||||||
it should now also check if one of the ranges overlaps the other.
|
|
||||||
A range r1 overlaps the other r2 if:
|
|
||||||
- r1.lower == r2.lower
|
|
||||||
- r1.upper == r2.upper
|
|
||||||
- r1.lower > r2.lower and r1.lower <= r2.upper
|
|
||||||
- r1.upper < r1.upper r1.upper >= r2.lower
|
|
||||||
...
|
|
||||||
sorry i failed to mention that the overlaps and containment should be counted separately
|
|
||||||
|
@ -6,7 +6,7 @@ I am not necessarily trying to do it the best or most efficient way, nor choose
|
|||||||
|
|
||||||
The directories for each day contain my input file, my code and a README that shows how to run the code and what approach I used.
|
The directories for each day contain my input file, my code and a README that shows how to run the code and what approach I used.
|
||||||
|
|
||||||
I recommend you take at look at 'my' solution for day *4*, which is definetly the most interesting to date.
|
I recommend you take at look at *my* solution for day **4**, which is definetly the most interesting to date.\n
|
||||||
|
|
||||||
Also, check out the repositories of my friends who do stuff in go, Visual Basic and php:
|
Also, check out the repositories of my friends who do stuff in go, Visual Basic and php:
|
||||||
- [Daniel](https://git.quintern.xyz/TheShinyMelon/AOC_2022)
|
- [Daniel](https://git.quintern.xyz/TheShinyMelon/AOC_2022)
|
||||||
|
Loading…
Reference in New Issue
Block a user