Compare commits

...

3 commits

Author SHA1 Message Date
Andy Lu
f1907db70c Add day 1 solution 2023-12-01 16:07:21 -05:00
Andy Lu
5f569ccb77 Add day1 inputs 2023-12-01 16:04:55 -05:00
Andy Lu
ca016707a3 Create directory structure 2023-12-01 12:08:49 -05:00
7 changed files with 1132 additions and 0 deletions

12
python/2023/README.org Normal file
View file

@ -0,0 +1,12 @@
* Advent of Code Python
** Day 1
#+begin_src shell
source ~/virtualenvs/aoc-2023/bin/activate
cd ~/git/advent-of-code/python/2023
python src/andy_aoc_2023/day1.py src/andy_aoc_2023/day1_big
#+end_src
*** Answers
54573
54591

View file

@ -0,0 +1,25 @@
[project]
name = "andy_aoc_2023"
version = "0.0.1"
dependencies = [
"",
]
authors = [
{ name="Andy Lu" },
]
description = "All of my code for 2023"
readme = "README.org"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
Homepage = "https://github.com/luandy64/advent-of-code"
Issues = "https://github.com/luandy64/advent-of-code/issues"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

View file

@ -0,0 +1,84 @@
from functools import reduce
from operator import add
import re
import sys
NUMBERS = {
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
}
WORDS = {
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
BOTH = {**WORDS, **NUMBERS}
def get_numbers(line, parse_words=False):
if parse_words:
return re.findall(rf"(?=({'|'.join(BOTH)}))", line)
return re.findall("|".join(NUMBERS.keys()), line)
def line_to_number(line, parse_words=False):
string_nums = get_numbers(line, parse_words)
if parse_words:
lookup = BOTH
else:
lookup = NUMBERS
first = lookup[string_nums[0]]
last = lookup[string_nums[-1]]
number = first + last
return int(number)
def part1(lines):
nums = map(line_to_number,
lines)
answer = reduce(add,
nums)
print(answer)
def part2(lines):
nums = map(lambda x: line_to_number(x, parse_words=True),
lines)
answer = reduce(add,
nums)
print(answer)
if __name__ == "__main__":
input_file = sys.argv[1]
with open(input_file) as infile:
input_lines = [
line.strip()
for line in infile.readlines()
]
part1(input_lines)
part2(input_lines)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

View file

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen