[Day 2 completed]

This commit is contained in:
xoy 2023-12-02 15:22:18 +01:00
parent e35e7103a3
commit 861b5c925e
8 changed files with 111 additions and 1004 deletions

View File

@ -14,7 +14,7 @@ def getDigitsOfLine(line) -> int:
print(f'Number found: {number}') print(f'Number found: {number}')
return number return number
lines = open('values', 'r').readlines() lines = open('values1', 'r').readlines()
number_sum = 0 number_sum = 0
@ -22,4 +22,4 @@ for line in lines:
if line != '': if line != '':
number_sum += getDigitsOfLine(line) number_sum += getDigitsOfLine(line)
print(f'Sum: {number_sum}') print(f'Sum: {number_sum}') # 142

View File

@ -25,7 +25,7 @@ def getDigitsOfLine(line) -> int:
print(f'Number found: {number}') print(f'Number found: {number}')
return number return number
lines = open('values', 'r').readlines() lines = open('values2', 'r').readlines()
number_sum = 0 number_sum = 0
@ -33,4 +33,4 @@ for line in lines:
if line != '': if line != '':
number_sum += getDigitsOfLine(line) number_sum += getDigitsOfLine(line)
print(f'Sum: {number_sum}') print(f'Sum: {number_sum}') # 281

File diff suppressed because it is too large Load Diff

4
Day 1/values1 Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
Day 1/values2 Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

47
Day 2/1.py Normal file
View File

@ -0,0 +1,47 @@
def parse_lists(lines: list[str]) -> dict[int, list[dict[str, int]]]:
game_list: dict[int, dict[str, int]] = dict()
for line in lines:
if not line == '':
game_id: int = (int)(line.split(':')[0].split(' ')[1])
print(f'Found game: {game_id}')
dirty_rounds = line.split(': ')[1].split('; ')
rounds = list()
for dirty_round in dirty_rounds:
round_split = dirty_round.split(', ')
round_part: dict[str, int] = dict()
for split in round_split:
part_split = split.split(' ')
round_part[part_split[1]] = (int)(part_split[0])
rounds.append(round_part)
game_list[game_id] = rounds
return game_list
def check_game_possibilities(max_blue: int, max_red: int, max_green: int, game_list: dict[int, list[dict[str, int]]]) -> int:
possible_sum = 0
for game_id in game_list:
is_possible = True
for round in game_list[game_id]:
if 'blue' in round and round['blue'] > max_blue:
is_possible = False
break
if 'red' in round and round['red'] > max_red:
is_possible = False
break
if 'green' in round and round['green'] > max_green:
is_possible = False
break
if is_possible:
possible_sum += game_id
return possible_sum
lines = open('./values', 'r').readlines()
game_list = parse_lists(lines)
print(f'Parsed {len(game_list)} games!')
print(f'Possible games sum: {check_game_possibilities(14, 12, 13, game_list)}') # 8

44
Day 2/2.py Normal file
View File

@ -0,0 +1,44 @@
def parse_lists(lines: list[str]) -> dict[int, list[dict[str, int]]]:
game_list: dict[int, dict[str, int]] = dict()
for line in lines:
if not line == '':
game_id: int = (int)(line.split(':')[0].split(' ')[1])
dirty_rounds = line.split(': ')[1].split('; ')
rounds = list()
for dirty_round in dirty_rounds:
round_split = dirty_round.split(', ')
round_part: dict[str, int] = dict()
for split in round_split:
part_split = split.split(' ')
round_part[part_split[1].strip('\n')] = (int)(part_split[0])
rounds.append(round_part)
game_list[game_id] = rounds
return game_list
def get_game_possible_sum(game_list: dict[int, list[dict[str, int]]]) -> int:
possible_sum = 0
for game_id in game_list:
reds = list()
blues = list()
greens = list()
for round in game_list[game_id]:
if 'blue' in round:
blues.append(round['blue'])
if 'red' in round:
reds.append(round['red'])
if 'green' in round:
greens.append(round['green'])
possible_sum += max(reds) * max(blues) * max(greens)
return possible_sum
lines = open('./values', 'r').readlines()
game_list = parse_lists(lines)
print(f'Parsed {len(game_list)} games!')
print(f'Possible games sum: {get_game_possible_sum(game_list)}') # 2286

5
Day 2/values Normal file
View File

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green