From 33ac9dc2b59860871bf2ac875ca40e450f84c662 Mon Sep 17 00:00:00 2001 From: ec93ujoh <jonas.plewinski@fau.de> Date: Fri, 2 Dec 2022 13:29:22 +0100 Subject: [PATCH] day 2 --- .idea/.gitignore | 8 ++++++++ __init__.py | 0 day2/day2.py | 32 ++++++++++++++++---------------- utilities.py | 4 ++++ 4 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 __init__.py create mode 100644 utilities.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day2/day2.py b/day2/day2.py index 5e199fe..8dc4eab 100644 --- a/day2/day2.py +++ b/day2/day2.py @@ -1,4 +1,5 @@ -import ../util.py +import utilities as ut + # Day 2 - Part 1 def rock_paper_scissor_guide_result(): @@ -8,14 +9,13 @@ def rock_paper_scissor_guide_result(): "C X": 6, "C Y": 0, "C Z": 3} print(score_rps) score = 0 - with open("./input.dat") as file: - for line in file: - #print(line) - player1, player2 = line.split() - print(f"{player2}: {score_rps[player2]}") - score += score_rps[player2] - print(f"{line.strip()}: {game_rps[line.strip()]}") - score += game_rps[line.strip()] + for line in ut.read_line_by_line("input.dat"): + #print(line) + player1, player2 = line.split() + print(f"{player2}: {score_rps[player2]}") + score += score_rps[player2] + print(f"{line.strip()}: {game_rps[line.strip()]}") + score += game_rps[line.strip()] print(score) @@ -26,15 +26,15 @@ def rock_paper_scissor_guide_result2(): "B X": 1, "B Y": 2, "B Z": 3, "C X": 2, "C Y": 3, "C Z": 1} score = 0 - with open("./input.dat") as file: - for line in file: - player1, result = line.split() - print(f"{result}: {score_rps[result]}") - score += score_rps[result] - print(f"{line.strip()}: {game_rps[line.strip()]}") - score += game_rps[line.strip()] + for line in ut.read_line_by_line("input.dat"): + player1, result = line.split() + print(f"{result}: {score_rps[result]}") + score += score_rps[result] + print(f"{line.strip()}: {game_rps[line.strip()]}") + score += game_rps[line.strip()] print(score) + if __name__ == '__main__': #rock_paper_scissor_guide_result() rock_paper_scissor_guide_result2() diff --git a/utilities.py b/utilities.py new file mode 100644 index 0000000..9074509 --- /dev/null +++ b/utilities.py @@ -0,0 +1,4 @@ +def read_line_by_line(filename): + with open(filename) as file: + for line in file: + yield line -- GitLab