Skip to content

Instantly share code, notes, and snippets.

@glitchcowboy
Last active January 9, 2024 03:15
Show Gist options
  • Save glitchcowboy/10e48e7db9f4e95ca3dc3f361e7c7049 to your computer and use it in GitHub Desktop.
Save glitchcowboy/10e48e7db9f4e95ca3dc3f361e7c7049 to your computer and use it in GitHub Desktop.
day4-part2-barak
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