Last active
December 4, 2022 09:37
-
-
Save ivmos/969c87f974a1b995f7ad484eaa805ba8 to your computer and use it in GitHub Desktop.
AOC2022 Day04 implemented with ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoc.day01; | |
import aoc.Day; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Day04 implements Day { | |
@Override | |
public String part1(List<String> input) { | |
String s = String.join("\n", input.toArray(new String[0])); | |
return String.valueOf(GptSolution04.countFullyContainedPairs(s)); | |
} | |
@Override | |
public String part2(List<String> input) { | |
String s = String.join("\n", input.toArray(new String[0])); | |
return String.valueOf(GptSolution04Part2.countOverlappingPairs(s)); | |
} | |
} | |
class GptSolution04 { | |
public static void main(String[] args) { | |
String input = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8"; | |
int count = countFullyContainedPairs(input); | |
System.out.println(count); // 2 | |
} | |
// Parses the input and returns a list of Section objects | |
public static List<Section> parseInput(String input) { | |
List<Section> sections = new ArrayList<>(); | |
String[] lines = input.split("\n"); | |
System.out.println(input); | |
for (String line : lines) { | |
String[] parts = line.split(","); | |
for (String part : parts) { | |
String[] range = part.split("-"); | |
int start = Integer.parseInt(range[0]); | |
int end = Integer.parseInt(range[1]); | |
sections.add(new Section(start, end)); | |
} | |
} | |
System.out.println(sections); | |
return sections; | |
} | |
// Counts the number of pairs of sections where one fully contains the other | |
public static int countFullyContainedPairs(String input) { | |
List<Section> sections = parseInput(input); | |
int count = 0; | |
for (int i = 0; i < sections.size() - 1; i+=2) { | |
// BUG fixed manually, shame on you GPT! | |
//for (int i = 0; i < sections.size() - 1; i++) { | |
//for (int j = i + 1; j < sections.size(); j++) { | |
Section a = sections.get(i); | |
Section b = sections.get(i + 1); | |
if (a.start <= b.start && a.end >= b.end) { | |
System.out.println(a); | |
count++; | |
} else if (b.start <= a.start && b.end >= a.end) { | |
System.out.println(b); | |
count++; | |
} | |
//} | |
} | |
return count; | |
} | |
} | |
// Represents a range of section IDs | |
class Section { | |
public int start; | |
public int end; | |
public Section(int start, int end) { | |
this.start = start; | |
this.end = end; | |
} | |
public String toString() { | |
return "[" + start + "," + end + "]"; | |
} | |
} | |
class GptSolution04Part2 { | |
public static void main(String[] args) { | |
String input = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8"; | |
int count = countOverlappingPairs(input); | |
System.out.println(count); // 4 | |
} | |
// Parses the input and returns a list of Section objects | |
public static List<Section> parseInput(String input) { | |
List<Section> sections = new ArrayList<>(); | |
String[] lines = input.split("\n"); | |
for (String line : lines) { | |
String[] parts = line.split(","); | |
for (String part : parts) { | |
String[] range = part.split("-"); | |
int start = Integer.parseInt(range[0]); | |
int end = Integer.parseInt(range[1]); | |
sections.add(new Section(start, end)); | |
} | |
} | |
return sections; | |
} | |
// Counts the number of pairs of sections that overlap | |
public static int countOverlappingPairs(String input) { | |
List<Section> sections = parseInput(input); | |
int count = 0; | |
for (int i = 0; i < sections.size() - 1; i+=2) { | |
//BUG fixed manually, shame on you GPT! | |
//for (int i = 0; i < sections.size(); i++) { | |
//for (int j = i + 1; j < sections.size(); j++) { | |
Section a = sections.get(i); | |
Section b = sections.get(i+1); | |
if (a.start <= b.end && a.end >= b.start) { | |
count++; | |
} | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
![Captura de pantalla de 2022-12-04 09-44-23](https://user-imag
es.githubusercontent.com/251096/205481855-fe959b19-d8e6-489f-bd4a-7b7305b90455.png)