add 2023/day1-2
This commit is contained in:
parent
517107044f
commit
eff16cced4
2 changed files with 147 additions and 0 deletions
58
2023/01/main.py
Normal file
58
2023/01/main.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- encoding: utf-8; py-indent-offset: 4 -*-
|
||||||
|
|
||||||
|
# Author: Sebastian Mark
|
||||||
|
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
|
||||||
|
|
||||||
|
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def readinput():
|
||||||
|
with open("input", "r", encoding="utf-8") as file:
|
||||||
|
lines = file.readlines()
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def get_number(line: str) -> int:
|
||||||
|
regex = r"\d"
|
||||||
|
matches = re.findall(regex, line)
|
||||||
|
new_number = matches[0] + matches[-1]
|
||||||
|
return int(new_number)
|
||||||
|
|
||||||
|
|
||||||
|
def get_spelled_number(line: str) -> int:
|
||||||
|
number_map = {
|
||||||
|
"one": "o1e",
|
||||||
|
"two": "t2o",
|
||||||
|
"three": "t3e",
|
||||||
|
"four": "f4r",
|
||||||
|
"five": "f5e",
|
||||||
|
"six": "s6x",
|
||||||
|
"seven": "s7n",
|
||||||
|
"eight": "e8t",
|
||||||
|
"nine": "n9e",
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v in number_map.items():
|
||||||
|
if k in line:
|
||||||
|
line = line.replace(k, v)
|
||||||
|
|
||||||
|
return get_number(line)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lines = readinput()
|
||||||
|
|
||||||
|
# part1
|
||||||
|
count = sum(get_number(line) for line in lines)
|
||||||
|
print("Sum: %d" % count)
|
||||||
|
|
||||||
|
# part1
|
||||||
|
count = sum(get_spelled_number(line) for line in lines)
|
||||||
|
print("Sum (literals): %d" % count)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
89
2023/02/main.py
Normal file
89
2023/02/main.py
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- encoding: utf-8; py-indent-offset: 4 -*-
|
||||||
|
|
||||||
|
# Author: Sebastian Mark
|
||||||
|
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
|
||||||
|
|
||||||
|
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def readinput():
|
||||||
|
with open("input", "r", encoding="utf-8") as file:
|
||||||
|
lines = file.readlines()
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def is_playable(line: str, max_cubes: tuple) -> bool:
|
||||||
|
games = line.split(";")
|
||||||
|
|
||||||
|
for game in games:
|
||||||
|
cube_count = {"red": 0, "green": 0, "blue": 0}
|
||||||
|
|
||||||
|
regex = r"(\d+) (\w+)"
|
||||||
|
matches = re.findall(regex, game)
|
||||||
|
|
||||||
|
for m in matches:
|
||||||
|
color = m[1]
|
||||||
|
count = int(m[0])
|
||||||
|
cube_count[color] += count
|
||||||
|
|
||||||
|
for color, count in cube_count.items():
|
||||||
|
if count > max_cubes[color]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def get_game_nr(line: str) -> int:
|
||||||
|
regex = r"Game (\d+):"
|
||||||
|
matches = re.findall(regex, line)
|
||||||
|
return int(matches[0])
|
||||||
|
|
||||||
|
|
||||||
|
def get_cube_power(line: str) -> int:
|
||||||
|
|
||||||
|
cube_count = {"red": 0, "green": 0, "blue": 0}
|
||||||
|
|
||||||
|
regex = r"(\d+) (\w+)"
|
||||||
|
matches = re.findall(regex, line)
|
||||||
|
|
||||||
|
for m in matches:
|
||||||
|
color = m[1]
|
||||||
|
count = int(m[0])
|
||||||
|
if count > cube_count[color]:
|
||||||
|
cube_count[color] = count
|
||||||
|
|
||||||
|
product = 1
|
||||||
|
for value in cube_count.values():
|
||||||
|
product *= value
|
||||||
|
|
||||||
|
return product
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lines = readinput()
|
||||||
|
|
||||||
|
max_cubes = {
|
||||||
|
"red": 12,
|
||||||
|
"green": 13,
|
||||||
|
"blue": 14,
|
||||||
|
}
|
||||||
|
|
||||||
|
# part 1
|
||||||
|
count = 0
|
||||||
|
for line in lines:
|
||||||
|
if is_playable(line, max_cubes):
|
||||||
|
count += get_game_nr(line)
|
||||||
|
print("Playable games: %d" % count)
|
||||||
|
|
||||||
|
# part 2
|
||||||
|
count = 0
|
||||||
|
for line in lines:
|
||||||
|
count += get_cube_power(line)
|
||||||
|
print("Set power sum: %d" % count)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue