advent of code 2023 edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
aoc_2023/aoc_04.py

42 lines
1.0 KiB

#!python3
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="the file from which to take data")
args = parser.parse_args()
points = 0
with open(args.filename, "r") as f:
for l in f.readlines():
secondhalf = l.split(":")[1]
winning = set(secondhalf.split("|")[0].split())
ours = set(secondhalf.split("|")[1].split())
print(winning & ours)
points += 2 ** (len(winning & ours) - 1) if winning & ours else 0
print(points)
line_re = re.compile("Card([ ]+\d+):(.*)\|(.*)")
with open(args.filename, "r") as f:
lines = f.readlines()
games = []
for l in lines:
games.append(1)
print("starting ouot we have:")
print(games)
for l in lines:
print(l)
line = line_re.match(l)
current_game_number = int(line.group(1))
winning = set(line.group(2).split())
ours = set(line.group(3).split())
wins = len(winning & ours)
next_several = range(current_game_number, current_game_number+wins)
for i in next_several:
if i < len(games):
games[i] += games[current_game_number-1]
print(sum(games))