Day 2 2023
This commit is contained in:
32
2023/Day-02/day02-1.py
Executable file
32
2023/Day-02/day02-1.py
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
def main():
|
||||||
|
allowed_totals = {
|
||||||
|
'red': 12,
|
||||||
|
'green': 13,
|
||||||
|
'blue':14,
|
||||||
|
}
|
||||||
|
|
||||||
|
possible_games = []
|
||||||
|
|
||||||
|
with open('input.txt', 'r') as input_file:
|
||||||
|
for line in input_file:
|
||||||
|
possible = True
|
||||||
|
line = line.strip()
|
||||||
|
game_num = int(line.split(':')[0].split(' ')[1])
|
||||||
|
game_res = [result.split(',') for result in line.split(':')[1].split(';')]
|
||||||
|
|
||||||
|
for result in game_res:
|
||||||
|
for colour in result:
|
||||||
|
num_colour = int(colour.strip().split(' ')[0])
|
||||||
|
colour_name = colour.strip().split(' ', 1)[1]
|
||||||
|
|
||||||
|
if num_colour > allowed_totals[colour_name]:
|
||||||
|
possible = False
|
||||||
|
|
||||||
|
if possible:
|
||||||
|
possible_games.append(game_num)
|
||||||
|
print(sum(possible_games))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
31
2023/Day-02/day02-2.py
Executable file
31
2023/Day-02/day02-2.py
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from math import prod
|
||||||
|
|
||||||
|
def main():
|
||||||
|
total = 0
|
||||||
|
with open('input.txt', 'r') as input_file:
|
||||||
|
for line in input_file:
|
||||||
|
|
||||||
|
total_colours = {
|
||||||
|
'red': 0,
|
||||||
|
'green': 0,
|
||||||
|
'blue': 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
line = line.strip()
|
||||||
|
game_res = [result.split(',') for result in line.split(':')[1].split(';')]
|
||||||
|
|
||||||
|
for result in game_res:
|
||||||
|
for colour in result:
|
||||||
|
num_colour = int(colour.strip().split(' ')[0])
|
||||||
|
colour_name = colour.strip().split(' ', 1)[1]
|
||||||
|
|
||||||
|
if num_colour > total_colours[colour_name]:
|
||||||
|
total_colours[colour_name] = num_colour
|
||||||
|
|
||||||
|
total += prod(total_colours.values())
|
||||||
|
print(total)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Reference in New Issue
Block a user