|
| 1 | +package aminetti.adventofcode2024.day02; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +public class Day02 { |
| 12 | + private static final Logger LOGGER = LoggerFactory.getLogger(Day02.class); |
| 13 | + private List<String> input; |
| 14 | + |
| 15 | + public Day02() { |
| 16 | + } |
| 17 | + |
| 18 | + public void parseInput(List<String> input) { |
| 19 | + this.input = input; |
| 20 | + } |
| 21 | + |
| 22 | + public long solvePart1() { |
| 23 | + Pattern p = Pattern.compile("(\\d+)"); |
| 24 | + |
| 25 | + List<List<Long>> allLists = new ArrayList<>(input.size()); |
| 26 | + |
| 27 | + for (String s : input) { |
| 28 | + Matcher matcher = p.matcher(s); |
| 29 | + List<Long> list = new ArrayList<>(); |
| 30 | + |
| 31 | + while (matcher.find()) { |
| 32 | + Long a = Long.valueOf(matcher.group(1)); |
| 33 | + LOGGER.debug("Reading: {}", a); |
| 34 | + list.add(a); |
| 35 | + } |
| 36 | + |
| 37 | + allLists.add(list); |
| 38 | + } |
| 39 | + |
| 40 | + return allLists.stream() |
| 41 | + .filter(this::isSafe) |
| 42 | + .count(); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + private boolean isSafe(List<Long> l) { |
| 47 | + |
| 48 | + LOGGER.info("Checking {}", l); |
| 49 | + Long prev = null; |
| 50 | + Boolean ascending = null; |
| 51 | + for (Long i : l) { |
| 52 | + if (prev == null) { |
| 53 | + prev = i; |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (prev == i) { |
| 58 | + LOGGER.info(" has 2 same values {}", i); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (ascending == null) { |
| 63 | + ascending = prev < i; |
| 64 | + LOGGER.info(" is {}", ascending ? "ascending" : "descending"); |
| 65 | + } else { |
| 66 | + |
| 67 | + if (ascending && prev >= i) { |
| 68 | + LOGGER.info(" started as ascending, but is not!"); |
| 69 | + return false; |
| 70 | + } |
| 71 | + if (!ascending && prev <= i) { |
| 72 | + LOGGER.info(" started as descending, but is not!"); |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (Math.abs(prev - i) > 3 && Math.abs(prev - i) > 0) { |
| 78 | + LOGGER.info(" has too big gap between {} and {}", prev, i); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + prev = i; |
| 83 | + } |
| 84 | + |
| 85 | + LOGGER.info(" is safe"); |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + public long solvePart2() { |
| 90 | + Pattern p = Pattern.compile("(\\d+)"); |
| 91 | + |
| 92 | + List<List<Long>> allLists = new ArrayList<>(input.size()); |
| 93 | + |
| 94 | + for (String s : input) { |
| 95 | + Matcher matcher = p.matcher(s); |
| 96 | + List<Long> list = new ArrayList<>(); |
| 97 | + |
| 98 | + while (matcher.find()) { |
| 99 | + Long a = Long.valueOf(matcher.group(1)); |
| 100 | + LOGGER.debug("Reading: {}", a); |
| 101 | + list.add(a); |
| 102 | + } |
| 103 | + |
| 104 | + allLists.add(list); |
| 105 | + } |
| 106 | + |
| 107 | + return allLists.stream() |
| 108 | + .filter(this::isSafeWithoutOneLevel) |
| 109 | + .count(); |
| 110 | + } |
| 111 | + |
| 112 | + private boolean isSafeWithoutOneLevel(List<Long> l) { |
| 113 | + List<List<Long>> possibleCombinations = new ArrayList<>(); |
| 114 | + for (int i = 0; i < l.size(); i++) { |
| 115 | + List<Long> l1 = new ArrayList<>(l); |
| 116 | + l1.remove(i); |
| 117 | + possibleCombinations.add(l1); |
| 118 | + } |
| 119 | + return possibleCombinations.stream().anyMatch(this::isSafe); |
| 120 | + } |
| 121 | +} |
0 commit comments