Skip to content

Commit b4daccb

Browse files
committed
Add func to retrieve input file
1 parent c74b3b6 commit b4daccb

File tree

8 files changed

+45
-2
lines changed

8 files changed

+45
-2
lines changed

.env

147 Bytes
Binary file not shown.

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
input.txt filter=git-crypt diff=git-crypt
1+
input*.txt filter=git-crypt diff=git-crypt
22
.env filter=git-crypt diff=git-crypt
Binary file not shown.

src/AoC_2015/d24_sleigh_balance_subset_sum/sleigh_balance.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@
3232
from itertools import combinations
3333
import common.aoc_commons as td
3434

35+
YEAR = 2015
36+
DAY = 24
37+
3538
locations = td.get_locations(__file__)
3639
logger = td.retrieve_console_logger(locations.script_name)
3740
logger.setLevel(logging.INFO)
41+
td.write_puzzle_input_file(YEAR, DAY, locations.input_file)
3842

3943
def main():
4044
# with open(locations.sample_input_file, mode="rt") as f:

src/common/aoc_commons.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import logging
1818
import os
1919
from pathlib import Path
20+
from dotenv import load_dotenv
21+
import requests
2022
from colorama import Fore
2123

2224
#################################################################
@@ -116,7 +118,40 @@ def get_locations(script_file):
116118
input_file = Path(input_dir, "input.txt")
117119
sample_input_file = Path(script_dir, "input/sample_input.txt")
118120

119-
return Locations(script_name, script_dir, input_dir, output_dir, sample_input_file, input_file)
121+
return Locations(script_name, script_dir,
122+
input_dir,
123+
output_dir,
124+
sample_input_file, input_file)
125+
126+
##################################################################
127+
# Retrieving input data
128+
##################################################################
129+
130+
def write_puzzle_input_file(year: int, day: int, input_file):
131+
if os.path.exists(input_file):
132+
logger.debug("%s already exists", os.path.basename(input_file))
133+
return
134+
135+
load_dotenv(os.path.join(os.path.dirname(__file__), '../../.env'))
136+
SESSION_COOKIE = os.getenv('AOC_SESSION_COOKIE')
137+
if SESSION_COOKIE:
138+
logger.info('Session cookie retrieved.')
139+
else:
140+
logger.error('Failed to retrieve session cookie')
141+
142+
url = f"https://adventofcode.com/{year}/day/{day}/input"
143+
cookies = {"session": SESSION_COOKIE}
144+
response = requests.get(url, cookies=cookies)
145+
data = ""
146+
147+
if response.status_code == 200:
148+
data = response.text
149+
else:
150+
data = f"Failed to retrieve puzzle input: {response.status_code}"
151+
152+
logger.debug("Writing %s", os.path.basename(input_file))
153+
with open(input_file, 'w') as file:
154+
file.write(data)
120155

121156
#################################################################
122157
# POINTS, VECTORS AND GRIDS

src/template_folder/input/input.txt

-33 Bytes
Binary file not shown.

src/template_folder/template.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
import time
1414
import common.aoc_commons as td
1515

16+
YEAR = 2017
17+
DAY = 1
18+
1619
locations = td.get_locations(__file__)
1720
logger = td.retrieve_console_logger(locations.script_name)
1821
logger.setLevel(logging.DEBUG)
1922
# td.setup_file_logging(logger, locations.output_dir)
23+
td.write_puzzle_input_file(YEAR, DAY, locations.input_file)
2024

2125
def main():
2226
with open(locations.sample_input_file, mode="rt") as f:
File renamed without changes.

0 commit comments

Comments
 (0)