Skip to content

Commit

Permalink
AoC 2023 Day 1 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 1, 2023
1 parent 6f31dbb commit b6dab37
Show file tree
Hide file tree
Showing 11 changed files with 1,078 additions and 0 deletions.
Empty file added aoc2023/__init__.py
Empty file.
Empty file added aoc2023/src/__init__.py
Empty file.
Empty file added aoc2023/src/day01/__init__.py
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions aoc2023/src/day01/python/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 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.
def _find_first_digit(string: str) -> int:
"""Returns the left-most digit in a string."""
return next(int(char) for char in string if char.isdigit())


def _find_last_digit(string: str) -> int:
"""Returns the right-most digit in a string."""
return next(
int(string[i]) for i in range(len(string) - 1, -1, -1)
if string[i].isdigit())


def _get_calibration_value(string: str) -> int:
"""Calculates calibration value of a string."""
return _find_first_digit(string) * 10 + _find_last_digit(string)


def sum_calibration_values(calibration_document: tuple[str, ...]) -> int:
"""Calculates sum of the calibration values.
The calibration value can be found by combining the first digit and the last
digit (in that order) to form a single two-digit number.
Args
calibration_document A calibration document.
Returns
Sum of all calibration values in the document.
"""
return sum((_get_calibration_value(line) for line in calibration_document))
Empty file added aoc2023/test/__init__.py
Empty file.
Empty file added aoc2023/test/day01/__init__.py
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions aoc2023/test/day01/python/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
Loading

0 comments on commit b6dab37

Please sign in to comment.