diff --git a/2/advent.py b/2/advent.py new file mode 100755 index 0000000..c51c2ef --- /dev/null +++ b/2/advent.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import re + +LIMITS = {"red": 12, "green": 13, "blue": 14} + + +def validateround(round): + for k in round: + if LIMITS[k] < round[k]: + return False + return True + + +with open("data.txt") as file: + lines = [line.rstrip() for line in file] + +gamesum = 0 + +for line in lines: + print(line) + matches = re.match("^Game (\\d+):(.*)", line) + if matches is not None: + gameid = int(matches.groups()[0]) + rounds = matches.groups()[1].split(";") + for round in rounds: + colors = round.split(",") + roundcolors = {} + for color in colors: + colormatch = re.match("\\s*(\\d+)\\s+(red|green|blue)\\s*+", color) + roundcolors[colormatch.groups()[1]] = int(colormatch.groups()[0]) + print(roundcolors) + roundstate = validateround(roundcolors) + if not roundstate: + break + if not roundstate: + print("Game not valid") + continue + gamesum += gameid + +print(gamesum) diff --git a/2/advent_2.py b/2/advent_2.py new file mode 100755 index 0000000..4ca7485 --- /dev/null +++ b/2/advent_2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +import re + +LIMITS = {"red": 12, "green": 13, "blue": 14} + + +def validateround(round): + for k in round: + if LIMITS[k] < round[k]: + return False + return True + + +with open("data.txt") as file: + lines = [line.rstrip() for line in file] + +gametotal = 0 + +for line in lines: + print(line) + matches = re.match("^Game (\\d+):(.*)", line) + if matches is not None: + gamemax = {"red": 0, "green": 0, "blue": 0} + gameid = int(matches.groups()[0]) + rounds = matches.groups()[1].split(";") + for round in rounds: + colors = round.split(",") + roundcolors = {} + for color in colors: + colormatch = re.match("\\s*(\\d+)\\s+(red|green|blue)\\s*+", color) + roundcolors[colormatch.groups()[1]] = int(colormatch.groups()[0]) + c = colormatch.groups()[1] + ccount = int(colormatch.groups()[0]) + if ccount > gamemax[c]: + gamemax[c] = ccount + print(roundcolors) + print("#####") + print(gamemax) + gamesum = 0 + for i in gamemax: + if gamemax[i] > 0 and gamesum != 0: + gamesum = gamesum * gamemax[i] + if gamemax[i] > 0 and gamesum == 0: + gamesum = gamemax[i] + print(gamesum) + gametotal += gamesum +print(gametotal) \ No newline at end of file