This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDay4.java
More file actions
42 lines (37 loc) · 1.36 KB
/
Day4.java
File metadata and controls
42 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.togetherjava.adventofcode;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Day4 {
public static boolean isValidNumberPart1(int number) {
int[] digits = String.valueOf(number).chars().map(n -> n - 48).toArray();
int lastDigit = digits[0];
boolean hasConsecutiveDigits = false;
for(int i = 1; i < digits.length; i++) {
int digit = digits[i];
if (digit < lastDigit) {
return false;
}
lastDigit = digit;
if (digit == digits[i - 1]) {
hasConsecutiveDigits = true;
}
}
return hasConsecutiveDigits;
}
/**
* Written by Discord user and bae @Kemikals#3177
* I quite liked this better than mine so I decided to use this one instead
*/
public static int isValidNumberPart2(int[] valid) {
List<String> stillValid = IntStream.of(valid).boxed().map(Object::toString).filter(f -> f.matches("^((\\d)\\2(?!\\2)\\d*|\\d*(\\d)(?!\\3)(\\d)\\4(?!\\4)\\d*)$")).collect(Collectors.toList());
return stillValid.size();
}
public static void main(String[] args) {
final int LOWER_LIMIT = 138307;
final int UPPER_LIMIT = 654504;
int[] validNumbers = IntStream.range(LOWER_LIMIT, UPPER_LIMIT).filter(Day4::isValidNumberPart1).toArray();
System.out.println("Part 1) " + validNumbers.length);
System.out.println("Part 2) " + isValidNumberPart2(validNumbers));
}
}