Last active
January 9, 2024 03:15
-
-
Save glitchcowboy/10e48e7db9f4e95ca3dc3f361e7c7049 to your computer and use it in GitHub Desktop.
day4-part2-barak
This file contains hidden or 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
import pandas as pd | |
def main(): | |
df = pd.read_csv('input.txt', delim_whitespace=True, header=None) | |
total_score = 0 | |
df.insert(0, 'count_multiplier', 1) # set up an initial count multiplier field | |
for row in range(df.shape[0]): | |
row_score = 0 | |
winners, numbers_i_have = df.iloc[row, 3:13].tolist(), df.iloc[row, 14:40].tolist() | |
matches = [x for x in numbers_i_have if x in winners] | |
count_multiplier = df.loc[row, "count_multiplier"] | |
# Do the math | |
row_score = 0 if len(matches) == 0 else 2 ** (len(matches) - 1) * count_multiplier | |
print("row:", row, | |
"num_matches:", len(matches), | |
"match list:", matches, | |
"count_multiplier:", count_multiplier, | |
"row_score:", row_score) | |
# iterate over the next `num_matches` rows and increment their count_multiplier field | |
for i in range(len(matches)): | |
df.loc[row + i + 1, "count_multiplier"] += 1 | |
total_score += row_score | |
print("total score: ", total_score) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment