summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2023-12-06 12:28:22 +0000
committerNat Lasseter <user@4574.co.uk>2023-12-06 12:28:22 +0000
commit69ba3abc1f60b8756f68fac570ecda31a2086325 (patch)
treee53ec764955442e06ccf6b8102d5627366c850e5
parent056c290b1d0302d2396a99a80671932fb8368adc (diff)
Day 06
-rw-r--r--day06/day06.java59
-rw-r--r--day06/input2
-rw-r--r--day06/test2
3 files changed, 63 insertions, 0 deletions
diff --git a/day06/day06.java b/day06/day06.java
new file mode 100644
index 0000000..4965501
--- /dev/null
+++ b/day06/day06.java
@@ -0,0 +1,59 @@
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+public class day06 {
+ public static void main(String args[]) throws IOException {
+ RandomAccessFile input = new RandomAccessFile("input", "r");
+ System.out.println("Day 06 Part 1: " + part1(input));
+ input.seek(0);
+ System.out.println("Day 06 Part 2: " + part2(input));
+ input.close();
+ }
+
+ public static String part1(RandomAccessFile input) throws IOException {
+ ArrayList<Integer[]> races = new ArrayList<>();
+ Pattern digits = Pattern.compile("\\d+");
+ Matcher times = digits.matcher(input.readLine());
+ Matcher distances = digits.matcher(input.readLine());
+
+ while (times.find()) {
+ distances.find();
+ races.add(new Integer[]{
+ Integer.parseInt(times.group()),
+ Integer.parseInt(distances.group())
+ });
+ }
+
+ int margin = 1;
+ for (Integer[] race: races) {
+ int wins = 0;
+ for (int hold = 1; hold < race[0]; hold++) {
+ int dist = (race[0] - hold) * hold;
+ if (dist > race[1]) {
+ wins += 1;
+ }
+ }
+ margin *= wins;
+ }
+
+ return Integer.toString(margin);
+ }
+
+ public static String part2(RandomAccessFile input) throws IOException {
+ long time = Long.parseLong(input.readLine().replaceAll(" ", "").substring(5));
+ long distance = Long.parseLong(input.readLine().replaceAll(" ", "").substring(9));
+
+ int wins = 0;
+ for (long hold = 1; hold < time; hold++) {
+ long dist = (time - hold) * hold;
+ if (dist > distance) {
+ wins += 1;
+ }
+ }
+
+ return Integer.toString(wins);
+ }
+}
diff --git a/day06/input b/day06/input
new file mode 100644
index 0000000..7c06e07
--- /dev/null
+++ b/day06/input
@@ -0,0 +1,2 @@
+Time: 59 68 82 74
+Distance: 543 1020 1664 1022
diff --git a/day06/test b/day06/test
new file mode 100644
index 0000000..28f5ae9
--- /dev/null
+++ b/day06/test
@@ -0,0 +1,2 @@
+Time: 7 15 30
+Distance: 9 40 200