├── .gitignore ├── README.md ├── Solutions-2022-04-1.md └── openai.py /.gitignore: -------------------------------------------------------------------------------- 1 | **/llm_responses 2 | **/python_outputs 3 | **/input.txt 4 | **/20* 5 | .vscode 6 | __pycache__ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code, by GPT-3 2 | 3 | This solves [Advent of Code](https://adventofcode.com/) puzzles by having GPT-3 write code in response to the 4 | puzzle's input. 5 | 6 | I placed 1st on Day 4 Part 1 (2022) with this code, and 2nd on Day 3 Part 1 (2022) with a 7 | previous version. 8 | 9 | ## Table of Contents 10 | 11 | - [How it works](#how-it-works) 12 | - [How to use](#how-to-use) 13 | - [Installation](#installation) 14 | 15 | ## How it works 16 | 17 | The code is written in Python, and uses the OpenAI API to call GPT-3. 18 | It also uses the `aoc-cli` library to download the puzzle input and submit the answer. 19 | 20 | ## How to use 21 | 22 | ```bash 23 | python3 openai.py --day=2 24 | ``` 25 | 26 | All flags: 27 | 28 | - `--day` **(required)** - The day of the puzzle. 29 | - `--year` - The year of the puzzle. Defaults to the current year. 30 | - `--part` - The part of the puzzle. 31 | - `--n-workers` - The number of workers to use. Defaults to 1 32 | - `--runs` - The number of runs to make. Defaults to 200 33 | - `--stop-when-submitted` - Stop when the answer is submitted. Defaults to False 34 | 35 | Example: 36 | 37 | ```bash 38 | python3 openai.py --day=2 --year=2019 --runs=10 39 | ``` 40 | 41 | ## Installation 42 | 43 | ### Install aoc-cli 44 | 45 | All instructions can be found in the [aoc-cli repo](https://github.com/scarvalhojr/aoc-cli) 46 | 47 | Install the `aoc-cli` library: 48 | 49 | ```bash 50 | cargo install aoc-cli 51 | # or 52 | brew install scarvalhojr/tap/aoc-cli 53 | # or, for Windows 54 | winget install aoc-cli 55 | ``` 56 | 57 | And [add the AOC session cookie](https://github.com/scarvalhojr/aoc-cli#session-cookie). 58 | 59 | ### Install OpenAI API 60 | 61 | Install the `openai` library: 62 | 63 | ```bash 64 | pip install openai 65 | ``` 66 | 67 | Add an [OpenAI API key](https://beta.openai.com/account/api-keys) to the 68 | `OPENAI_API_KEY` environment variable. 69 | -------------------------------------------------------------------------------- /Solutions-2022-04-1.md: -------------------------------------------------------------------------------- 1 | # AoC GPT-3 Solutions — 2022 Day 4 Part 1 2 | 3 | Generated from 4 | 5 | ## Prompt 6 | 7 | ````` 8 | Here's an Advent of Code puzzle, which is a cryptic puzzle told through an imaginary story. You need to write code which solves it. The description includes an example of solving a simplified version of the puzzle. 9 | 10 | ----- 11 | 12 | Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique *ID number*, and each Elf is assigned a range of section IDs. 13 | 14 | However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments *overlap*. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a *big list of the section assignments for each pair* (your puzzle input). 15 | 16 | For example, consider the following list of section assignment pairs: 17 | 18 | ``` 19 | 2-4,6-8 20 | 2-3,4-5 21 | 5-7,7-9 22 | 2-8,3-7 23 | 6-6,4-6 24 | 2-6,4-8 25 | 26 | ``` 27 | 28 | For the first few pairs, this list means: 29 | 30 | * Within the first pair of Elves, the first Elf was assigned sections `2-4` (sections `2`, `3`, and `4`), while the second Elf was assigned sections `6-8` (sections `6`, `7`, `8`). 31 | * The Elves in the second pair were each assigned two sections. 32 | * The Elves in the third pair were each assigned three sections: one got sections `5`, `6`, and `7`, while the other also got `7`, plus `8` and `9`. 33 | 34 | This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this: 35 | 36 | ``` 37 | .234..... 2-4 38 | .....678. 6-8 39 | 40 | .23...... 2-3 41 | ...45.... 4-5 42 | 43 | ....567.. 5-7 44 | ......789 7-9 45 | 46 | .2345678. 2-8 47 | ..34567.. 3-7 48 | 49 | .....6... 6-6 50 | ...456... 4-6 51 | 52 | .23456... 2-6 53 | ...45678. 4-8 54 | 55 | ``` 56 | 57 | Some of the pairs have noticed that one of their assignments *fully contains* the other. For example, `2-8` fully contains `3-7`, and `6-6` is fully contained by `4-6`. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are `*2*` such pairs. 58 | 59 | *In how many assignment pairs does one range fully contain the other?* 60 | 61 | 62 | 63 | 64 | 65 | ----- 66 | 67 | Now write python3 code which prints the correct answer. Print any information which supports your answer on the same line as the answer. The input is in a file `input.txt`. 68 | 69 | ```python 70 | ````` 71 | 72 | ## Correct answers 73 | 74 | ### Run 1 75 | 76 | GPT-3 generated: 77 | 78 | ```python 79 | # define a function to read the input from the file 80 | def readFile(filename): 81 | sectionValues = [] 82 | with open(filename, "r") as f: 83 | for line in f: 84 | sections = line.strip().split(",") 85 | start1, end1 = sections[0].split('-') 86 | start2, end2 = sections[1].split('-') 87 | start1, end1, start2, end2 = map(int, [start1, end1, start2, end2]) 88 | sectionValues.append([start1, end1, start2, end2]) 89 | return sectionValues 90 | 91 | # define a function to check if one range fully contains the other 92 | def checkRange(start1, end1, start2, end2): 93 | if (start1 <= start2 and end1 >= end2) or (start2 <= start1 and end2 >= end1): 94 | return True 95 | else: 96 | return False 97 | 98 | # read the input 99 | sectionValues = readFile('input.txt') 100 | # count the number of pairs where one range fully contains the other 101 | count = 0 102 | for start1, end1, start2, end2 in sectionValues: 103 | if checkRange(start1, end1, start2, end2): 104 | count += 1 105 | 106 | # print the result 107 | print("Number of pairs where one range fully contains the other: " + str(count)) 108 | ``` 109 | 110 | Running the python over the input generated: 111 | 112 | ``` 113 | Number of pairs where one range fully contains the other: 567 114 | ``` 115 | 116 | We parsed `567` as an answer from this output, which was correct. 117 | 118 | ### Run 27 119 | 120 | GPT-3 generated: 121 | 122 | ```python 123 | def make_range(pair): 124 | # Takes a string with two numbers separated by a hyphen and returns 125 | # the range of integers between those two numbers. 126 | start, end = pair.split('-') 127 | return range(int(start), int(end)+1) 128 | 129 | # Read input from file 130 | with open('input.txt', 'r') as f: 131 | content = f.read().splitlines() # each line is a string 132 | 133 | # Iterate through each line 134 | pairs = 0 135 | for line in content: 136 | # Split up each pair of ranges into two separate strings 137 | pair1, pair2 = line.split(',') 138 | 139 | # Create lists of the values in each range 140 | list_1 = make_range(pair1) 141 | list_2 = make_range(pair2) 142 | 143 | # Check if first range contains the second 144 | if all(i in list_1 for i in list_2): 145 | pairs += 1 146 | 147 | # Check if second range contains the first 148 | elif all(i in list_2 for i in list_1): 149 | pairs += 1 150 | 151 | # Print answer 152 | print(f'Number of assignment pairs such that one range fully contains the other: {pairs}') 153 | ``` 154 | 155 | Running the python over the input generated: 156 | 157 | ``` 158 | Number of assignment pairs such that one range fully contains the other: 567 159 | ``` 160 | 161 | We parsed `567` as an answer from this output, which was correct. 162 | 163 | ### Run 40 164 | 165 | GPT-3 generated: 166 | 167 | ```python 168 | # Read lines from the file 169 | pairs = [] 170 | with open('input.txt', 'r') as fp: 171 | for line in fp: 172 | pairs.append(tuple(line.strip().split(','))) 173 | 174 | # Compare each pair and see if one range fully contains the other 175 | fully_contained_pairs = 0 176 | for pair in pairs: 177 | a, b = pair 178 | # Unpack the range strings and convert to integers 179 | a_start, a_end = [int(i) for i in a.split('-')] 180 | b_start, b_end = [int(i) for i in b.split('-')] 181 | # Check if one range fully contains the other 182 | if a_start <= b_start and b_end <= a_end: 183 | fully_contained_pairs += 1 184 | elif b_start <= a_start and a_end <= b_end: 185 | fully_contained_pairs += 1 186 | 187 | # Output the answer 188 | print(f"The number of assignment pairs where one range fully contains the other is {fully_contained_pairs}") 189 | ``` 190 | 191 | Running the python over the input generated: 192 | 193 | ``` 194 | The number of assignment pairs where one range fully contains the other is 567 195 | ``` 196 | 197 | We parsed `567` as an answer from this output, which was correct. 198 | 199 | ### Run 47 200 | 201 | GPT-3 generated: 202 | 203 | ```python 204 | #read the lines into a list 205 | with open('input.txt', 'r') as file: 206 | ranges = file.readlines() 207 | 208 | #initialize count 209 | count = 0 210 | 211 | #loop over each line 212 | for line in ranges: 213 | #split line into two parts 214 | p1, p2 = line.split(',') 215 | 216 | #extract minimum and maximum values 217 | a1, b1 = [int(x) for x in p1.split('-')] 218 | a2, b2 = [int(x) for x in p2.split('-')] 219 | 220 | #update count if one range is fully contained in the other 221 | if (a1 <= a2 and b2 <= b1) or (a2 <= a1 and b1 <= b2): 222 | count += 1 223 | 224 | #print answer 225 | print("The answer is:", count, "(pairs whose ranges fully contain the other)") 226 | ``` 227 | 228 | Running the python over the input generated: 229 | 230 | ``` 231 | The answer is: 567 (pairs whose ranges fully contain the other) 232 | ``` 233 | 234 | We parsed `567` as an answer from this output, which was correct. 235 | 236 | ### Run 48 237 | 238 | GPT-3 generated: 239 | 240 | ```python 241 | # open file and read in lines 242 | f = open("input.txt", "r") 243 | lines = f.readlines() 244 | 245 | # set up counters 246 | total_assignment_pairs = 0 247 | fully_contains_pairs = 0 248 | 249 | # go through each line 250 | for pair in lines: 251 | total_assignment_pairs += 1 # increment counter for each line 252 | 253 | # split on comma and strip empty characters 254 | assignment_pair = pair.strip().split(',') 255 | 256 | # parse each assignment 257 | assignment_1, assignment_2 = assignment_pair[0], assignment_pair[1] 258 | range_1 = [int(x) for x in assignment_1.split('-')] 259 | range_2 = [int(x) for x in assignment_2.split('-')] 260 | 261 | # check if one range fully contains the other 262 | if range_1[0] <= range_2[0] and range_1[1] >= range_2[1]: 263 | fully_contains_pairs += 1 264 | elif range_2[0] <= range_1[0] and range_2[1] >= range_1[1]: 265 | fully_contains_pairs += 1 266 | 267 | # print answer 268 | print('Answer:',fully_contains_pairs,'pairs of assignments out of',total_assignment_pairs) 269 | ``` 270 | 271 | Running the python over the input generated: 272 | 273 | ``` 274 | Answer: 567 pairs of assignments out of 1000 275 | ``` 276 | 277 | We parsed `['567', '1000']` as answers from this output, which was correct. 278 | 279 | ### Run 62 280 | 281 | GPT-3 generated: 282 | 283 | ```python 284 | # Open input file 285 | input_file = open('input.txt') 286 | 287 | # Initialize counter 288 | fully_contained_range_count = 0 289 | 290 | # Iterate over pairs of ranges 291 | for pair in input_file.readlines(): 292 | range_1, range_2 = pair.strip().split(',') 293 | # Extract integers for each range 294 | range_1_start, range_1_end = map(int, range_1.split('-')) 295 | range_2_start, range_2_end = map(int, range_2.split('-')) 296 | # Check if one range fully contains the other 297 | if range_1_start <= range_2_start and range_1_end >= range_2_end: 298 | fully_contained_range_count += 1 299 | elif range_2_start <= range_1_start and range_2_end >= range_1_end: 300 | fully_contained_range_count += 1 301 | 302 | # Print the answer 303 | print(f"Answer: {fully_contained_range_count}") 304 | ``` 305 | 306 | Running the python over the input generated: 307 | 308 | ``` 309 | Answer: 567 310 | ``` 311 | 312 | We parsed `567` as an answer from this output, which was correct. 313 | 314 | ### Run 63 315 | 316 | GPT-3 generated: 317 | 318 | ```python 319 | pairs = [] 320 | lines = open("input.txt").readlines() 321 | for line in lines: 322 | pair = line.split(",") 323 | pairs.append(pair) 324 | 325 | # convert the ranges to sets 326 | sets = [] 327 | for pair in pairs: 328 | set1 = set(range(int(pair[0].split("-")[0]), int(pair[0].split("-")[1])+1)) 329 | set2 = set(range(int(pair[1].split("-")[0]), int(pair[1].split("-")[1])+1)) 330 | sets.append([set1, set2]) 331 | 332 | # check how many sets have one contained in the other 333 | contained = 0 334 | for set1, set2 in sets: 335 | if set1.issubset(set2) or set2.issubset(set1): 336 | contained += 1 337 | 338 | # print answer 339 | print("Number of assignment pairs where one range fully contains the other: {cont}". 340 | format(cont=contained)) 341 | ``` 342 | 343 | Running the python over the input generated: 344 | 345 | ``` 346 | Number of assignment pairs where one range fully contains the other: 567 347 | ``` 348 | 349 | We parsed `567` as an answer from this output, which was correct. 350 | 351 | ### Run 56 352 | 353 | GPT-3 generated: 354 | 355 | ```python 356 | # Define a range class to represent ranges of integers 357 | class Range: 358 | def __init__(self, first, last): 359 | self.first = first 360 | self.last = last 361 | 362 | # Check whether range is fully contained in another range 363 | def fullyContainedIn(self, other): 364 | return self.first >= other.first and self.last <= other.last 365 | 366 | # Read the range pairs from a file 367 | with open('input.txt') as file: 368 | lines = file.readlines() 369 | 370 | # Count the number of assignment pairs where one range is fully contained in the other 371 | count = 0 372 | for line in lines: 373 | # Split the line into two ranges 374 | firstRange, secondRange = line.strip().split(',') 375 | firstSplit = firstRange.split('-') 376 | secondSplit = secondRange.split('-') 377 | firstRangeObj = Range(int(firstSplit[0]), int(firstSplit[1])) 378 | secondRangeObj = Range(int(secondSplit[0]), int(secondSplit[1])) 379 | 380 | # Check if one range is fully contained in the other 381 | if (firstRangeObj.fullyContainedIn(secondRangeObj) or secondRangeObj.fullyContainedIn(firstRangeObj)): 382 | count += 1 383 | 384 | # Print the result 385 | print("Answer: %d assignment pairs have one range fully contained in the other" % count) 386 | ``` 387 | 388 | Running the python over the input generated: 389 | 390 | ``` 391 | Answer: 567 assignment pairs have one range fully contained in the other 392 | ``` 393 | 394 | We parsed `567` as an answer from this output, which was correct. 395 | 396 | ### Run 69 397 | 398 | GPT-3 generated: 399 | 400 | ```python 401 | # Open input file 402 | f = open('input.txt', 'r') 403 | 404 | # Initialize counter and read each line in the input 405 | count = 0 406 | for line in f: 407 | 408 | # Split the line into two parts: the two ranges 409 | range1, range2 = line.split(',') 410 | 411 | # Get the start and end of each range 412 | start1, end1 = [int(x) for x in range1.split('-')] 413 | start2, end2 = [int(x) for x in range2.split('-')] 414 | 415 | # Compare the ranges and increment our counter 416 | if (start1 <= start2 and end1 >= end2) or (start1 >= start2 and end1 <= end2): 417 | count += 1 418 | 419 | # Print the answer 420 | print(f'Number of assignment pairs where one range fully contains the other: {count}') 421 | 422 | # Close the input file 423 | f.close() 424 | ``` 425 | 426 | Running the python over the input generated: 427 | 428 | ``` 429 | Number of assignment pairs where one range fully contains the other: 567 430 | ``` 431 | 432 | We parsed `567` as an answer from this output, which was correct. 433 | ## Incorrect answers 434 | 435 | ### Run 3 436 | 437 | GPT-3 generated: 438 | 439 | ```python 440 | with open("input.txt", "r") as f: 441 | pairs = [line.strip("\n").split(",") for line in f] 442 | 443 | tot_ful_pairs = 0 444 | for pair in pairs: 445 | [A,B] = [tuple(map(int,i.split('-'))) for i in pair] 446 | if (A[0] <= B[0] and A[1] >= B[1]): 447 | overlaps = max(0, A[1] - B[0]) 448 | tot_ful_pairs += 1 449 | 450 | print("There are {} assignment pairs, in which one range fully contains the other".format(tot_ful_pairs)) 451 | ``` 452 | 453 | Running the python over the input generated: 454 | 455 | ``` 456 | There are 266 assignment pairs, in which one range fully contains the other 457 | ``` 458 | 459 | We parsed `266` as an answer from this output, which was incorrect. 460 | 461 | ### Run 8 462 | 463 | GPT-3 generated: 464 | 465 | ```python 466 | # Read in the input 467 | input_file = open("input.txt", "r") 468 | ranges = [line.split(',') for line in input_file] 469 | input_file.close() 470 | 471 | # Combine the ranges in each line 472 | ranges_combined = [(rng[0].split('-'), rng[1].split('-')) for rng in ranges] 473 | 474 | # Find pairs with fully contained ranges 475 | counter = 0 476 | for range_pair in ranges_combined: 477 | # Get the start and end of each range 478 | start1, end1 = range_pair[0][0], range_pair[0][1] 479 | start2, end2 = range_pair[1][0], range_pair[1][1] 480 | # If the start of range 1 is less than or the same as the start of range 2 481 | # and the end of range 1 is greater than or the same as the end of range 2 482 | # then range 1 fully contains range 2 483 | if start1 <= start2 and end1 >= end2: 484 | counter += 1 485 | # If the start of range 2 is less than or the same as the start of range 1 486 | # and the end of range 2 is greater than or the same as the end of range 1 487 | # then range 2 fully contains range 1 488 | if start2 <= start1 and end2 >= end1: 489 | counter += 1 490 | 491 | # Print the answer 492 | print("Number of fully contained ranges: " + str(counter)) 493 | ``` 494 | 495 | Running the python over the input generated: 496 | 497 | ``` 498 | Number of fully contained ranges: 533 499 | ``` 500 | 501 | We parsed `533` as an answer from this output, which was incorrect. 502 | 503 | ### Run 12 504 | 505 | GPT-3 generated: 506 | 507 | ```python 508 | count = 0 509 | # open the input file 510 | with open('input.txt') as fp: 511 | lines = fp.readlines() 512 | # process each line in the input file 513 | for i, line in enumerate(lines): 514 | x = line.strip().split(",") 515 | # decompose each line into its two parts 516 | x_start, x_end = map(int, x[0].split("-")) 517 | y_start, y_end = map(int, x[1].split("-")) 518 | # check if x is a full subset of y 519 | if x_start >= y_start and x_end <= y_end: 520 | count += 1 521 | print(f"The answer is {count}") 522 | ``` 523 | 524 | Running the python over the input generated: 525 | 526 | ``` 527 | The answer is 322 528 | ``` 529 | 530 | We parsed `322` as an answer from this output, which was incorrect. 531 | 532 | ### Run 10 533 | 534 | GPT-3 generated: 535 | 536 | ```python 537 | # read in input 538 | file = open('input.txt', 'r') 539 | inputs = [pair.strip() for pair in file.readlines()] 540 | 541 | # create list of overlapping section pairs 542 | overlapping_pairs = [] 543 | for pair in inputs: 544 | start1,end1 = pair.split(',')[0].split('-') 545 | start2,end2 = pair.split(',')[1].split('-') 546 | if (start1 <= start2 and end1 >= end2) or (start2 <= start1 and end2 >= end1): 547 | overlapping_pairs.append(pair) 548 | 549 | # print answer 550 | print('Number of overlapping pairs: ', len(overlapping_pairs), '\nPairs are:', overlapping_pairs) 551 | ``` 552 | 553 | Running the python over the input generated: 554 | 555 | ``` 556 | Number of overlapping pairs: 584 ...[truncated] 557 | ``` 558 | 559 | We parsed `584` as an answer from this output, which was incorrect. 560 | 561 | ### Run 15 562 | 563 | GPT-3 generated: 564 | 565 | ```python 566 | #open file 567 | file = open('input.txt', 'r') 568 | 569 | # set counter variable to 0 570 | counter = 0 571 | 572 | # read each line in the file 573 | for line in file: 574 | # split line along commas 575 | sections = line.split(',') 576 | # split the first section along the dash 577 | first_start, first_end = sections[0].split('-') 578 | # convert start and end of first section to int 579 | first_start, first_end = int(first_start), int(first_end) 580 | # split the second section along the dash 581 | second_start, second_end = sections[1].split('-') 582 | # convert start and end of second section to int 583 | second_start, second_end = int(second_start), int(second_end) 584 | # if the end of the first section is greater than the end of the second section, 585 | # and the end of the first section is equal to or greater than the start of the second section 586 | if first_end > second_end and first_end >= second_start: 587 | # increment the counter 588 | counter += 1 589 | 590 | #print the answer 591 | print(f'The answer is {counter}') 592 | ``` 593 | 594 | Running the python over the input generated: 595 | 596 | ``` 597 | The answer is 404 598 | ``` 599 | 600 | We parsed `404` as an answer from this output, which was incorrect. 601 | 602 | ### Run 11 603 | 604 | GPT-3 generated: 605 | 606 | ```python 607 | # initiate a count of fully overlapping ranges 608 | overlap_count=0 609 | 610 | # open the input file and use a loop to process each line 611 | with open('input.txt') as f: 612 | for line in f: 613 | # split the line into separate ranges, and extract the start and end points of the ranges 614 | ranges=line.rstrip().split(',') 615 | first_start, first_end, second_start, second_end=ranges[0].split('-')[0],ranges[0].split('-')[1],ranges[1].split('-')[0],ranges[1].split('-')[1] 616 | # determine if one range fully contains the other 617 | if (first_start <= second_start) and (first_end >= second_end): 618 | overlap_count += 1 619 | elif (second_start <= first_start) and (second_end >= first_end): 620 | overlap_count += 1 621 | 622 | # print the overlaps 623 | print(f'Number of overlapping ranges: {overlap_count}') 624 | ``` 625 | 626 | Running the python over the input generated: 627 | 628 | ``` 629 | Number of overlapping ranges: 584 630 | ``` 631 | 632 | We parsed `584` as an answer from this output, which was incorrect. 633 | 634 | ### Run 20 635 | 636 | GPT-3 generated: 637 | 638 | ```python 639 | total_contained_sectons = 0 640 | 641 | with open('input.txt', 'r') as input_file: 642 | for line in input_file: 643 | section_a, section_b = line.strip().split(',') 644 | a_begin, a_end = [int(x) for x in section_a.split('-')] 645 | b_begin, b_end = [int(x) for x in section_b.split('-')] 646 | 647 | if a_begin <= b_begin and a_end >= b_end: 648 | print('Section {} contains section {}'.format(section_a, section_b)) 649 | total_contained_sectons += 1 650 | 651 | print('{} pairs contain the other range'.format(total_contained_sectons)) 652 | ``` 653 | 654 | Running the python over the input generated: 655 | 656 | ``` 657 | Section 9-89 contains section 88-88...[truncated] 658 | ``` 659 | 660 | We parsed `['9', '89', '88', '88']` as answers from this output, which was incorrect. 661 | 662 | ### Run 22 663 | 664 | GPT-3 generated: 665 | 666 | ```python 667 | # Read data from input file 668 | data = open("input.txt", "r").read() 669 | 670 | # Parse the input data into a list of pairs in the form [start, end] 671 | sections = [] 672 | for line in data.strip().split("\n"): 673 | start, end = line.split(",") 674 | sections.append([int(start.split('-')[1]), int(end.split('-')[1])]) 675 | 676 | # Count the number of overlapping pairs 677 | num_overlaps = 0 678 | for i in range(len(sections)): 679 | for j in range(i + 1, len(sections)): 680 | if (sections[i][0] >= sections[j][0] and sections[i][1] <= sections[j][1]) or (sections[j][0] >= sections[i][0] and sections[j][1] <= sections[i][1]): 681 | num_overlaps += 1 682 | 683 | # Print the result 684 | print("Number of overlapping pairs:", num_overlaps) 685 | ``` 686 | 687 | Running the python over the input generated: 688 | 689 | ``` 690 | Number of overlapping pairs: 136400 691 | ``` 692 | 693 | We parsed `136400` as an answer from this output, which was incorrect. 694 | 695 | ### Run 16 696 | 697 | GPT-3 generated: 698 | 699 | ```python 700 | # set of pairs to store all pairs 701 | pairs = set() 702 | # loop through the input file 703 | with open('input.txt') as f: 704 | for line in f.readlines(): 705 | # split the line into two parts (A and B) 706 | A, B = line.strip().split(',') 707 | # extract the min and max of the range 708 | min_A, max_A = map(int, A.split('-')) 709 | min_B, max_B = map(int, B.split('-')) 710 | # Case 1: A fully contains B 711 | if min_A < min_B and max_A > max_B: 712 | pairs.add(f"{A} fully contains {B}") 713 | # Case 2: B fully contains A 714 | if min_A > min_B and max_A < max_B: 715 | pairs.add(f"{B} fully contains {A}") 716 | 717 | # Print Answer 718 | print(f"Answer: {len(pairs)} assignment pairs where one range fully contains the other: {list(pairs)}") 719 | ``` 720 | 721 | Running the python over the input generated: 722 | 723 | ``` 724 | Answer: 291 assignment pairs where one range fully contains the other: ['51-55 fully contains 53-54', '1-93 fully contains 2-2', '13-78 fully contains 17-72', '18-49 fully contains 35-48', '26-99 fully contains 27-98', '23-98 fully contains 51-95', '12-54 fully contains 13-13', '9-69 fully contains 68-68', '47-92 fully contains 48-91', '9-45 fully contains 34-37', '3-65 fully contains 4-64', '8-80 fully contains 20-79', '5-90 fully contains 30-52', '21-72 fully contains 41-52', '26-92 fully contains 34-65', '25-90 fully contains 26-89', '10-96 fully contains 95-95', '7-49 fully contains 8-48', '18-95 fully contains 94-94', '31-86 fully contains 32-85', '24-74 fully contains 73-73', '8-10 fully contains 9-9', '13-90 fully contains 14-89', '20-96 fully contains 48-95', '25-93 fully contains 50-92', '20-40 fully contains 39-39', '10-49 fully contains 11-44', '56-66 fully contains 62-65', '74-89 fully contains 88-88', '20-68 fully contains 38-67', '22-78 fully contains 23-77', '34-74 fully contains 35-62', '73-92 fully contains 74-91', '25-99 fully contains 26-82', '81-92 fully contains 82-91', '31-89 fully contains 88-88', '16-58 fully contains 17-57', '57-74 fully contains 58-58', '18-38 fully contains 37-37', '5-93 fully contains 6-92', '12-86 fully contains 13-13', '21-23 fully contains 22-22', '17-90 fully contains 24-88', '18-93 fully contains 29-83', '45-56 fully contains 46-55', '68-98 fully contains 69-78', '48-98 fully contains 49-75', '70-87 fully contains 71-86', '3-63 fully contains 42-62', '2-99 fully contains 3-98', '38-43 fully contains 39-42', '8-65 fully contains 9-9', '88-94 fully contains 89-93', '17-89 fully contains 18-88', '5-94 fully contains 13-93', '25-82 fully contains 26-81', '54-99 fully contains 72-88', '48-83 fully contains 49-49', '9-99 fully contains 98-98', '38-45 fully contains 44-44', '46-74 fully contains 47-47', '12-61 fully contains 13-35', '81-91 fully contains 87-87', '46-78 fully contains 50-61', '19-90 fully contains 20-89', '24-86 fully contains 72-85', '8-99 fully contains 9-94', '61-78 fully contains 77-77', '7-96 fully contains 8-8', '19-95 fully contains 20-94', '26-99 fully contains 84-97', '12-93 fully contains 67-86', '20-40 fully contains 21-21', '30-77 fully contains 31-76', '5-77 fully contains 6-76', '2-49 fully contains 4-43', '41-47 fully contains 46-46', '15-63 fully contains 16-62', '38-47 fully contains 39-43', '2-73 fully contains 3-13', '10-78 fully contains 19-63', '45-89 fully contains 86-88', '54-97 fully contains 55-83', '7-32 fully contains 8-8', '35-89 fully contains 36-88', '24-96 fully contains 25-25', '11-28 fully contains 12-27', '4-9 fully contains 8-8', '3-92 fully contains 17-91', '3-95 fully contains 6-91', '2-99 fully contains 3-97', '8-65 fully contains 9-64', '22-67 fully contains 59-66', '7-98 fully contains 8-97', '40-97 fully contains 70-79', '9-94 fully contains 20-77', '12-40 fully contains 20-36', '10-85 fully contains 11-11', '39-92 fully contains 56-91', '27-84 fully contains 28-28', '11-84 fully contains 67-75', '6-87 fully contains 86-86', '4-64 fully contains 5-63', '1-61 fully contains 4-30', '8-89 fully contains 9-88', '5-95 fully contains 74-93', '2-87 fully contains 62-73', '9-53 fully contains 39-42', '67-93 fully contains 68-92', '88-90 fully contains 89-89', '33-47 fully contains 34-39', '69-89 fully contains 70-70', '54-93 fully contains 72-92', '18-86 fully contains 19-85', '31-48 fully contains 38-47', '43-69 fully contains 44-68', '6-86 fully contains 85-85', '57-64 fully contains 58-63', '16-42 fully contains 23-41', '3-74 fully contains 31-73', '8-89 fully contains 16-79', '4-89 fully contains 25-75', '5-58 fully contains 6-54', '54-69 fully contains 68-68', '1-96 fully contains 2-95', '40-88 fully contains 41-59', '50-75 fully contains 51-74', '4-90 fully contains 8-43', '33-46 fully contains 34-34', '36-62 fully contains 37-61', '4-60 fully contains 23-56', '1-67 fully contains 3-14', '8-67 fully contains 9-66', '57-99 fully contains 84-88', '2-83 fully contains 61-82', '56-85 fully contains 74-83', '24-37 fully contains 36-36', '2-62 fully contains 13-61', '23-46 fully contains 25-38', '2-88 fully contains 17-64', '16-97 fully contains 17-96', '35-72 fully contains 36-71', '6-96 fully contains 7-21', '19-96 fully contains 20-95', '67-80 fully contains 68-79', '50-99 fully contains 51-51', '2-78 fully contains 3-3', '26-71 fully contains 70-70', '1-68 fully contains 2-67', '42-58 fully contains 43-57', '30-70 fully contains 31-69', '46-79 fully contains 78-78', '14-72 fully contains 16-71', '72-90 fully contains 73-89', '28-74 fully contains 29-73', '25-98 fully contains 26-97', '27-33 fully contains 30-32', '9-96 fully contains 95-95', '8-90 fully contains 9-43', '27-85 fully contains 39-84', '5-98 fully contains 6-97', '6-72 fully contains 7-55', '37-92 fully contains 74-91', '13-97 fully contains 14-96', '51-83 fully contains 67-74', '1-98 fully contains 2-79', '13-91 fully contains 53-90', '80-90 fully contains 82-89', '17-66 fully contains 18-18', '2-95 fully contains 50-94', '50-63 fully contains 60-62', '9-89 fully contains 88-88', '6-49 fully contains 14-33', '26-81 fully contains 27-48', '74-81 fully contains 75-80', '10-73 fully contains 11-11', '27-79 fully contains 28-28', '54-83 fully contains 82-82', '1-99 fully contains 2-98', '10-98 fully contains 94-97', '4-97 fully contains 5-45', '10-39 fully contains 25-38', '65-82 fully contains 78-81', '4-61 fully contains 8-35', '33-56 fully contains 34-55', '37-91 fully contains 38-90', '95-99 fully contains 96-98', '5-85 fully contains 6-84', '11-69 fully contains 12-36', '66-87 fully contains 67-86', '54-98 fully contains 77-97', '48-95 fully contains 49-94', '65-73 fully contains 68-72', '86-93 fully contains 87-91', '12-99 fully contains 18-97', '62-76 fully contains 63-63', '51-98 fully contains 52-80', '41-53 fully contains 42-52', '10-96 fully contains 11-95', '29-37 fully contains 30-36', '17-79 fully contains 78-78', '40-70 fully contains 41-67', '6-96 fully contains 10-70', '63-93 fully contains 64-92', '59-70 fully contains 60-60', '55-90 fully contains 56-89', '44-96 fully contains 45-45', '32-61 fully contains 33-60', '72-92 fully contains 91-91', '74-95 fully contains 77-84', '68-98 fully contains 69-96', '10-92 fully contains 11-11', '20-23 fully contains 21-22', '19-91 fully contains 42-88', '11-13 fully contains 12-12', '29-90 fully contains 30-89', '14-94 fully contains 15-15', '5-89 fully contains 6-6', '2-54 fully contains 3-53', '22-55 fully contains 23-54', '46-73 fully contains 66-72', '11-93 fully contains 12-40', '5-69 fully contains 8-68', '42-79 fully contains 48-78', '41-58 fully contains 53-57', '26-77 fully contains 27-76', '13-85 fully contains 14-84', '2-13 fully contains 3-3', '52-56 fully contains 53-53', '18-75 fully contains 19-74', '22-80 fully contains 23-79', '59-71 fully contains 66-70', '23-80 fully contains 24-42', '6-38 fully contains 7-37', '22-97 fully contains 23-93', '57-96 fully contains 60-95', '64-79 fully contains 78-78', '1-94 fully contains 2-2', '81-91 fully contains 84-84', '9-37 fully contains 33-35', '28-77 fully contains 29-29', '41-91 fully contains 70-87', '26-94 fully contains 43-91', '51-77 fully contains 76-76', '14-51 fully contains 15-50', '4-80 fully contains 46-70', '10-99 fully contains 11-98', '24-98 fully contains 25-25', '88-91 fully contains 89-90', '8-91 fully contains 52-88', '33-73 fully contains 34-34', '2-88 fully contains 3-24', '1-22 fully contains 2-21', '59-79 fully contains 78-78', '29-80 fully contains 30-79', '18-83 fully contains 19-82', '24-53 fully contains 25-25', '58-69 fully contains 59-68', '5-90 fully contains 9-30', '33-65 fully contains 34-64', '15-83 fully contains 46-48', '60-86 fully contains 83-85', '18-43 fully contains 19-42', '51-73 fully contains 72-72', '24-61 fully contains 25-60', '35-42 fully contains 36-41', '30-34 fully contains 33-33', '74-95 fully contains 78-94', '7-55 fully contains 54-54', '45-78 fully contains 77-77', '5-73 fully contains 7-47', '5-93 fully contains 8-89', '63-79 fully contains 77-78', '19-93 fully contains 20-82', '62-75 fully contains 63-74', '3-99 fully contains 4-98', '1-85 fully contains 2-2', '23-83 fully contains 82-82', '50-83 fully contains 82-82', '11-79 fully contains 12-78', '1-92 fully contains 26-79', '57-59 fully contains 58-58', '21-96 fully contains 22-52', '1-93 fully contains 2-92', '61-85 fully contains 84-84', '58-68 fully contains 59-63', '2-93 fully contains 3-23', '4-99 fully contains 98-98', '1-72 fully contains 2-71', '8-94 fully contains 93-93', '11-92 fully contains 12-81'] 725 | ``` 726 | 727 | We parsed `[]` as answers from this output, which was incorrect. 728 | 729 | ### Run 24 730 | 731 | GPT-3 generated: 732 | 733 | ```python 734 | # Read data 735 | file = open("input.txt", "r") 736 | data = file.read() 737 | 738 | # Split the data into lines 739 | lines = data.split("\n") 740 | 741 | # Go through each line, split the ranges into components 742 | # and assess them 743 | contained = 0 744 | for line in lines: 745 | if not line: 746 | continue 747 | (left, right) = line.split(",") 748 | (left_start, left_end) = left.split("-") 749 | (right_start, right_end) = right.split("-") 750 | if left_start <= right_start and right_end <= left_end: 751 | contained += 1 752 | 753 | # Print the results 754 | print("Total number of pairs fully contained: " + str(contained)) 755 | ``` 756 | 757 | Running the python over the input generated: 758 | 759 | ``` 760 | Total number of pairs fully contained: 265 761 | ``` 762 | 763 | We parsed `265` as an answer from this output, which was incorrect. 764 | 765 | ### Run 26 766 | 767 | GPT-3 generated: 768 | 769 | ```python 770 | # Open file and read line-by-line 771 | file = open("input.txt","r") 772 | lines = file.readlines() 773 | 774 | # Initialize counter 775 | counter = 0 776 | for line in lines: 777 | # Convert each line to a list of numbers 778 | pairs = line.split(',') 779 | range1 = [int(i) for i in pairs[0].split('-')] 780 | range2 = [int(i) for i in pairs[1].split('-')] 781 | 782 | # If one range starts after the other and ends before the other, then it is inside the other 783 | if range1[0] >= range2[0] and range1[1] <= range2[1]: 784 | counter += 1 785 | print("Range", line.strip(), "is inside another range") 786 | 787 | if range2[0] >= range1[0] and range2[1] <= range1[1]: 788 | counter += 1 789 | print("Range", line.strip(), "is inside another range") 790 | 791 | # Print answer 792 | print("There are", counter, "assignment pairs where one range fully contains the other.") 793 | 794 | # Close file 795 | file.close() 796 | ``` 797 | 798 | Running the python over the input generated: 799 | 800 | ``` 801 | Range 25-96,3-96 is inside another range...[truncated] 802 | ``` 803 | 804 | We parsed `['25', '96', '3', '96']` as answers from this output, which was incorrect. 805 | 806 | ### Run 31 807 | 808 | GPT-3 generated: 809 | 810 | ```python 811 | # Create list of all ranges from the input file 812 | ranges = [] 813 | with open("input.txt") as file: 814 | for line in file: 815 | (start1, end1) = line.split(',', 1)[0].split('-') 816 | (start2, end2) = line.split(',', 1)[1].split('-') 817 | ranges.append([(int(start1), int(end1)), (int(start2), int(end2))]) 818 | 819 | # Find pairs where one range fully contains the other 820 | count = 0 821 | for pair in ranges: 822 | if pair[0][0] <= pair[1][0] <= pair[0][1] <= pair[1][1] or pair[1][0] <= pair[0][0] <= pair[1][1] <= pair[0][1]: 823 | count += 1 824 | 825 | print("Pairs where one range fully contains the other: {}".format(count)) 826 | ``` 827 | 828 | Running the python over the input generated: 829 | 830 | ``` 831 | Pairs where one range fully contains the other: 614 832 | ``` 833 | 834 | We parsed `614` as an answer from this output, which was incorrect. 835 | 836 | ### Run 37 837 | 838 | GPT-3 generated: 839 | 840 | ```python 841 | # Read in the lines from the file and store them in a list 842 | input_file = open("input.txt") 843 | lines = input_file.readlines() 844 | input_file.close() 845 | 846 | # For each line in the list 847 | contained = 0 848 | for line in lines: 849 | # Split the line into a pair of ranges, 850 | rng1,rng2 = line.split(',') 851 | 852 | #Split the ranges into start and end numbers 853 | start1, end1 = rng1.split('-') 854 | start2, end2 = rng2.split('-') 855 | 856 | # If the first range starts before the second and ends after it, it fully contains it 857 | if int(start1) <= int(start2) and int(end1) >= int(end2): 858 | contained += 1 859 | 860 | print('Answer: ' + str(contained) + " pairs of ranges where one range fully contains the other") 861 | ``` 862 | 863 | Running the python over the input generated: 864 | 865 | ``` 866 | Answer: 266 pairs of ranges where one range fully contains the other 867 | ``` 868 | 869 | We parsed `266` as an answer from this output, which was incorrect. 870 | 871 | ### Run 42 872 | 873 | GPT-3 generated: 874 | 875 | ```python 876 | # Read the file 877 | with open('input.txt') as f: 878 | data = f.read().splitlines() 879 | 880 | # Initialize a count 881 | fully_contained_pairs = 0 882 | 883 | # Iterate through each entry 884 | for item in data: 885 | # Split the range into start and end points 886 | item = item.split(',') 887 | start_vals = item[0].split('-') 888 | end_vals = item[1].split('-') 889 | start_pair = [int(start_vals[0]), int(start_vals[1])] 890 | end_pair = [int(end_vals[0]), int(end_vals[1])] 891 | 892 | # If the start is greater and the end is less than the other 893 | # pair, then one fully contains the other 894 | if start_pair[0] > end_pair[0] and start_pair[1] < end_pair[1]: 895 | fully_contained_pairs += 1 896 | print(f'Range {start_pair[0]}-{start_pair[1]} is fully contained within {end_pair[0]}-{end_pair[1]}') 897 | 898 | # Print the answer 899 | print(f'\nNumber of fully contained pairs: {fully_contained_pairs}') 900 | ``` 901 | 902 | Running the python over the input generated: 903 | 904 | ``` 905 | Range 35-48 is fully contained within 18-49...[truncated] 906 | ``` 907 | 908 | We parsed `['35', '48', '18', '49']` as answers from this output, which was incorrect. 909 | 910 | ### Run 33 911 | 912 | GPT-3 generated: 913 | 914 | ```python 915 | # Read the input 916 | with open('input.txt') as f: 917 | data = f.read() 918 | 919 | # Parse the input 920 | input_ranges = [] 921 | for line in data.split('\n'): 922 | ranges = line.split(',') 923 | if len(ranges) != 2: 924 | continue 925 | range1 = range(int(ranges[0].split('-')[0]), int(ranges[0].split('-')[1]) + 1) 926 | range2 = range(int(ranges[1].split('-')[0]), int(ranges[1].split('-')[1]) + 1) 927 | input_ranges.append((range1, range2)) 928 | 929 | # Find overlapping ranges 930 | count = 0 931 | for r1, r2 in input_ranges: 932 | if len(set(r1) & set(r2)) == len(r2): 933 | count += 1 934 | 935 | # Print the result 936 | print('Answer:', count, '(Pairs where one range fully contains the other)') 937 | ``` 938 | 939 | Running the python over the input generated: 940 | 941 | ``` 942 | Answer: 266 (Pairs where one range fully contains the other) 943 | ``` 944 | 945 | We parsed `266` as an answer from this output, which was incorrect. 946 | 947 | ### Run 38 948 | 949 | GPT-3 generated: 950 | 951 | ```python 952 | # Read input file 953 | input_file = open("input.txt", "r") 954 | 955 | # Initialize counter 956 | counter = 0 957 | 958 | # Iterate over each line 959 | for line in input_file: 960 | # Separate individual pairs 961 | pairlist = line.split(',') 962 | 963 | # Iterate over the first and second pair 964 | for i in range(len(pairlist) - 1): 965 | for j in range(i + 1, len(pairlist)): 966 | # Retrieve individual ranges 967 | first_pair = pairlist[i].split('-') 968 | second_pair = pairlist[j].split('-') 969 | # Check if the ranges range fully contain each other 970 | if second_pair[0] >= first_pair[0] and second_pair[1] <= first_pair[1]: 971 | counter += 1 972 | 973 | # Print the answers 974 | print("Number of assignment pairs in which one range fully contains the other: " + str(counter)) 975 | 976 | # Close the file 977 | input_file.close() 978 | ``` 979 | 980 | Running the python over the input generated: 981 | 982 | ``` 983 | Number of assignment pairs in which one range fully contains the other: 193 984 | ``` 985 | 986 | We parsed `193` as an answer from this output, which was incorrect. 987 | 988 | ### Run 44 989 | 990 | GPT-3 generated: 991 | 992 | ```python 993 | #open input file and read each line, store the lines in the list "data" 994 | has_overlap = 0 #keep track of overlap 995 | for line in open('input.txt'): 996 | line = line.strip() #clean up whitespace 997 | #split data into two sets 998 | ids1, ids2 = line.split(',') 999 | #extract ids, converting them to a list of integers 1000 | ids1= list(map(int, ids1.split('-'))) 1001 | ids2= list(map(int, ids2.split('-'))) 1002 | 1003 | #check if the first set contains the entire second set 1004 | if ids1[0] <= ids2[0] and ids1[1] >= ids2[1]: 1005 | has_overlap += 1 1006 | 1007 | print(f"The answer is {has_overlap} pairs of numbers which overlap") 1008 | ``` 1009 | 1010 | Running the python over the input generated: 1011 | 1012 | ``` 1013 | The answer is 266 pairs of numbers which overlap 1014 | ``` 1015 | 1016 | We parsed `266` as an answer from this output, which was incorrect. 1017 | 1018 | ### Run 41 1019 | 1020 | GPT-3 generated: 1021 | 1022 | ```python 1023 | # Open the file 1024 | with open("input.txt") as f: 1025 | content = f.readlines() 1026 | 1027 | # Strip \n from lines 1028 | content = [x.strip() for x in content] 1029 | 1030 | # Split the pairs 1031 | pairs = [x.split(',') for x in content] 1032 | 1033 | # Initialize the number of fully contained ranges 1034 | fully_contained = 0 1035 | 1036 | # Iterate through all pairs of ranges 1037 | for pair in pairs: 1038 | # Extract min and max values for range 1 1039 | range1_min_max = pair[0].split('-') 1040 | range1_min = int(range1_min_max[0]) 1041 | range1_max = int(range1_min_max[1]) 1042 | 1043 | # Extract min and max values for range 2 1044 | range2_min_max = pair[1].split('-') 1045 | range2_min = int(range2_min_max[0]) 1046 | range2_max = int(range2_min_max[1]) 1047 | 1048 | # Increment the number of fully contained ranges when condition is met 1049 | if (range1_min <= range2_min) and (range2_max <= range1_max): 1050 | fully_contained += 1 1051 | 1052 | # Print the result 1053 | print('Number of fully contained ranges:', fully_contained) 1054 | ``` 1055 | 1056 | Running the python over the input generated: 1057 | 1058 | ``` 1059 | Number of fully contained ranges: 266 1060 | ``` 1061 | 1062 | We parsed `266` as an answer from this output, which was incorrect. 1063 | 1064 | ### Run 49 1065 | 1066 | GPT-3 generated: 1067 | 1068 | ```python 1069 | # open and read the file 1070 | file = open("input.txt", "r") 1071 | lines = file.readlines() 1072 | 1073 | # initialize count of overlapping ranges 1074 | count = 0 1075 | 1076 | # loop through each line 1077 | for l in lines: 1078 | 1079 | # split the line into two ranges 1080 | l1, l2 = l.split(",") 1081 | 1082 | # extract the two ranges 1083 | start1, end1 = [int(x) for x in l1.split("-")] 1084 | start2, end2 = [int(x) for x in l2.split("-")] 1085 | 1086 | # check if they overlap 1087 | if (start1 > start2 and start1 < end2) or (start2 > start1 and start2 < end1): 1088 | 1089 | # if they do, increment count and print which ranges overlapped 1090 | count += 1 1091 | print("Range ", start1, "-", end1, " overlaps with Range ", start2, "-", end2) 1092 | 1093 | print("\n" + "Answer:", count, "pairs of ranges fully contain each other") 1094 | ``` 1095 | 1096 | Running the python over the input generated: 1097 | 1098 | ``` 1099 | Range 94 - 97 overlaps with Range 31 - 95...[truncated] 1100 | ``` 1101 | 1102 | We parsed `['94', '97', '31', '95']` as answers from this output, which was incorrect. 1103 | 1104 | ### Run 53 1105 | 1106 | GPT-3 generated: 1107 | 1108 | ```python 1109 | file = open('input.txt', 'r') 1110 | count = 0 1111 | for pair in file: 1112 | range1 = pair.split(',')[0] 1113 | range2 = pair.split(',')[1] 1114 | r1_min = int(range1.split('-')[0]) 1115 | r1_max = int(range1.split('-')[1]) 1116 | r2_min = int(range2.split('-')[0]) 1117 | r2_max = int(range2.split('-')[1]) 1118 | 1119 | if r1_min <= r2_min and r1_max >= r2_max: 1120 | count += 1 1121 | 1122 | print('Number of assignment pairs where one range fully contains the other: {}'.format(count)) 1123 | ``` 1124 | 1125 | Running the python over the input generated: 1126 | 1127 | ``` 1128 | Number of assignment pairs where one range fully contains the other: 266 1129 | ``` 1130 | 1131 | We parsed `266` as an answer from this output, which was incorrect. 1132 | 1133 | ### Run 50 1134 | 1135 | GPT-3 generated: 1136 | 1137 | ```python 1138 | def parse_input_string(input_string): 1139 | # parse input string into list of tuples containing the range for each pair 1140 | range_list = [] 1141 | input_list = input_string.split(',') 1142 | for string in input_list: 1143 | range_list.append(tuple(int(n) for n in string.split('-'))) 1144 | return range_list 1145 | 1146 | with open('input.txt', 'r') as f: 1147 | overlaps = 0 1148 | input_string = f.readline().rstrip() 1149 | range_list = parse_input_string(input_string) 1150 | # Loop through the range_list and compare ranges 1151 | for i in range(len(range_list) - 1): 1152 | for j in range(i + 1, len(range_list)): 1153 | if range_list[i][1] < range_list[j][0] or range_list[j][1] < range_list[i][0]: 1154 | # ranges do not overlap 1155 | continue 1156 | else: 1157 | overlaps += 1 1158 | 1159 | print(f"Number of overlaps: {overlaps}") 1160 | ``` 1161 | 1162 | Running the python over the input generated: 1163 | 1164 | ``` 1165 | Number of overlaps: 1 1166 | ``` 1167 | 1168 | We parsed `[]` as answers from this output, which was incorrect. 1169 | 1170 | ### Run 52 1171 | 1172 | GPT-3 generated: 1173 | 1174 | ```python 1175 | # Open up the input file, get each line (pair of assignments) into an array 1176 | input_file = open("input.txt", "r") 1177 | pairs = [] 1178 | 1179 | # Parse the input into a 2D array of sections 1180 | for line in input_file: 1181 | assignments = line.split(',') 1182 | first = assignments[0].split('-') 1183 | second = assignments[1].split('-') 1184 | pairs.append([int(first[0]), int(first[1]), int(second[0]), int(second[1])]) 1185 | 1186 | # Count the number of pairs with one assignment fully contained by the other 1187 | # If the starting value of one assignment is greater than the starting value of the other 1188 | # and the ending value of the first is less than or equal to the ending value of the other, 1189 | # then one assignment is fully contained by the other 1190 | 1191 | num_fully_contained = 0 1192 | 1193 | for pair in pairs: 1194 | if pair[0] >= pair[2] and pair[1] <= pair[3]: 1195 | num_fully_contained += 1 1196 | 1197 | # Print the answer 1198 | print("The number of assignment pairs where one range fully contains the other is " + str(num_fully_contained)) 1199 | ``` 1200 | 1201 | Running the python over the input generated: 1202 | 1203 | ``` 1204 | The number of assignment pairs where one range fully contains the other is 322 1205 | ``` 1206 | 1207 | We parsed `322` as an answer from this output, which was incorrect. 1208 | 1209 | ### Run 65 1210 | 1211 | GPT-3 generated: 1212 | 1213 | ```python 1214 | # Open the file and read each line 1215 | with open('input.txt', 'r') as file: 1216 | lines = [line.strip() for line in file] 1217 | 1218 | # Initialize a counter to store the total number of fully contained assignments 1219 | contained_pairs = 0 1220 | 1221 | # For each line in the input, find out if the ranges overlap 1222 | for line in lines: 1223 | # Get the two ranges 1224 | range1, range2 = [rng.split('-') for rng in line.split(',')] 1225 | 1226 | # Extract the min and max of each 1227 | min1, max1 = [int(val) for val in range1] 1228 | min2, max2 = [int(val) for val in range2] 1229 | 1230 | # If range1 is fully contained in range2 then add 1 to the counter 1231 | if min1 >= min2 and max1 <= max2: 1232 | contained_pairs += 1 1233 | 1234 | # Print the answer and the counter of fully contained assignments 1235 | print(f"The answer is {contained_pairs} fully contained pairs") 1236 | ``` 1237 | 1238 | Running the python over the input generated: 1239 | 1240 | ``` 1241 | The answer is 322 fully contained pairs 1242 | ``` 1243 | 1244 | We parsed `322` as an answer from this output, which was incorrect. 1245 | 1246 | ### Run 71 1247 | 1248 | GPT-3 generated: 1249 | 1250 | ```python 1251 | overlaps = 0 1252 | lines = open('input.txt').readlines() 1253 | for line in lines: 1254 | ranges = [tuple(map(int, range.split('-'))) for range in line.split(',')] 1255 | for r1, r2 in zip(ranges, ranges[1:]): 1256 | (s1, e1) = r1 1257 | (s2, e2) = r2 1258 | if (s1 <= s2 and e2 <= e1): 1259 | overlaps += 1 1260 | print(f"Number of assignment pairs where one range fully contains the other: {overlaps}") 1261 | ``` 1262 | 1263 | Running the python over the input generated: 1264 | 1265 | ``` 1266 | Number of assignment pairs where one range fully contains the other: 266 1267 | ``` 1268 | 1269 | We parsed `266` as an answer from this output, which was incorrect. 1270 | ## Error answers 1271 | 1272 | ### Run 9 1273 | 1274 | GPT-3 generated: 1275 | 1276 | ```python 1277 | input_data = [tuple(map(int, row.split('-'))) for row in open('input.txt').read().split('\n') if row] 1278 | 1279 | overlaps = 0 1280 | 1281 | for i in range(len(input_data)): 1282 | for j in range(i+1, len(input_data)): 1283 | first = input_data[i] 1284 | second = input_data[j] 1285 | 1286 | start = max(first[0], second[0]) 1287 | end = min(first[1], second[1]) 1288 | 1289 | if start <= end: 1290 | overlaps += 1 1291 | 1292 | print(f'Number of assignment pairs where one range fully contains the other: {overlaps}') 1293 | ``` 1294 | 1295 | The python generated an error: 1296 | 1297 | ``` 1298 | Traceback (most recent call last): 1299 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_9.py", line 2, in 1300 | input_data = [tuple(map(int, row.split('-'))) for row in open('input.txt').read().split('\n') if row] 1301 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_9.py", line 2, in 1302 | input_data = [tuple(map(int, row.split('-'))) for row in open('input.txt').read().split('\n') if row] 1303 | ValueError: invalid literal for int() with base 10: '97,31' 1304 | ``` 1305 | 1306 | ### Run 6 1307 | 1308 | GPT-3 generated: 1309 | 1310 | ```python 1311 | def parse_pair(text): 1312 | start, end = list(map(int, text.split('-'))) 1313 | return (start, end) 1314 | 1315 | ranges = [parse_pair(line) for line in open('input.txt').read().split()] 1316 | 1317 | fully_contained = 0 1318 | 1319 | for i, v in enumerate(ranges): 1320 | for j, w in enumerate(ranges): 1321 | if i == j: 1322 | continue 1323 | 1324 | if v[0] <= w[0] and w[1] <= v[1]: # v fully contains w 1325 | fully_contained += 1 1326 | print(f'{v} is fully contained by {w}.') 1327 | 1328 | print(f'The answer is {fully_contained}.') 1329 | ``` 1330 | 1331 | The python generated an error: 1332 | 1333 | ``` 1334 | Traceback (most recent call last): 1335 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_6.py", line 6, in 1336 | ranges = [parse_pair(line) for line in open('input.txt').read().split()] 1337 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_6.py", line 6, in 1338 | ranges = [parse_pair(line) for line in open('input.txt').read().split()] 1339 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_6.py", line 3, in parse_pair 1340 | start, end = list(map(int, text.split('-'))) 1341 | ValueError: invalid literal for int() with base 10: '97,31' 1342 | ``` 1343 | 1344 | ### Run 7 1345 | 1346 | GPT-3 generated: 1347 | 1348 | ```python 1349 | # Load file 1350 | with open("input.txt", "r") as in_file: 1351 | content = in_file.readlines() 1352 | 1353 | # Parse ranges from file 1354 | parsed_content = [list(map(int, x.strip().split("-"))) for x in content] 1355 | 1356 | # Find ranges which fully contain another 1357 | count = 0 1358 | for i in range(len(parsed_content) - 1): 1359 | for j in range(i + 1, len(parsed_content)): 1360 | # If the beginning of the first range is within the second range, 1361 | # and the end of the first range is within the second range, 1362 | # then the second range contains the first range 1363 | if parsed_content[i][0] >= parsed_content[j][0] \ 1364 | and parsed_content[i][1] <= parsed_content[j][1]: 1365 | 1366 | count += 1 1367 | 1368 | print("Number of ranges which fully contain another:", count) 1369 | ``` 1370 | 1371 | The python generated an error: 1372 | 1373 | ``` 1374 | Traceback (most recent call last): 1375 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_7.py", line 7, in 1376 | parsed_content = [list(map(int, x.strip().split("-"))) for x in content] 1377 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_7.py", line 7, in 1378 | parsed_content = [list(map(int, x.strip().split("-"))) for x in content] 1379 | ValueError: invalid literal for int() with base 10: '97,31' 1380 | ``` 1381 | 1382 | ### Run 2 1383 | 1384 | GPT-3 generated: 1385 | 1386 | ```python 1387 | def get_bitset(range): 1388 | begin, end = range.split('-') 1389 | begin = int(begin) 1390 | end = int(end) 1391 | bitset = 0 1392 | for i in range(begin, end+1): 1393 | bitset = bitset | (2**i) 1394 | return bitset 1395 | 1396 | with open('input.txt', 'r') as file: 1397 | pairs = file.read().splitlines() 1398 | 1399 | overlaps = 0 1400 | for pair in pairs: 1401 | range1, range2 = pair.split(',') 1402 | bitset1 = get_bitset(range1) 1403 | bitset2 = get_bitset(range2) 1404 | if bitset1 & bitset2: 1405 | overlaps += 1 1406 | 1407 | print('Answer:', overlaps, '(the number of assignment pairs in which one range fully contains the other)') 1408 | ``` 1409 | 1410 | The python generated an error: 1411 | 1412 | ``` 1413 | Traceback (most recent call last): 1414 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_2.py", line 17, in 1415 | bitset1 = get_bitset(range1) 1416 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_2.py", line 7, in get_bitset 1417 | for i in range(begin, end+1): 1418 | TypeError: 'str' object is not callable 1419 | ``` 1420 | 1421 | ### Run 19 1422 | 1423 | GPT-3 generated: 1424 | 1425 | ```python 1426 | # Read the input file and store the sections 1427 | with open('input.txt','r') as f: 1428 | sections = [tuple(sorted(map(int, line.split('-')))) for line in f] 1429 | 1430 | # Count how many sections are contained 1431 | contained = 0 1432 | for s1, s2 in sections: 1433 | if s1[0] <= s2[0] and s1[1] >= s2[1]: 1434 | contained += 1 1435 | 1436 | # Print the answer and the number of sections contained 1437 | print(f'{contained} sections are contained') 1438 | ``` 1439 | 1440 | The python generated an error: 1441 | 1442 | ``` 1443 | Traceback (most recent call last): 1444 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_19.py", line 4, in 1445 | sections = [tuple(sorted(map(int, line.split('-')))) for line in f] 1446 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_19.py", line 4, in 1447 | sections = [tuple(sorted(map(int, line.split('-')))) for line in f] 1448 | ValueError: invalid literal for int() with base 10: '97,31' 1449 | ``` 1450 | 1451 | ### Run 18 1452 | 1453 | GPT-3 generated: 1454 | 1455 | ```python 1456 | with open('input.txt') as f: 1457 | assignment_pairs = [l.strip() for l in f] 1458 | 1459 | # Parse the assignment pairs into a list of tuples of the start and end coordinates of the range 1460 | assignments = [s.split('-') for s in assignment_pairs] 1461 | assignments = [(int(x[0]), int(x[1])) for x in assignments] 1462 | 1463 | # Keep track of how many pairs have one range fully contained within the other 1464 | ans = 0 1465 | 1466 | for idx, assignment in enumerate(assignments): 1467 | for jdx, other_assignment in enumerate(assignments): 1468 | # Make sure we don't check the same assignment against itself 1469 | if jdx == idx: 1470 | continue 1471 | # Check if one range is fully contained within the other 1472 | if assignment[0] <= other_assignment[0] and assignment[1] >= other_assignment[1]: 1473 | ans += 1 1474 | 1475 | print("There are {} assignment pairs where one range fully contains the other.".format(ans)) 1476 | ``` 1477 | 1478 | The python generated an error: 1479 | 1480 | ``` 1481 | Traceback (most recent call last): 1482 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_18.py", line 7, in 1483 | assignments = [(int(x[0]), int(x[1])) for x in assignments] 1484 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_18.py", line 7, in 1485 | assignments = [(int(x[0]), int(x[1])) for x in assignments] 1486 | ValueError: invalid literal for int() with base 10: '97,31' 1487 | ``` 1488 | 1489 | ### Run 29 1490 | 1491 | GPT-3 generated: 1492 | 1493 | ```python 1494 | # Read input from file 1495 | file = open("input.txt", "r") 1496 | sections = [line.strip().split(",") for line in file.readlines()] 1497 | file.close() 1498 | 1499 | # Count overlapping pairs 1500 | overlapping_pairs = 0 1501 | for section in sections: 1502 | start1, end1 = map(int, sections[0].split("-")) 1503 | start2, end2 = map(int, sections[1].split("-")) 1504 | 1505 | if start2 >= start1 and end2 <= end1: 1506 | # Range 2 is contained in range 1 1507 | overlapping_pairs += 1 1508 | elif start1 >= start2 and end1 <= end2: 1509 | # Range 1 is contained in range 2 1510 | overlapping_pairs += 1 1511 | 1512 | # Print answer 1513 | print("Answer: " + str(overlapping_pairs) + " overlapping pairs") 1514 | ``` 1515 | 1516 | The python generated an error: 1517 | 1518 | ``` 1519 | Traceback (most recent call last): 1520 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_29.py", line 10, in 1521 | start1, end1 = map(int, sections[0].split("-")) 1522 | AttributeError: 'list' object has no attribute 'split' 1523 | ``` 1524 | 1525 | ### Run 30 1526 | 1527 | GPT-3 generated: 1528 | 1529 | ```python 1530 | # open the file 1531 | with open('input.txt') as f: 1532 | contents = f.readlines() 1533 | 1534 | # split each line into two parts 1535 | # and convert them to a list of tuples 1536 | sections = [tuple(x.strip().split('-')) for x in contents] 1537 | 1538 | # keep count of the overlapping sections 1539 | overlapping = 0 1540 | 1541 | # for each section in the list 1542 | for i in range(len(sections)-1): 1543 | 1544 | # get the range of the current section 1545 | lower1, upper1 = int(sections[i][0]), int(sections[i][1]) 1546 | 1547 | # loop through the other sections 1548 | for j in range(i+1, len(sections)): 1549 | 1550 | # get the range of the other section 1551 | lower2, upper2 = int(sections[j][0]), int(sections[j][1]) 1552 | 1553 | # if range one is within range two, then it's overlapping 1554 | if lower1 >= lower2 and upper1 <= upper2: 1555 | overlapping += 1 1556 | # check if any other case gives a result 1557 | 1558 | print(f'Number of assignment pairs where one range contains the other: {overlapping}') 1559 | ``` 1560 | 1561 | The python generated an error: 1562 | 1563 | ``` 1564 | Traceback (most recent call last): 1565 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_30.py", line 17, in 1566 | lower1, upper1 = int(sections[i][0]), int(sections[i][1]) 1567 | ValueError: invalid literal for int() with base 10: '97,31' 1568 | ``` 1569 | 1570 | ### Run 25 1571 | 1572 | GPT-3 generated: 1573 | 1574 | ```python 1575 | with open('input.txt') as f: 1576 | range_pairs = f.read().splitlines() 1577 | 1578 | # Print the number of assignment pairs where one range contains the other. 1579 | contained_pairs = 0 1580 | for pair in range_pairs: 1581 | # Get the two numbers from each assignment 1582 | first_num = int(pair[0:pair.index('-')]) 1583 | second_num = int(pair[pair.index('-')+1:len(pair)]) 1584 | 1585 | if first_num > second_num: 1586 | contained_pairs += 1 1587 | 1588 | print(contained_pairs, "(pairs where the smaller number comes first)") 1589 | 1590 | # Output: 4 (pairs where the smaller number comes first) 1591 | ``` 1592 | 1593 | The python generated an error: 1594 | 1595 | ``` 1596 | Traceback (most recent call last): 1597 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_25.py", line 10, in 1598 | second_num = int(pair[pair.index('-')+1:len(pair)]) 1599 | ValueError: invalid literal for int() with base 10: '97,31-95' 1600 | ``` 1601 | 1602 | ### Run 34 1603 | 1604 | GPT-3 generated: 1605 | 1606 | ```python 1607 | #open file with input data 1608 | with open("input.txt") as f: 1609 | counts = 0 1610 | #read file line by line 1611 | for line in f: 1612 | #extract left and right end of the range of position ids 1613 | l, r = line.strip().split(',') 1614 | a, b = map(int, l.split('-')), map(int, r.split('-') ) 1615 | #range comparison 1616 | # if the start of one range is within the other range and the second range is larger than the first, 1617 | if min(a) >= min(b) and max(a) <= max(b): 1618 | counts+=1 1619 | #print supporting info on the same line 1620 | print("Pair: ", l, r) 1621 | 1622 | #print the answer 1623 | print("Number of assignment pairs:", counts) 1624 | ``` 1625 | 1626 | The python generated an error: 1627 | 1628 | ``` 1629 | Traceback (most recent call last): 1630 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_34.py", line 12, in 1631 | if min(a) >= min(b) and max(a) <= max(b): 1632 | ValueError: max() arg is an empty sequence 1633 | ``` 1634 | 1635 | ### Run 39 1636 | 1637 | GPT-3 generated: 1638 | 1639 | ```python 1640 | filenum = 0 1641 | contained = 0 1642 | with open("input.txt", "r") as fp: 1643 | for line in fp: 1644 | # decode the 2 numbers in the line 1645 | i1, i2 = [int(_) for _ in line.split(",")] 1646 | filenum += 1 1647 | 1648 | # determine if i2 contains i1 1649 | if i1 >= i2 and i1 <= i2 + abs(i2 - i1): 1650 | contained += 1 1651 | print ("Section assignment pair %d, the range %d-%d contains the range %d-%d" % (filenum, i1, i1 + abs(i2 - i1), i2, i2 + abs(i2 - i1))) 1652 | 1653 | print ("\nIn %d assignment pairs, one range fully contains the other" % contained) 1654 | ``` 1655 | 1656 | The python generated an error: 1657 | 1658 | ``` 1659 | Traceback (most recent call last): 1660 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_39.py", line 7, in 1661 | i1, i2 = [int(_) for _ in line.split(",")] 1662 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_39.py", line 7, in 1663 | i1, i2 = [int(_) for _ in line.split(",")] 1664 | ValueError: invalid literal for int() with base 10: '94-97' 1665 | ``` 1666 | 1667 | ### Run 36 1668 | 1669 | GPT-3 generated: 1670 | 1671 | ```python 1672 | pairs = [] 1673 | with open('input.txt') as f: 1674 | for line in f: 1675 | pair = [int(num) for num in line.strip().split(',')] 1676 | pairs.append(pair) 1677 | 1678 | # Count pairs with full containment 1679 | num_full = 0 1680 | for pair in pairs: 1681 | start, end = pair[0], pair[1] 1682 | # if start and end are the same 1683 | if start[0] == end[0] and start[1] == end[1]: 1684 | num_full += 1 1685 | continue 1686 | # check containment 1687 | if start[0] <= end[0] and start[1] >= end[1]: 1688 | num_full += 1 1689 | 1690 | print("Number of assignment pairs where one range fully contains the other: %d" % num_full) 1691 | ``` 1692 | 1693 | The python generated an error: 1694 | 1695 | ``` 1696 | Traceback (most recent call last): 1697 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_36.py", line 5, in 1698 | pair = [int(num) for num in line.strip().split(',')] 1699 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_36.py", line 5, in 1700 | pair = [int(num) for num in line.strip().split(',')] 1701 | ValueError: invalid literal for int() with base 10: '94-97' 1702 | ``` 1703 | 1704 | ### Run 46 1705 | 1706 | GPT-3 generated: 1707 | 1708 | ```python 1709 | def parse_input_file(file_name): 1710 | section_assignments = [] 1711 | with open(file_name) as input_file: 1712 | content = input_file.readlines() 1713 | content = [x.strip() for x in content] 1714 | for pair in content: 1715 | pair_range = list(map(int, pair.split('-'))) 1716 | section_assignments.append(pair_range) 1717 | return section_assignments 1718 | 1719 | def count_fully_contained_assignment_pairs(section_assignments): 1720 | fully_contained_pairs_count = 0 1721 | for i in range(len(section_assignments)): 1722 | for j in range(i+1, len(section_assignments)): 1723 | pair1 = section_assignments[i] 1724 | pair2 = section_assignments[j] 1725 | if (pair1[0] <= pair2[0]) and (pair2[1] <= pair1[1]): 1726 | fully_contained_pairs_count += 1 1727 | elif (pair2[0] <= pair1[0]) and (pair1[1] <= pair2[1]): 1728 | fully_contained_pairs_count += 1 1729 | return fully_contained_pairs_count 1730 | 1731 | assignment_pairs = parse_input_file('input.txt') 1732 | 1733 | answer = count_fully_contained_assignment_pairs(assignment_pairs) 1734 | 1735 | print("The answer is %d." % answer) 1736 | ``` 1737 | 1738 | The python generated an error: 1739 | 1740 | ``` 1741 | Traceback (most recent call last): 1742 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_46.py", line 24, in 1743 | assignment_pairs = parse_input_file('input.txt') 1744 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_46.py", line 8, in parse_input_file 1745 | pair_range = list(map(int, pair.split('-'))) 1746 | ValueError: invalid literal for int() with base 10: '97,31' 1747 | ``` 1748 | 1749 | ### Run 45 1750 | 1751 | GPT-3 generated: 1752 | 1753 | ```python 1754 | def get_range_info(): 1755 | with open("input.txt") as file: 1756 | return [line.split(",") for line in file.read().split("\n")] 1757 | 1758 | def count_overlapping_ranges(range_info): 1759 | count = 0 1760 | for ranges in range_info: 1761 | left_start, left_end = get_range_edges(ranges[0]) 1762 | right_start, right_end = get_range_edges(ranges[1]) 1763 | if left_start <= right_start and right_end <= left_end: 1764 | count += 1 1765 | return count 1766 | 1767 | def get_range_edges(range_str): 1768 | return [int(edge) for edge in range_str.split("-")] 1769 | 1770 | range_info = get_range_info() 1771 | print("Answer:", count_overlapping_ranges(range_info)) 1772 | ``` 1773 | 1774 | The python generated an error: 1775 | 1776 | ``` 1777 | Traceback (most recent call last): 1778 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_45.py", line 19, in 1779 | print("Answer:", count_overlapping_ranges(range_info)) 1780 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_45.py", line 9, in count_overlapping_ranges 1781 | left_start, left_end = get_range_edges(ranges[0]) 1782 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_45.py", line 16, in get_range_edges 1783 | return [int(edge) for edge in range_str.split("-")] 1784 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_45.py", line 16, in 1785 | return [int(edge) for edge in range_str.split("-")] 1786 | ValueError: invalid literal for int() with base 10: '' 1787 | ``` 1788 | 1789 | ### Run 51 1790 | 1791 | GPT-3 generated: 1792 | 1793 | ```python 1794 | # Read input into a list 1795 | section_ranges = [] 1796 | with open('input.txt') as f: 1797 | for line in f: 1798 | range_list = line.strip().split(',') 1799 | for range in range_list: 1800 | s, e = [int(x) for x in range.split('-')] 1801 | section_ranges.append((s, e)) 1802 | 1803 | # Find overlapping pairs and save them in a list 1804 | overlapping_pairs = [] 1805 | for i in range(0, len(section_ranges)-1): 1806 | for j in range(i+1, len(section_ranges)): 1807 | s1, e1 = section_ranges[i] 1808 | s2, e2 = section_ranges[j] 1809 | if s1 > s2 and e1 <= e2: 1810 | overlapping_pairs.append(section_ranges[i]) 1811 | elif s2 > s1 and e2 <= e1: 1812 | overlapping_pairs.append(section_ranges[j]) 1813 | 1814 | # Print the answer 1815 | answer = len(overlapping_pairs) 1816 | print('The answer is {} overlapping pairs.'.format(answer)) 1817 | ``` 1818 | 1819 | The python generated an error: 1820 | 1821 | ``` 1822 | Traceback (most recent call last): 1823 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_51.py", line 13, in 1824 | for i in range(0, len(section_ranges)-1): 1825 | TypeError: 'str' object is not callable 1826 | ``` 1827 | 1828 | ### Run 57 1829 | 1830 | GPT-3 generated: 1831 | 1832 | ```python 1833 | # parse input into a list of tuples 1834 | pairs = [tuple(map(int, line.split('-'))) for line in open('input.txt', 'r')] 1835 | 1836 | # sort the tuples by lower bounds 1837 | sorted_pairs = sorted(pairs, key=lambda x: x[0]) 1838 | 1839 | # an overlap is found when the lower bound of a pair is inside the range of 1840 | # the previously seen pair. Calculate overlaps and print the answer. 1841 | overlaps = 0 1842 | for start, end in sorted_pairs: 1843 | if start > last_end: 1844 | last_end = end 1845 | else: 1846 | overlaps += 1 1847 | last_end = max(end, last_end) 1848 | 1849 | print('Number of assignment pairs which overlap fully: {}'.format(overlaps)) 1850 | ``` 1851 | 1852 | The python generated an error: 1853 | 1854 | ``` 1855 | Traceback (most recent call last): 1856 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_57.py", line 3, in 1857 | pairs = [tuple(map(int, line.split('-'))) for line in open('input.txt', 'r')] 1858 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_57.py", line 3, in 1859 | pairs = [tuple(map(int, line.split('-'))) for line in open('input.txt', 'r')] 1860 | ValueError: invalid literal for int() with base 10: '97,31' 1861 | ``` 1862 | 1863 | ### Run 55 1864 | 1865 | GPT-3 generated: 1866 | 1867 | ```python 1868 | file = open("input.txt", "r") 1869 | 1870 | # Create a list of all sections from the input text 1871 | sections = [] 1872 | for line in file: 1873 | line = line.strip('\n') 1874 | for range in line.split(','): 1875 | start, end = range.split('-') 1876 | for i in range(int(start), int(end) + 1): 1877 | sections.append(i) 1878 | 1879 | # Count the occurrences of each section in the list 1880 | counts = {} 1881 | for i in set(sections): 1882 | counts[i] = sections.count(i) 1883 | 1884 | # Calculate the number of assignment pairs where one range fully contains the other 1885 | pairs = 0 1886 | for count in counts.values(): 1887 | if count > 1: 1888 | pairs += count * (count - 1) // 2 1889 | 1890 | # Print the answer 1891 | print("*2* pairs where one range fully contains the other: " + str(pairs)) 1892 | ``` 1893 | 1894 | The python generated an error: 1895 | 1896 | ``` 1897 | Traceback (most recent call last): 1898 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_55.py", line 10, in 1899 | for i in range(int(start), int(end) + 1): 1900 | TypeError: 'str' object is not callable 1901 | ``` 1902 | 1903 | ### Run 66 1904 | 1905 | GPT-3 generated: 1906 | 1907 | ```python 1908 | #read the file as a list of strings, where each line is converted to an element of the list 1909 | f = open('input.txt') 1910 | input_list = f.read().splitlines() 1911 | 1912 | #splits the elements of the input_list based on the hyphen, converts each ID number to an integer, and 1913 | # inserts them into a list of tuples 1914 | tuple_list = [] 1915 | for line in input_list: 1916 | line_values = line.split('-') 1917 | tup = (int(line_values[0]), int(line_values[1])) 1918 | tuple_list.append(tup) 1919 | 1920 | #checks which of the tuples has the smaller value as the start of a range and the larger value as the end 1921 | # as the second number should greater if the range is completely contained 1922 | overlap_counter = 0 1923 | for tup in tuple_list: 1924 | if tup[0] < tup[1]: 1925 | overlap_counter += 1 1926 | 1927 | print("The number of assignment pairs where one range fully contains the other is: ", overlap_counter) 1928 | 1929 | #this function prints: The number of assignment pairs where one range fully contains the other is: 2 1930 | ``` 1931 | 1932 | The python generated an error: 1933 | 1934 | ``` 1935 | Traceback (most recent call last): 1936 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_66.py", line 11, in 1937 | tup = (int(line_values[0]), int(line_values[1])) 1938 | ValueError: invalid literal for int() with base 10: '97,31' 1939 | ``` 1940 | 1941 | ### Run 60 1942 | 1943 | GPT-3 generated: 1944 | 1945 | ```python 1946 | def parseInput(input_data): 1947 | #take input text data in the form of pairs of section IDs and turn into a list of tuples of start and end ids 1948 | pairs = input_data.split('\n') 1949 | section_ids = [] 1950 | 1951 | #parse and store pairs 1952 | for pair in pairs: 1953 | ids = pair.split(',') 1954 | section_ids.append((ids[0], ids[1])) 1955 | 1956 | return section_ids 1957 | 1958 | def countFullyContainedPairs(section_ids): 1959 | #go through each id pair and check if one fully contains the other 1960 | counter = 0 1961 | for section_id in section_ids: 1962 | (start1, end1) = (int(section_id[0][0]), int(section_id[0][2])) 1963 | (start2, end2) = (int(section_id[1][0]), int(section_id[1][2])) 1964 | #check if one id range fully contains the other 1965 | if (start1 <= start2 and end1 >= end2): 1966 | counter += 1 1967 | elif (start2 <= start1 and end2 >= end1): 1968 | counter += 1 1969 | 1970 | return counter 1971 | 1972 | #open the input file 1973 | input_file = open('input.txt') 1974 | input_data = input_file.read() 1975 | 1976 | #parse the input and count the number of pairs which one fully contains the other 1977 | section_ids = parseInput(input_data) 1978 | fully_contained_pairs = countFullyContainedPairs(section_ids) 1979 | 1980 | #print the answer 1981 | print("Answer:", fully_contained_pairs, "(Number of assignment pairs where one range fully contains the other)") 1982 | ``` 1983 | 1984 | The python generated an error: 1985 | 1986 | ``` 1987 | Traceback (most recent call last): 1988 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_60.py", line 33, in 1989 | section_ids = parseInput(input_data) 1990 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_60.py", line 10, in parseInput 1991 | section_ids.append((ids[0], ids[1])) 1992 | IndexError: list index out of range 1993 | ``` 1994 | 1995 | ### Run 70 1996 | 1997 | GPT-3 generated: 1998 | 1999 | ```python 2000 | # Read each line of the input 2001 | f = open("input.txt") 2002 | pairs = f.readlines() 2003 | 2004 | # Initialize a variable to hold the overlaps 2005 | overlaps = 0 2006 | 2007 | # For each pair, check if one range fully contains the other 2008 | for pair in pairs: 2009 | # Assign the two elements of the pair to x and y 2010 | x_start, x_end, y_start, y_end = tuple(pair.rstrip().split(',')) 2011 | 2012 | # Convert the values to integers 2013 | x_start, x_end, y_start, y_end = int(x_start), int(x_end), int(y_start), int(y_end) 2014 | 2015 | # Compare the start and end of rach range 2016 | # If one range fully contains the other, increment overlaps 2017 | if (x_start <= y_start and y_end <= x_end) or (y_start <= x_start and x_end <= y_end): 2018 | overlaps += 1 2019 | 2020 | # Print the result 2021 | print("Answer:", overlaps, " overaps") 2022 | ``` 2023 | 2024 | The python generated an error: 2025 | 2026 | ``` 2027 | Traceback (most recent call last): 2028 | File "/Users/maximilian/workspace/aoc-2022/2022/04/llm_responses/part_1_70.py", line 12, in 2029 | x_start, x_end, y_start, y_end = tuple(pair.rstrip().split(',')) 2030 | ValueError: not enough values to unpack (expected 4, got 2) 2031 | ``` 2032 | -------------------------------------------------------------------------------- /openai.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import logging 4 | import os 5 | import re 6 | import shlex 7 | import subprocess 8 | import textwrap 9 | import datetime 10 | from concurrent import futures 11 | from contextlib import redirect_stdout 12 | from functools import partial 13 | from pathlib import Path 14 | from typing import Counter, Iterable 15 | 16 | import click 17 | 18 | logger = logging.getLogger(__name__) 19 | 20 | logging.basicConfig() 21 | 22 | LLM_COMMAND_PREFIX = ( 23 | f"openai api completions.create --engine=text-davinci-003 --max-tokens=2000 -p " 24 | ) 25 | 26 | RESPONSES_PATH = "llm_responses" 27 | REQUESTS_PATH = "llm_requests" 28 | OUTPUTS_PATH = "python_outputs" 29 | PUZZLE_FILE = "puzzle.md" 30 | 31 | THRESHOLD = 2 32 | 33 | 34 | def year_day_path(year: int, day: int) -> Path: 35 | return Path(f"{year}-{day}") 36 | 37 | 38 | def write_instructions(day: int, year: int) -> None: 39 | subprocess.run( 40 | f"aoc download --overwrite --input-file=input.txt --day={day} --year={year}", 41 | shell=True, 42 | capture_output=True, 43 | check=True, 44 | text=True, 45 | ).stdout 46 | subprocess.run( 47 | f"aoc read --overwrite --puzzle-file=puzzle.md --day={day} --year={year}", 48 | shell=True, 49 | capture_output=True, 50 | check=True, 51 | text=True, 52 | ).stdout 53 | 54 | 55 | def read_instructions(day: int) -> str: 56 | return Path(PUZZLE_FILE).read_text() 57 | 58 | 59 | def extract_part_1(text): 60 | text = re.sub(".*--- Day.*", "", text) 61 | text = re.sub(".*-----", "", text) 62 | text = re.sub("\\--- Part", "--- Part", text) 63 | text = text.replace("\n\n\n", "\n") 64 | text, _ = re.subn("Your puzzle answer was.*", "", text) 65 | return text.split(r"\--- Part Two ---")[0] 66 | 67 | 68 | def extract_part_2(text): 69 | text = re.sub(".*--- Day.*", "", text) 70 | text = re.sub(".*-----", "", text) 71 | text, _ = re.subn("Your puzzle answer was.*", "", text) 72 | # awkward way of transforming ` \--- Part Two ---` into `--- Part Two ---` 73 | text = re.sub(r"(.*)\\(--- Part)", r"\1\2", text) 74 | 75 | text = text.split("If you still want to see it,")[0] 76 | text = text.split("Both parts of this puzzle are complete")[0].strip() 77 | 78 | # There's no Part One, only a Part Two, so we add in that to make it clearer 79 | # 80 | # TODO: weirdly this once caused it to do much worse. Need to test that; it's 81 | # very possible it's something else that caused it. 82 | return f"--- Part One ---\n\n{text}" 83 | 84 | 85 | def amend_instructions(text, part): 86 | 87 | # Telling it it's an AoC problem seems to help slightly (I got that advice from 88 | # GPT-Chat, no joke) 89 | # 90 | # Adding "difficult" seemed to help at one point, but doesn't seem to any longer 91 | llm_prefix = ( 92 | "Here's an Advent of Code puzzle, which is a cryptic puzzle told through " 93 | "an imaginary story. You need to write code which solves it. The description " 94 | "includes an example of solving a simplified version of the puzzle.\n\n-----\n" 95 | ) 96 | # "with any relevant details on the same line" seems to help (without saying the 97 | # same line, it'll do things like print every value as it's looping). 98 | llm_instructions = ( 99 | f"Now write python3 code which prints the correct answer" 100 | f"{'' if part == 1 else ' to Part Two'}. " 101 | "Print any information which supports your answer on the same line as the answer. " 102 | "The input is in a file `input.txt`. " 103 | "\n\n" 104 | "```python" 105 | ) 106 | # I also tried prompts like these, but they didn't seem to work as well: 107 | # 108 | # This is a difficult problem! Think about it carefully, step-by-step. If you're not \ 109 | # confident, have the python print "I'm not confident" instead. 110 | # llm_instructions = f""" 111 | # Write python3 code which prints the correct answer{'' if part == 1 else ' to Part Two'}, \ 112 | # as a sentence. Include any other useful numbers in the sentence. The input is in a 113 | # file `input.txt`. 114 | # 115 | # It might be more difficult than it looks, so go step-by-step. 116 | 117 | # ```python 118 | # """ 119 | 120 | text = f"{llm_prefix}{textwrap.indent(text, ' ')}\n\n-----\n\n{llm_instructions}" 121 | (Path(REQUESTS_PATH) / f"part-{part}.txt").write_text(text) 122 | return shlex.quote(text) 123 | 124 | 125 | def do_part(n: int, day: int, part: int) -> str | None: 126 | # TODO: this part can be split up; doesn't need to be done by each process (but it's 127 | # fast so not a big impact) 128 | instructions = read_instructions(day) 129 | if part == 1: 130 | instructions = amend_instructions(extract_part_1(instructions), part=part) 131 | elif part == 2: 132 | instructions = amend_instructions(extract_part_2(instructions), part=part) 133 | else: 134 | raise ValueError 135 | 136 | try: 137 | llm_response = request_python_from_llm(instructions) 138 | except Exception as e: 139 | print(f"Run {n} didn't generate a python excerpt") 140 | logger.info(f"Run {n} didn't generate a python excerpt") 141 | (Path(RESPONSES_PATH) / f"part_{part}_{n}.error").write_text(str(e)) 142 | return None 143 | (Path(RESPONSES_PATH) / f"part_{part}_{n}.py").write_text(llm_response) 144 | try: 145 | # Run the code, writing the output to a file 146 | f = Path(OUTPUTS_PATH) / f"part_{part}_{n}.txt" 147 | with redirect_stdout(f.open("w+")): 148 | exec(llm_response) 149 | return f.read_text() 150 | except Exception as e: 151 | print(f"Run {n} failed with `{e}`") 152 | logger.info(f"Run {n} failed with `{e}`") 153 | return None 154 | 155 | 156 | def request_python_from_llm(instructions) -> str: 157 | llm_command = f"{LLM_COMMAND_PREFIX} {instructions}" 158 | 159 | llm_respose = subprocess.run( 160 | llm_command, 161 | shell=True, 162 | capture_output=True, 163 | text=True, 164 | ) 165 | if llm_respose.returncode == 0: 166 | try: 167 | return llm_respose.stdout.split("```python")[1].split("```")[0] 168 | except Exception: 169 | raise ValueError( 170 | f"Couldn't parse python from LLM: {llm_respose.stdout}, {llm_respose.stderr}" 171 | ) 172 | else: 173 | raise ValueError( 174 | f"Error code from openai: {llm_respose.stdout}, {llm_respose.stderr}" 175 | ) 176 | 177 | 178 | def parse_answer(text) -> Iterable[str]: 179 | """ 180 | Returns an iterable of the numbers from the first line 181 | """ 182 | if not text: 183 | return [] 184 | # Only grab the first line. Some scripts will write lots, but most only 185 | # write one (we ask it to only write one). 186 | line = text.splitlines()[0] 187 | numbers = re.search(r"(\d+)", line) 188 | if numbers and len(numbers.groups()) < 10: 189 | # Sometimes there are multiple numbers; include all of them as long as there 190 | # aren't like 10 191 | answers = numbers.groups() 192 | # Also filter out `0` and `1` as they're probably mistakes 193 | for answer in answers: 194 | if answer not in ("0", "1"): 195 | yield answer 196 | else: 197 | return [] 198 | 199 | 200 | def read_results(part: int) -> Counter: 201 | c: Counter = Counter() 202 | for n in range(1, 100): 203 | f = Path(OUTPUTS_PATH) / f"part_{part}_{n}.txt" 204 | if not f.exists(): 205 | continue 206 | answer = parse_answer(f.read_text()) 207 | if answer: 208 | c.update(answer) 209 | 210 | return c 211 | 212 | 213 | def submit_result(day, year, part, answer): 214 | logger.info(f"Submitting {answer} for part {part}") 215 | return subprocess.run( 216 | f"aoc submit --day={day} --year={year} {part} {answer}", 217 | shell=True, 218 | capture_output=True, 219 | check=True, 220 | text=True, 221 | ).stdout 222 | 223 | 224 | def run_parallel( 225 | day: int, 226 | year: int, 227 | part: int, 228 | stop_when_submitted: bool, 229 | n_workers: int, 230 | runs: int, 231 | threshold: int = THRESHOLD, 232 | ): 233 | 234 | print(f"Starting {n_workers} workers") 235 | c: Counter = Counter() 236 | submitted = False 237 | func = partial(do_part, day=day, part=part) 238 | with futures.ProcessPoolExecutor(max_workers=n_workers) as executor: 239 | 240 | fs = [] 241 | 242 | # If we can't do it in 200 runs, we're probably not going to do it at all 243 | for n in range(runs): 244 | fs.append(executor.submit(func, n)) 245 | 246 | for f in futures.as_completed(fs): 247 | result = f.result() 248 | logger.debug(f"Got result {result}") 249 | answer = parse_answer(result) 250 | if answer: 251 | c.update(answer) 252 | if submitted: 253 | print( 254 | f"Already submitted but continuing collecting results. Current counts: {c}" 255 | ) 256 | continue 257 | 258 | # Only submit if the top answer is 2 or more above the next answer 259 | top_results = c.most_common(2) 260 | if len(top_results) >= 1: 261 | top = top_results[0][1] 262 | # Only one result, so use zero for next 263 | next = top_results[1][1] if len(top_results) == 2 else 0 264 | if top - next >= threshold: 265 | print(f"Submitting {top_results[0][0]}. Counts were {c}") 266 | out = submit_result(day, year, part, c.most_common(1)[0][0]) 267 | print(out) 268 | submitted = True 269 | if stop_when_submitted: 270 | print(f"Now stopping on part {part}. Final results: {c}") 271 | # We can't seem to cancel everything, so this hangs until all 272 | # the running futures are complete. That's annoying when we want 273 | # to start part 2 after submitting part 1. Looks not that easy 274 | # to do 275 | # https://stackoverflow.com/questions/29177490/how-do-you-kill-futures-once-they-have-started 276 | executor.shutdown(wait=False, cancel_futures=True) 277 | return 278 | print(f"No answer hit the threshold, not submitting. Current counts: {c}") 279 | print(f"Reached all attempts without success, stopping. Final results: {c}") 280 | return 281 | 282 | 283 | #def get_year(): 284 | # return int( 285 | # subprocess.run("date +%Y", shell=True, capture_output=True, text=True).stdout 286 | # ) 287 | 288 | def get_year(): 289 | return datetime.datetime.now().year 290 | 291 | 292 | @click.command() 293 | @click.option("--part", type=int) 294 | @click.option("--day", type=int, required=True) 295 | @click.option("--year", type=int, required=False, default=get_year()) 296 | @click.option("--n-workers", type=int, default=1) 297 | @click.option("--runs", type=int, required=False, default=200) 298 | @click.option("--stop-when-submitted", is_flag=True) 299 | def run( 300 | day: int, year: int, part: int, n_workers: int, runs: int, stop_when_submitted: bool 301 | ) -> None: 302 | 303 | print(f"Running {part=}, {day=}, {year=}") 304 | 305 | year_day_path(year, day).mkdir(exist_ok=True) 306 | (year_day_path(year, day) / RESPONSES_PATH).mkdir(exist_ok=True) 307 | (year_day_path(year, day) / OUTPUTS_PATH).mkdir(exist_ok=True) 308 | (year_day_path(year, day) / REQUESTS_PATH).mkdir(exist_ok=True) 309 | # Slightly hacky way to make the day path local; which for executing the python is 310 | # important, becasue we don't want to confuse the AI by giving it a more complicated 311 | # path than `input.txt`. 312 | os.chdir(year_day_path(year, day)) 313 | 314 | write_instructions(day, year) 315 | 316 | if part is None: 317 | run_parallel(day, year, 1, stop_when_submitted, n_workers, runs) 318 | run_parallel(day, year, 2, stop_when_submitted, n_workers, runs) 319 | else: 320 | if n_workers == 1: 321 | do_part(0, day, part) 322 | else: 323 | run_parallel(day, year, part, stop_when_submitted, n_workers, runs) 324 | 325 | 326 | if __name__ == "__main__": 327 | logging.basicConfig(level=logging.INFO, force=True) 328 | run() 329 | --------------------------------------------------------------------------------