Re: Advent of Code
Verfasst: Donnerstag 14. Juli 2022, 13:59
https://csacademy.com/app/graph_editor/ fand ich sehr hilfreich, um die Verbindungen zwischen Höhlen an Tag 12 zu visualisieren!
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Ich bin nicht sicher, wo ich das sehen kann, aber habe mal versuchsweise das Leaderboard untersucht (obwohl ich nie Punkte erhalten werde, weil ich viel zu spät und dazu vergleichsweise langsam bin)
Code: Alles auswählen
These are your personal leaderboard statistics.
Rank is your position on that leaderboard:
1 means you were the first person to get that star, 2 means the second, 100 means the 100th, etc.
Score is the number of points you got for that rank:
100 for 1st, 99 for 2nd, ..., 1 for 100th, and 0 otherwise.
--------Part 1--------- --------Part 2---------
Day Time Rank Score Time Rank Score
(snip)
3 >24h 165345 0 >24h 130186 0
2 >24h 176249 0 >24h 181738 0
1 >24h 211476 0 >24h 189291 0
Code: Alles auswählen
Here are the current completion statistics for each day.
Gold indicates users that have completed both parts of a puzzle,
while silver indicates users that have completed only the first half.
Each * or * star represents up to 5746 users.
25 10830 5376 ***
24 12565 124 ****
23 13042 2130 ****
22 15433 5930 *****
21 19574 6295 ******
20 22483 352 *****
19 16543 249 ****
18 24853 169 ******
17 32582 1433 *******
16 32565 1645 *******
15 38174 3388 ********
14 45636 6956 **********
13 51446 881 **********
12 49389 1955 **********
11 58435 309 ************
10 65151 1769 *************
9 63962 10314 **************
8 68367 12023 ***************
7 87681 2318 *****************
6 88779 6731 ******************
5 90364 4536 *****************
4 102431 6113 ********************
3 131743 35789 ******************************
2 183989 7147 ***********************************
1 204762 25053 *****************************************
Code: Alles auswählen
#!/usr/bin/env python3
from pathlib import Path
CALORIES = Path("/home/dennis/Dokumente/calories.txt")
def read_input_file(file):
calories = []
with open(file, encoding="UTF-8") as calories_file:
for line in calories_file:
try:
calories.append(int(line))
except ValueError:
calories.append(None)
return calories
def search_three_maxima(calories):
return sum(sorted(calories, reverse=True)[:3])
def select_calories_pro_elv(calories):
calorie_pro_elv = []
calorie_of_all_elves = []
for calorie in calories:
if calorie is not None:
calorie_pro_elv.append(calorie)
else:
calorie_of_all_elves.append(sum(calorie_pro_elv))
calorie_pro_elv = []
return calorie_of_all_elves
def main():
calories = read_input_file(CALORIES)
total_calories = select_calories_pro_elv(calories)
print(max(total_calories))
print(search_three_maxima(total_calories))
if __name__ == "__main__":
main()
Davor hatte ich die Funktion so:Ähm, das ist mir gerade zu hoch
Code: Alles auswählen
def search_three_maxima(calories):
max_1 = [calorie for calorie in calories if calorie != max(calories)]
max_2 = [calorie for calorie in max_1 if calorie != max(max_1)]
max_3 = [calorie for calorie in max_2 if calorie != max(max_2)]
return sum(calories) - sum(max_3)
Tag 1 geht oft auch noch ganz gut ohne Python :
Code: Alles auswählen
$ awk 'BEGIN{RS="\n\n"}{sum=0;for (i=1;i<=NF;i++) sum+=$i; print sum }' day01_test.txt | sort -n | tail -n 1
24000
$ awk 'BEGIN{RS="\n\n"}{sum=0;for (i=1;i<=NF;i++) sum+=$i; print sum }' day01_text.txt | sort -n | tail -n 3 | awk '{sum+=$0}END{print sum}'
45000
Code: Alles auswählen
10 TI$="000000":EC=0:E1=0:E2=0:E3=0:C=0
20 OPEN 2,8,2,"INPUT01,S"
30 PRINT"PROCESSING ELVES..."
40 IF ST<>0 THEN CLOSE 2:GOTO 80
50 INPUT#2,A$:N=VAL(A$):C=C+N
60 IF N=0 THEN GOSUB 500
70 GOTO 40
80 GOSUB 500:PRINT
90 PRINT E1;E1+E2+E3
100 PRINT TI$:END
500 EC=EC+1:PRINT EC:PRINT"{UP}";
510 IF C>E1 THEN E3=E2:E2=E1:E1=C:GOTO 540
520 IF C>E2 THEN E3=E2:E2=C:GOTO 540
530 IF C>E3 THEN E3=C
540 C=0:RETURN
Code: Alles auswählen
RUN
PROCESSING ELVES...
260
69912 208180
000122
READY.
Code: Alles auswählen
#!/usr/bin/env python3
import sys
from more_itertools import split_at
TEST_LINES = """\
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
""".splitlines()
def is_blank(line):
return line.strip() == ""
def calculate_elves_calories(lines):
return sorted(
(
sum(map(int, elve_lines))
for elve_lines in split_at(lines, is_blank)
),
reverse=True,
)
def main():
elves_calories = calculate_elves_calories(sys.stdin)
assert len(elves_calories) >= 3
print("# of elves:", len(elves_calories))
print("Top calories:", elves_calories[0])
print("Sum of top 3 calories:", sum(elves_calories[:3]))
def test_calculate_elves_calories():
assert calculate_elves_calories(TEST_LINES) == [
24000,
11000,
10000,
6000,
4000,
]
if __name__ == "__main__":
main()
Code: Alles auswählen
from pathlib import Path
TEST = """\
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
"""
def get_input():
with Path('input.txt').open() as fp:
return fp.read()
def main():
inp = get_input()
calories = [sum(map(int, x.split('\n'))) for x in inp.strip().split('\n\n')]
calories.sort(reverse=True)
print('Part 1')
print(calories[0])
print('Part 2')
print(sum(calories[:3]))
if __name__ == '__main__':
main()
Code: Alles auswählen
a=sorted(sum(map(int,c.split()))for c in open("i").read().split("\n\n"));print(a[-1],sum(a[-3:]))
Code: Alles auswählen
#!/usr/bin/env python3
from pathlib import Path
STRATEGY_FILE = Path(r"C:\Users\Dennis\Documents\strategy.txt")
WINNING_COMBINATIONS = [("A", "Y"), ("B", "Z"), ("C", "X")]
DRAWING_COMBINATIONS = [("A", "X"), ("B", "Y"), ("C", "Z")]
CHOICE_TO_POINTS = {
"X": 1,
"Y": 2,
"Z": 3,
}
def read_file(file):
with open(file, encoding="UTF-8") as strategy_file:
strategy = [line.strip("\n") for line in strategy_file]
return strategy
def calculate_total_points(strategy):
points = []
for game in strategy:
elv, me = game.split(" ")
if (elv, me) in WINNING_COMBINATIONS:
game_situation = 6
elif (elv, me) in DRAWING_COMBINATIONS:
game_situation = 3
else:
game_situation = 0
points.append(game_situation + CHOICE_TO_POINTS[me])
return sum(points)
def main():
strategy = read_file(STRATEGY_FILE)
total_points = calculate_total_points(strategy)
print(total_points)
if __name__ == "__main__":
main()
Code: Alles auswählen
#!/usr/bin/env python3
from pathlib import Path
STRATEGY_FILE = Path(r"C:\Users\Dennis\Documents\strategy.txt")
WINNING_COMBINATIONS = [("A", "B"), ("B", "C"), ("C", "A")]
CHOICE_TO_POINTS = {
"A": 1,
"B": 2,
"C": 3,
}
def read_file(file):
with open(file, encoding="UTF-8") as strategy_file:
strategy = [line.strip("\n") for line in strategy_file]
return strategy
def calculate_total_points(strategy):
points = []
for game in strategy:
elv, game_situation = game.split(" ")
if game_situation == 'Z':
game_points = 6
for situation in WINNING_COMBINATIONS:
if situation[0] == elv:
tool_points = CHOICE_TO_POINTS[situation[1]]
elif game_situation == 'Y':
game_points = 3
tool_points = CHOICE_TO_POINTS[elv]
else:
game_points = 0
for situation in WINNING_COMBINATIONS:
if situation[1] == elv:
tool_points = CHOICE_TO_POINTS[situation[0]]
points.append(game_points + tool_points)
return sum(points)
def main():
strategy = read_file(STRATEGY_FILE)
total_points = calculate_total_points(strategy)
print(total_points)
if __name__ == "__main__":
main()