feat: day_2 A done
This commit is contained in:
1 parent
bf4ebda10b
commit
7d7d41ad2a
6 files changed
+183
-5
No files matched your search
@@ -0,0 +1,64 @@
|
||||
from dataclasses import dataclass
|
||||
import re
|
||||
|
||||
|
||||
def run():
|
||||
game = GameRecord(r"./src/assets/day_2.txt")
|
||||
total_valid = game.sum_valid()
|
||||
print(f"Total games valid: {total_valid}")
|
||||
|
||||
|
||||
@dataclass
|
||||
class Game:
|
||||
id: int
|
||||
plays: list[list[tuple]]
|
||||
|
||||
|
||||
class GameRecord:
|
||||
path: str
|
||||
games: list[Game]
|
||||
|
||||
def __init__(self, path: str):
|
||||
self.path = path
|
||||
self.games = self.read_games()
|
||||
|
||||
def sum_valid(self):
|
||||
sum = 0
|
||||
for game in self.games:
|
||||
red = 0
|
||||
green = 0
|
||||
blue = 0
|
||||
for play in game.plays:
|
||||
for item in play:
|
||||
match item[1]:
|
||||
case "blue":
|
||||
blue = max(blue,int(item[0]))
|
||||
case "red":
|
||||
red = max(red, int(item[0]))
|
||||
case "green":
|
||||
green = max(green,int(item[0]))
|
||||
|
||||
if red <= 12 and green <= 13 and blue <= 14:
|
||||
sum += game.id
|
||||
|
||||
return sum
|
||||
|
||||
|
||||
def read_games(self) -> list[Game]:
|
||||
with open(self.path) as file:
|
||||
lines = file.readlines()
|
||||
games = []
|
||||
|
||||
for line in lines:
|
||||
game_parsed = re.findall(
|
||||
r"^Game ([0-9]+):(.*)", line
|
||||
)
|
||||
game_plays_raw = str(game_parsed[0][1]).split(";")
|
||||
game_id = int(game_parsed[0][0])
|
||||
game_plays = []
|
||||
for play in game_plays_raw:
|
||||
game_plays.append(re.findall(r"([0-9]+) (blue|red|green)", play.lstrip().rstrip()))
|
||||
|
||||
games.append(Game(game_id, game_plays))
|
||||
|
||||
return games
|
||||
Reference in new issue
Block a user