-
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.
- Loading branch information
Showing
9 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,47 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Sequence | ||
|
||
def _extract_all_lines(input: Sequence[str]) -> Sequence[str]: | ||
lines = [] | ||
width = len(input[0]) | ||
height = len(input) | ||
# Horizontals | ||
for line in input: | ||
lines.append(line) | ||
# Verticals | ||
for x in range(width): | ||
vertical = [] | ||
for y in range(len(input)): | ||
vertical.append(input[y][x]) | ||
lines.append(''.join(vertical)) | ||
# Decreasing diagonals | ||
for delta in range(-width+2, width): | ||
diagonal = [] | ||
for y in range(height): | ||
if 0 <= y+delta < width: | ||
diagonal.append(input[y][y+delta]) | ||
lines.append(''.join(diagonal)) | ||
# Increasing diagonals | ||
for delta in range(-width, width-2): | ||
diagonal = [] | ||
for y in range(height-1, -1, -1): | ||
if 0 <= width-y+delta < width: | ||
diagonal.append(input[y][width-y+delta]) | ||
lines.append(''.join(diagonal)) | ||
return lines | ||
|
||
def count_xmas_words(input: Sequence[str]) -> int: | ||
lines = _extract_all_lines(input) | ||
return sum(line.count('XMAS') for line in lines) + sum(line.count('SAMX') for line in lines) |
Empty file.
Empty file.
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,5 @@ | ||
..X... | ||
.SAMX. | ||
.A..A. | ||
XMAS.S | ||
.X.... |
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,10 @@ | ||
....XXMAS. | ||
.SAMXMS... | ||
...S..A... | ||
..A.A.MS.X | ||
XMASAMX.MM | ||
X.....XA.A | ||
S.S.S.S.SS | ||
.A.A.A.A.A | ||
..M.M.M.MM | ||
.X.X.XMASX |
Large diffs are not rendered by default.
Oops, something went wrong.
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,35 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import unittest | ||
|
||
from aoc2024.src.day04.python.solution import count_xmas_words | ||
from common.python3.AdventOfCodeTestCase import AdventOfCodeTestCase | ||
|
||
|
||
class TestDay04Solution(AdventOfCodeTestCase): | ||
def __init__(self, *args, **kwargs): | ||
(super(TestDay04Solution, self).__init__(__file__, *args, **kwargs)) | ||
|
||
def test_part1_withExample_counts(self): | ||
self.assertEqual(4, count_xmas_words(self.examples[0])) | ||
|
||
def test_part1_withExample2_counts(self): | ||
self.assertEqual(18, count_xmas_words(self.examples[1])) | ||
|
||
def test_part1_withPuzzleInput_counts(self): | ||
self.assertEqual(2603, count_xmas_words(self.input)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |