-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Siller
committed
Dec 6, 2024
1 parent
9b57883
commit cb57430
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/input/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
|
||
|
||
def solve_part_1(inp: list) -> str: | ||
# split in 2 lists | ||
split1, split2 = zip(*[map(int, line.split()) | ||
for line in (line.strip() for line in inp)]) | ||
|
||
# sort by size | ||
split1 = sorted(split1) | ||
split2 = sorted(split2) | ||
|
||
# sum up absolute distances of each element between two list | ||
sol = sum(abs(split1[i] - split2[i]) for i in range(len(split1))) | ||
|
||
return f"Part 1 solved {sol}" | ||
|
||
|
||
def solve_part_2(inp: list) -> str: | ||
# add up similarity scores | ||
# sim score is how often element of right list appears in right list | ||
# split in 2 lists | ||
split1, split2 = zip(*[map(int, line.split()) | ||
for line in (line.strip() for line in inp)]) | ||
|
||
sim_score = sum(split1[i]*split2.count(split1[i]) | ||
for i in range(len(split1))) | ||
return f'Part 2 solved {sim_score}' | ||
|
||
|
||
if __name__ == '__main__': | ||
file_path = os.path.join(os.path.dirname(__file__), '../input/day01.txt') | ||
with open(file_path, 'r') as f: | ||
data = f.readlines() | ||
|
||
print(solve_part_1(data)) | ||
|
||
print(solve_part_2(data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import requests | ||
|
||
# Configuration | ||
YEAR = 2024 | ||
DAY = 1 # Change this to the day you want | ||
# Replace with your session cookie | ||
SESSION_COOKIE = "53616c7465645f5f12c69d3462b8e1bd70521f6ab7d69672e1a05650139b78abad9f9e7954266954676949723eca2c01ae1182973707f5df3ae15b5d4eede6c6" | ||
OUTPUT_DIR = "2k24_py/input" # Directory to store inputs | ||
OUTPUT_FILE = f"day{DAY:02}.txt" | ||
|
||
|
||
def fetch_input(year, day, session_cookie): | ||
url = f"https://adventofcode.com/{year}/day/{day}/input" | ||
headers = {"Cookie": f"session={session_cookie}"} | ||
response = requests.get(url, headers=headers) | ||
|
||
if response.status_code == 200: | ||
return response.text | ||
else: | ||
raise Exception( | ||
f"Failed to fetch input: {response.status_code} {response.reason}") | ||
|
||
|
||
def save_to_file(content, output_dir, output_file): | ||
os.makedirs(output_dir, exist_ok=True) | ||
file_path = os.path.join(output_dir, output_file) | ||
with open(file_path, "w") as file: | ||
file.write(content) | ||
print(f"Input saved to {file_path}") | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
input_data = fetch_input(YEAR, DAY, SESSION_COOKIE) | ||
save_to_file(input_data, OUTPUT_DIR, OUTPUT_FILE) | ||
except Exception as e: | ||
print(f"Error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
0.52 bar | ||
{'temperature_K': 227.34250723484834, 'enthalpy_kJ_per_kg': 1544.5402852635202, 'specific_volume': 2.0958537578094534} | ||
|
||
0.53 bar | ||
{'temperature_K': 227.67601059399306, 'enthalpy_kJ_per_kg': 1545.08063663741, 'specific_volume': 2.05954646197654253} | ||
|
||
0.54 bar | ||
{'temperature_K': 228.0095139531378, 'enthalpy_kJ_per_kg': 1545.6209880112997, 'specific_volume': 2.023239166143631} | ||
|
||
0.55 bar | ||
{'temperature_K': 228.33301641592902, 'enthalpy_kJ_per_kg': 1546.1431989267066, 'specific_volume': 1.9894432670324504} | ||
|
||
0.56 bar | ||
{'temperature_K': 228.65651887872806, 'enthalpy_kJ_per_kg': 1546.6654098421134, 'specific_volume': 1.95564736792127} | ||
|
||
|
||
|
||
|
||
|
||
0.74bar | ||
{'temperature_K': 233.75844138599876, 'enthalpy_kJ_per_kg': 1554.7637477949058, 'specific_volume': 1.5072787921740647} | ||
|
||
0.75 bar | ||
{'temperature_K': 234.00805027264542, 'enthalpy_kJ_per_kg': 1555.1529252451353, 'specific_volume': 1.48896292758477} | ||
|
||
0.76 bar | ||
{'temperature_K': 234.25765915929205, 'enthalpy_kJ_per_kg': 1555.5421026953647, 'specific_volume': 1.470647062995475} | ||
|
||
0.77 bar | ||
{'temperature_K': 234.5072680459387, 'enthalpy_kJ_per_kg': 1555.9312801455942, 'specific_volume': 1.4523311984061804} |
File renamed without changes.