Skip to content

Commit

Permalink
Day04: Bit more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 4, 2024
1 parent 6ca02dc commit 1ee77b6
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions lib/day04.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ int solveA(List<String> input) {
if (grid.get(x, y) == letterX) {
// horizontal
if (checkWord(
grid.get(x, y),
grid.get(x + 1, y),
grid.get(x + 2, y),
grid.get(x + 3, y),
)) {
xmasCount++;
}
if (checkWord(
grid.get(x, y),
grid.get(x - 1, y),
grid.get(x - 2, y),
grid.get(x - 3, y),
Expand All @@ -59,15 +57,13 @@ int solveA(List<String> input) {

// vertical
if (checkWord(
grid.get(x, y),
grid.get(x, y + 1),
grid.get(x, y + 2),
grid.get(x, y + 3),
)) {
xmasCount++;
}
if (checkWord(
grid.get(x, y),
grid.get(x, y - 1),
grid.get(x, y - 2),
grid.get(x, y - 3),
Expand All @@ -77,31 +73,27 @@ int solveA(List<String> input) {

// diagonal
if (checkWord(
grid.get(x, y),
grid.get(x + 1, y + 1),
grid.get(x + 2, y + 2),
grid.get(x + 3, y + 3),
)) {
xmasCount++;
}
if (checkWord(
grid.get(x, y),
grid.get(x - 1, y - 1),
grid.get(x - 2, y - 2),
grid.get(x - 3, y - 3),
)) {
xmasCount++;
}
if (checkWord(
grid.get(x, y),
grid.get(x + 1, y - 1),
grid.get(x + 2, y - 2),
grid.get(x + 3, y - 3),
)) {
xmasCount++;
}
if (checkWord(
grid.get(x, y),
grid.get(x - 1, y + 1),
grid.get(x - 2, y + 2),
grid.get(x - 3, y + 3),
Expand All @@ -115,20 +107,9 @@ int solveA(List<String> input) {
return xmasCount;
}

bool checkWord(
int? letter1,
int? letter2,
int? letter3,
int? letter4,
) =>
(letter1 == letterX &&
letter2 == letterM &&
letter3 == letterA &&
letter4 == letterS) ||
(letter1 == letterS &&
letter2 == letterA &&
letter3 == letterM &&
letter4 == letterX);
// First letter have already been checked to be `X`
bool checkWord(int? letter2, int? letter3, int? letter4) =>
letter2 == letterM && letter3 == letterA && letter4 == letterS;

int solveB(List<String> input) {
final grid = Grid(input.first.length, input.length)..setFromInput(input);
Expand All @@ -142,22 +123,19 @@ int solveB(List<String> input) {
grid.get(x, y + 2) == letterM &&
grid.get(x + 2, y + 2) == letterS) {
xmasCount++;
}
if (grid.get(x, y) == letterS &&
} else if (grid.get(x, y) == letterS &&
grid.get(x + 2, y) == letterS &&
grid.get(x + 1, y + 1) == letterA &&
grid.get(x, y + 2) == letterM &&
grid.get(x + 2, y + 2) == letterM) {
xmasCount++;
}
if (grid.get(x, y) == letterM &&
} else if (grid.get(x, y) == letterM &&
grid.get(x + 2, y) == letterM &&
grid.get(x + 1, y + 1) == letterA &&
grid.get(x, y + 2) == letterS &&
grid.get(x + 2, y + 2) == letterS) {
xmasCount++;
}
if (grid.get(x, y) == letterS &&
} else if (grid.get(x, y) == letterS &&
grid.get(x + 2, y) == letterM &&
grid.get(x + 1, y + 1) == letterA &&
grid.get(x, y + 2) == letterS &&
Expand Down

0 comments on commit 1ee77b6

Please sign in to comment.