Skip to content

Commit

Permalink
AoC 2024 day 4 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 4, 2024
1 parent f0b1188 commit 5b2fc7b
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 0 deletions.
Empty file added aoc2024/src/day04/__init__.py
Empty file.
Empty file.
47 changes: 47 additions & 0 deletions aoc2024/src/day04/python/solution.py
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 added aoc2024/test/day04/__init__.py
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions aoc2024/test/day04/python/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
..X...
.SAMX.
.A..A.
XMAS.S
.X....
10 changes: 10 additions & 0 deletions aoc2024/test/day04/python/example2.txt
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
140 changes: 140 additions & 0 deletions aoc2024/test/day04/python/input.txt

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions aoc2024/test/day04/python/test_solution.py
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()

0 comments on commit 5b2fc7b

Please sign in to comment.