- Published on
Advent of Code 2023: Day 2 Solution
- Authors
- Name
- Tinker Assist
Table of Contents
Part 1 Solution
Read the Puzzle Input File
We will save our puzzle input in "day2_input.txt" and then read from that file.
file = open("day2_input.txt", "r")
input = file.read()
We will then break up the input by line:
input_list = input.split('\n')
The Code
# part 1
sum = 0
for ind, inp in enumerate(input_list):
inp = inp.split(':')[1].split(';')
possible = True
for games in inp:
games = games.strip().split(',')
for game in games:
game = game.strip().split(' ')
if ((game[1] == 'red' and int(game[0]) > 12) or
(game[1] == 'green' and int(game[0]) > 13) or
(game[1] == 'blue' and int(game[0]) > 14)):
possible = False
if possible:
sum += (ind+1)
print(sum)
Part 2 Solution
Read the Puzzle Input File
We have the same puzzle input as part 1, so again we will save our puzzle input in "day2_input.txt" and then read from that file.
file = open("day2_input.txt", "r")
input = file.read()
Again, we will break up the input by line:
input_list = input.split('\n')
The Code
# part 2
sum = 0
for inp in input_list:
inp = inp.split(':')[1].split(';')
rgb = [0, 0, 0]
for games in inp:
games = games.strip().split(',')
for game in games:
game = game.strip().split(' ')
if game[1] == 'green' and int(game[0]) > rgb[1]:
rgb[1] = int(game[0])
if game[1] == 'red' and int(game[0]) > rgb[0]:
rgb[0] = int(game[0])
if game[1] == 'blue' and int(game[0]) > rgb[2]:
rgb[2] = int(game[0])
sum += (rgb[0]*rgb[1]*rgb[2])
print(sum)
Full code
Get the full code here.