From a30eef69c6862d9e6267c32a55d774a41dadf4b1 Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Thu, 12 Dec 2024 15:36:25 +0000 Subject: [PATCH] Day 5 part 1 initialization code Signed-off-by: Eoghan Conlon --- src/Day05.java | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 10 +++++- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/Day05.java diff --git a/src/Day05.java b/src/Day05.java new file mode 100644 index 0000000..8120894 --- /dev/null +++ b/src/Day05.java @@ -0,0 +1,84 @@ +import java.io.IOException; +import java.util.ArrayList; + +public class Day05 extends Input{ + private int[][] sample_rules; + private ArrayList sample_pages; + + private int[][] rules; + private ArrayList pages; + + + public Day05() throws IOException { + // Initialise the input object + Input input = new Input(); + input.init("in/Day05"); + + //Extract the 2 parts of the input + ArrayList sample_rules_str = new ArrayList<>(); + ArrayList sample_pages_str = new ArrayList<>(); + boolean rules = true; + for(String s : input.getSample_input()){ + if(s.isEmpty()){ + rules = false; + } else { + if(rules){ + sample_rules_str.add(s); + } else { + sample_pages_str.add(s); + } + } + } + + ArrayList rules_str = new ArrayList<>(); + ArrayList pages_str = new ArrayList<>(); + rules = true; + for(String s : input.getSample_input()){ + if(s.isEmpty()){ + rules = false; + } else { + if(rules){ + rules_str.add(s); + } else { + pages_str.add(s); + } + } + } + + // Make the rules easier to parse + this.sample_rules = new int[sample_rules_str.size()][2]; + for (int i = 0; i < sample_rules_str.size(); i++) { + String[] split = sample_rules_str.get(i).split("\\|"); + int[] temp_rule = {Integer.parseInt(split[0]), Integer.parseInt(split[1])}; + this.sample_rules[i] = temp_rule; + } + + this.rules = new int[rules_str.size()][2]; + for (int i = 0; i < rules_str.size(); i++) { + String[] split = rules_str.get(i).split("\\|"); + int[] temp_rule = {Integer.parseInt(split[0]), Integer.parseInt(split[1])}; + this.rules[i] = temp_rule; + } + + // Make the pages easier to parse + this.sample_pages = new ArrayList<>(); + for(String s : sample_pages_str){ + String[] split = s.split(","); + int[] pages = new int[split.length]; + for (int i = 0; i < split.length; i++) { + pages[i] = Integer.parseInt(split[i]); + } + this.sample_pages.add(pages); + } + + this.pages = new ArrayList<>(); + for(String s : pages_str){ + String[] split = s.split(","); + int[] pages = new int[split.length]; + for (int i = 0; i < split.length; i++) { + pages[i] = Integer.parseInt(split[i]); + } + this.pages.add(pages); + } + } +} diff --git a/src/Main.java b/src/Main.java index 1d8c4fb..b6578a5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -82,6 +82,14 @@ public class Main { System.out.printf("Day 3 part 1 (sample): %d (%dms)\n", day03_p1_sample, day03_p1_sample_t); System.out.printf("Day 3 part 1 : %d (%dms)\n", day03_p1, day03_p1_t); System.out.printf("Day 3 part 2 (sample): %d (%dms)\n", day03_p2_sample, day03_p2_sample_t); - System.out.printf("Day 3 part 2: %d (%dms)", day03_p2, day03_p2_t); + System.out.printf("Day 3 part 2: %d (%dms)\n", day03_p2, day03_p2_t); + + System.out.println("\nDay 5:"); + + start = System.currentTimeMillis(); + Day05 day05 = new Day05(); + long day05_init = System.currentTimeMillis() - start; + + System.out.printf("Day 5 init took %dms", day05_init); } }