Compare commits
3 commits
c1ac5a1c54
...
f1907db70c
Author | SHA1 | Date | |
---|---|---|---|
|
f1907db70c | ||
|
5f569ccb77 | ||
|
ca016707a3 |
7 changed files with 1132 additions and 0 deletions
12
python/2023/README.org
Normal file
12
python/2023/README.org
Normal 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
|
25
python/2023/pyproject.toml
Normal file
25
python/2023/pyproject.toml
Normal 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"
|
0
python/2023/src/andy_aoc_2023/__init__.py
Normal file
0
python/2023/src/andy_aoc_2023/__init__.py
Normal file
84
python/2023/src/andy_aoc_2023/day1.py
Normal file
84
python/2023/src/andy_aoc_2023/day1.py
Normal 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)
|
1000
python/2023/src/andy_aoc_2023/day1_big
Normal file
1000
python/2023/src/andy_aoc_2023/day1_big
Normal file
File diff suppressed because it is too large
Load diff
4
python/2023/src/andy_aoc_2023/day1_small
Normal file
4
python/2023/src/andy_aoc_2023/day1_small
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
python/2023/src/andy_aoc_2023/day1_small_2
Normal file
7
python/2023/src/andy_aoc_2023/day1_small_2
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Reference in a new issue