generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/day_24
- Loading branch information
Showing
6 changed files
with
128 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.adventofcode.flashk.day25; | ||
|
||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class CodeChronicle { | ||
|
||
private Set<Lock> locks = new HashSet<>(); | ||
private Set<Key> keys = new HashSet<>(); | ||
|
||
public CodeChronicle(List<String> inputs) { | ||
|
||
int i = 0; | ||
char[][] map = new char[7][5]; | ||
boolean isLock = false; | ||
|
||
for(String row : inputs) { | ||
|
||
if(i == 0) { | ||
isLock = "#####".equals(row); | ||
} | ||
|
||
if(i == 7) { | ||
|
||
if(isLock) { | ||
locks.add(new Lock(map)); | ||
} else { | ||
keys.add(new Key(map)); | ||
} | ||
|
||
map = new char[7][5]; | ||
i = 0; | ||
|
||
} else { | ||
map[i++] = row.toCharArray(); | ||
} | ||
|
||
} | ||
|
||
if(isLock) { | ||
locks.add(new Lock(map)); | ||
} else { | ||
keys.add(new Key(map)); | ||
} | ||
} | ||
|
||
public long solveA() { | ||
long count = 0; | ||
for(Lock lock : locks) { | ||
for(Key key :keys) { | ||
if(lock.canFit(key)) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.adventofcode.flashk.day25; | ||
|
||
import com.adventofcode.flashk.common.Array2DUtil; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class Key { | ||
|
||
@Getter | ||
private final int[] heights = new int[5]; | ||
|
||
public Key(char[][] keyMap) { | ||
char[][] transposedMap = Array2DUtil.transpose(keyMap); | ||
|
||
for(int i = 0; i < transposedMap.length; i++) { | ||
String rowValue = new String(transposedMap[i]); | ||
heights[i] = StringUtils.countMatches(rowValue, "#")-1; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.adventofcode.flashk.day25; | ||
|
||
import com.adventofcode.flashk.common.Array2DUtil; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class Lock { | ||
|
||
private final int[] pins = new int[5]; | ||
|
||
public Lock(char[][] lockMap) { | ||
|
||
char[][] transposedMap = Array2DUtil.transpose(lockMap); | ||
|
||
for(int i = 0; i < transposedMap.length; i++) { | ||
String rowValue = new String(transposedMap[i]); | ||
pins[i] = StringUtils.countMatches(rowValue, "#")-1; | ||
} | ||
} | ||
|
||
public boolean canFit(Key key) { | ||
|
||
boolean fit = true; | ||
int[] heights = key.getHeights(); | ||
int i = 0; | ||
|
||
while(fit && i < 5) { | ||
int spaceLeft = 5 - pins[i]; | ||
if(spaceLeft < heights[i]){ | ||
fit = false; | ||
} | ||
i++; | ||
} | ||
|
||
return fit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Day 25: | ||
# Day 25: Code Chronicle | ||
|
||
[https://adventofcode.com/2024/day/25](https://adventofcode.com/2024/day/25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters