|
| 1 | +package eu.happycoders.adventofcode2022.day21; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +class Jobs { |
| 7 | + |
| 8 | + private static final String ROOT = "root"; |
| 9 | + private static final String HUMAN = "humn"; |
| 10 | + |
| 11 | + private final List<Job> allJobs; |
| 12 | + private final Job rootJob; |
| 13 | + private final Job humanJob; |
| 14 | + |
| 15 | + Jobs(Map<String, Job> monkeyNameToJob) { |
| 16 | + this.allJobs = List.copyOf(monkeyNameToJob.values()); |
| 17 | + this.rootJob = monkeyNameToJob.get(ROOT); |
| 18 | + this.humanJob = monkeyNameToJob.get(HUMAN); |
| 19 | + } |
| 20 | + |
| 21 | + long solve() { |
| 22 | + return solve(rootJob); |
| 23 | + } |
| 24 | + |
| 25 | + private long solve(Job job) { |
| 26 | + return switch (job) { |
| 27 | + case NumberJob numberJob -> numberJob.result(); |
| 28 | + case MathOperationJob mathOperationJob -> solveMathOperationJob(mathOperationJob); |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + private long solveMathOperationJob(MathOperationJob mathOperationJob) { |
| 33 | + Long cachedResult = mathOperationJob.result(); |
| 34 | + if (cachedResult != null) { |
| 35 | + return cachedResult; |
| 36 | + } |
| 37 | + |
| 38 | + long operand1 = solve(mathOperationJob.operand1Job()); |
| 39 | + long operand2 = solve(mathOperationJob.operand2Job()); |
| 40 | + |
| 41 | + long result = mathOperationJob.apply(operand1, operand2); |
| 42 | + |
| 43 | + mathOperationJob.setCachedResult(result); |
| 44 | + |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + long findNumberForHumanIfRootOperandsMustBeEqual() { |
| 49 | + solve(); |
| 50 | + removeCachedResultsFromRootToHuman(); |
| 51 | + return applyReverseOperationsFromRootToHuman(); |
| 52 | + } |
| 53 | + |
| 54 | + private void removeCachedResultsFromRootToHuman() { |
| 55 | + while (true) { |
| 56 | + for (Job job : allJobs) { |
| 57 | + if (isOperationWithResultAndOneOperandHavingNoResultOrBeingHuman(job)) { |
| 58 | + ((MathOperationJob) job).setCachedResult(null); |
| 59 | + if (job == rootJob) { |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private boolean isOperationWithResultAndOneOperandHavingNoResultOrBeingHuman(Job job) { |
| 68 | + return job.result() != null |
| 69 | + && job instanceof MathOperationJob operation |
| 70 | + && (getResultOrNullIfHuman(operation.operand1Job()) == null |
| 71 | + || getResultOrNullIfHuman(operation.operand2Job()) == null); |
| 72 | + } |
| 73 | + |
| 74 | + private Long getResultOrNullIfHuman(Job job) { |
| 75 | + return job == humanJob ? null : job.result(); |
| 76 | + } |
| 77 | + |
| 78 | + private long applyReverseOperationsFromRootToHuman() { |
| 79 | + return applyReverseOperationsDownToHuman((MathOperationJob) rootJob, 0); |
| 80 | + } |
| 81 | + |
| 82 | + private long applyReverseOperationsDownToHuman(MathOperationJob job, long lastValue) { |
| 83 | + Long operand1 = getResultOrNullIfHuman(job.operand1Job()); |
| 84 | + Long operand2 = getResultOrNullIfHuman(job.operand2Job()); |
| 85 | + |
| 86 | + // Either go down the left path... |
| 87 | + if (operand1 == null && operand2 != null) { |
| 88 | + return applyReverseOperationsDownToHumanViaOperand1(job, lastValue, operand2); |
| 89 | + } |
| 90 | + |
| 91 | + // ... or the right path |
| 92 | + if (operand1 != null && operand2 == null) { |
| 93 | + return applyReverseOperationsDownToHumanViaOperand2(job, lastValue, operand1); |
| 94 | + } |
| 95 | + |
| 96 | + throw new IllegalStateException(); |
| 97 | + } |
| 98 | + |
| 99 | + private long applyReverseOperationsDownToHumanViaOperand1( |
| 100 | + MathOperationJob job, long lastValue, Long operand2) { |
| 101 | + Long operand1; |
| 102 | + if (job == rootJob) { |
| 103 | + operand1 = operand2; |
| 104 | + } else { |
| 105 | + operand1 = job.applyReverseToFindOperand1(lastValue, operand2); |
| 106 | + } |
| 107 | + |
| 108 | + if (job.operand1Job() == humanJob) { |
| 109 | + return operand1; |
| 110 | + } |
| 111 | + |
| 112 | + return applyReverseOperationsDownToHuman((MathOperationJob) job.operand1Job(), operand1); |
| 113 | + } |
| 114 | + |
| 115 | + private long applyReverseOperationsDownToHumanViaOperand2( |
| 116 | + MathOperationJob job, long lastValue, Long operand1) { |
| 117 | + Long operand2; |
| 118 | + if (job == rootJob) { |
| 119 | + operand2 = operand1; |
| 120 | + } else { |
| 121 | + operand2 = job.applyReverseToFindOperand2(lastValue, operand1); |
| 122 | + } |
| 123 | + |
| 124 | + if (job.operand2Job() == humanJob) { |
| 125 | + return operand2; |
| 126 | + } |
| 127 | + |
| 128 | + return applyReverseOperationsDownToHuman((MathOperationJob) job.operand2Job(), operand2); |
| 129 | + } |
| 130 | +} |
0 commit comments