Skip to content

Commit 69d0e29

Browse files
committed
2023 day 1 (and presolve)
1 parent d20539e commit 69d0e29

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
input.txt
3+
venv

2023/day1/main1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def calval(s):
2+
cv1, cv2 = 0, 0
3+
for i in range(len(s)):
4+
if s[i].isdigit():
5+
cv1 = int(s[i])
6+
break
7+
8+
for ii in range(len(s)):
9+
i = len(s) - ii - 1
10+
if s[i].isdigit():
11+
cv2 = int(s[i])
12+
break
13+
return cv1 * 10 + cv2
14+
15+
16+
def solution(lines: list[str]):
17+
ret = 0
18+
for line in lines:
19+
c = calval(line)
20+
ret += c
21+
return ret
22+
23+
24+
# data = '''1abc2
25+
# pqr3stu8vwx
26+
# a1b2c3d4e5f
27+
# treb7uchet
28+
# '''.split('\n')
29+
30+
data = open('input.txt', 'r').read().split('\n')
31+
32+
print(solution(data))

2023/day1/main2.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
strnums = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6,
2+
'seven': 7, 'eight': 8, 'nine': 9}
3+
4+
5+
def substrnum(s):
6+
for strnum in strnums:
7+
if s.startswith(strnum):
8+
return strnums[strnum]
9+
return 0
10+
11+
12+
def calval(s):
13+
cv1, cv2 = 0, 0
14+
for i in range(len(s)):
15+
if s[i].isdigit():
16+
cv1 = int(s[i])
17+
break
18+
else:
19+
c = substrnum(s[i:])
20+
if c != 0:
21+
cv1 = c
22+
break
23+
24+
for ii in range(len(s)):
25+
i = len(s) - ii - 1
26+
if s[i].isdigit():
27+
cv2 = int(s[i])
28+
break
29+
else:
30+
c = substrnum(s[i:])
31+
if c != 0:
32+
cv2 = c
33+
break
34+
return cv1 * 10 + cv2
35+
36+
37+
def solution(lines: list[str]):
38+
ret = 0
39+
for line in lines:
40+
c = calval(line)
41+
ret += c
42+
return ret
43+
44+
45+
# data = '''1abc2
46+
# pqr3stu8vwx
47+
# a1b2c3d4e5f
48+
# treb7uchet
49+
# '''.split('\n')
50+
51+
# data = '''two1nine
52+
# eightwothree
53+
# abcone2threexyz
54+
# xtwone3four
55+
# 4nineeightseven2
56+
# zoneight234
57+
# 7pqrstsixteen
58+
# '''.split('\n')
59+
60+
data = open('input.txt', 'r').read().split('\n')
61+
62+
print(solution(data))

presolve.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os.path
2+
import requests
3+
from bs4 import BeautifulSoup
4+
5+
from dotenv import load_dotenv
6+
7+
load_dotenv()
8+
9+
YEAR = int(os.environ.get('YEAR', 2023))
10+
DAY = int(os.environ.get('DAY', 1))
11+
SESSION_COOKIE = str(os.environ.get('SESSION_COOKIE', ''))
12+
13+
cookies = {'session': SESSION_COOKIE}
14+
input_response = requests.get(
15+
f'https://adventofcode.com/{YEAR}/day/{DAY}/input', cookies=cookies
16+
)
17+
18+
if not os.path.exists(str(YEAR)):
19+
os.mkdir(str(YEAR))
20+
21+
dirname = f'{YEAR}/day{DAY}'
22+
23+
if not os.path.exists(dirname):
24+
os.mkdir(dirname)
25+
26+
response = requests.get(
27+
f'https://adventofcode.com/{YEAR}/day/{DAY}', cookies=cookies
28+
)
29+
response.raise_for_status()
30+
soup = BeautifulSoup(response.text, 'html.parser')
31+
code_tags = soup.find_all('pre')
32+
code_content_list = [code_tag.get_text() for code_tag in code_tags]
33+
test_cases = [
34+
"# data = '''" + '\n# '.join(code_content.split("\n")) + r"'''.split('\n')"
35+
for code_content in code_content_list
36+
]
37+
main_content = r'''...
38+
39+
40+
def solution(lines: list[str]):
41+
ret = 0
42+
...
43+
return ret
44+
45+
46+
''' + '\n\n'.join(test_cases) + r'''
47+
48+
data = open('input.txt', 'r').read().split('\n')
49+
50+
print(solution(data))
51+
'''
52+
53+
main_path = f'{dirname}/main{len(code_content_list)}.py'
54+
if not os.path.exists(main_path):
55+
with open(main_path, 'w+') as f:
56+
f.write(main_content)
57+
58+
with open(f'{dirname}/input.txt', 'w+') as f:
59+
f.write(input_response.content.decode().strip('\n'))

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
beautifulsoup4
2+
python-dotenv~=1.0.0
3+
requests~=2.31.0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy