Skip to content

Instantly share code, notes, and snippets.

@HundredVisionsGuy
Created November 28, 2023 18:00
Show Gist options
  • Save HundredVisionsGuy/2b6b02a4178d65f580fd6f6403af1611 to your computer and use it in GitHub Desktop.
Save HundredVisionsGuy/2b6b02a4178d65f580fd6f6403af1611 to your computer and use it in GitHub Desktop.
Advent of Code Starter Functions
"""
I organize my Advent of Code Challenges in folders.
Below is the folder structure:
01a
|_ inputs.txt # where I store the puzzle inputs
|_ main.py # where I put my code
|_ test.txt # where I store the sample inputs
"""
def get_numbers(file: str) -> list:
"""returns a list of each line in the file as a string
Args:
line: the line in the text file
Returns:
numbers: a list of each number in each row from top
to bottom."""
file = open(file, 'r')
numbers = file.readlines()
file.close()
return numbers
def strip_return(line: str) -> int:
"""converts number in a line to an int.
NOTE: this only works if each line has a single number.
Args:
line: the line in the text file
Returns:
int: the number as an integer"""
if '\n' in line:
return str_to_int(line[:-1])
else:
return str_to_int(line)
def str_to_int(num: str) -> int:
"""Returns an int version of a string for math operations"""
return int(num)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment