-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fc4e583
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import typer | ||
import re | ||
|
||
def main(input_file: typer.FileText): | ||
input = input_file.read().split("\n") | ||
|
||
ans = 0 | ||
for i in input: | ||
res = re.findall("\d", i) | ||
if res == []: | ||
continue | ||
print(f"{i=} {res=}") | ||
num = int(f"{res[0]}{res[-1]}") | ||
print(f"{num=}") | ||
ans += num | ||
|
||
print(ans) | ||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import typer | ||
import re | ||
|
||
digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | ||
|
||
def main(input_file: typer.FileText): | ||
input = input_file.read().split("\n") | ||
|
||
ans = 0 | ||
for i in input: | ||
res = re.finditer(r"(?=(\d|one|two|three|four|five|six|seven|eight|nine))", i) | ||
res = [i.group(1) for i in res] | ||
if res == []: | ||
continue | ||
if res[0] in digits: | ||
res[0] = digits.index(res[0]) + 1 | ||
if res[-1] in digits: | ||
res[-1] = digits.index(res[-1]) + 1 | ||
print(f"{i=} {res=}") | ||
num = int(f"{res[0]}{res[-1]}") | ||
print(f"{num=}") | ||
ans += num | ||
|
||
print(ans) | ||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import typer | ||
import re | ||
from typing import TypedDict | ||
from enum import Enum | ||
|
||
|
||
class Set(TypedDict): | ||
red: int | ||
green: int | ||
blue: int | ||
|
||
|
||
class Game(TypedDict): | ||
game: int | ||
sets: list[Set] | ||
|
||
def is_possible(game: Game, bag: Set): | ||
for s in game["sets"]: | ||
if s["red"] > bag["red"] or s["green"] > bag["green"] or s["blue"] > bag["blue"]: | ||
return False | ||
return True | ||
|
||
def maximum(game: Game) -> Set: | ||
max: Set = {"red": 0, "green": 0, "blue": 0} | ||
for s in game["sets"]: | ||
if s["red"] > max["red"]: | ||
max["red"] = s["red"] | ||
if s["green"] > max["green"]: | ||
max["green"] = s["green"] | ||
if s["blue"] > max["blue"]: | ||
max["blue"] = s["blue"] | ||
return max | ||
|
||
def power(s: Set) -> int: | ||
return s["red"] * s["green"] * s["blue"] | ||
|
||
|
||
def main(input_file: typer.FileText): | ||
input = input_file.read().split("\n") | ||
bag: Set = {"red": 12, "green": 13, "blue": 14} | ||
|
||
games : list[Game] = [] | ||
for i in input: | ||
game = int(re.findall(r"\d+", i)[0]) | ||
details = i.split(":")[1] | ||
sets = [] | ||
for j in details.split(";"): | ||
s: Set = {"red": 0, "green": 0, "blue": 0} | ||
for k in j.split(","): | ||
num = int(re.findall(r"\d+", k)[0]) | ||
color = re.findall(r"red|green|blue", k)[0] | ||
print(f"{game=} {num=} {color=}") | ||
s[color] = num | ||
sets.append(s) | ||
games.append(Game(game=game, sets=sets)) | ||
print(f"{games=}") | ||
|
||
sum = 0 | ||
sum_power = 0 | ||
for g in games: | ||
if is_possible(g, bag): | ||
print(f"{g['game']=}") | ||
sum += g["game"] | ||
sum_power += power(maximum(g)) | ||
print(f"{sum=}") | ||
print(f"{sum_power=}") | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import typer | ||
import re | ||
from typing import TypedDict | ||
from enum import Enum | ||
|
||
|
||
class Number: | ||
pass | ||
|
||
|
||
class Symbol(TypedDict): | ||
row: int | ||
column: int | ||
value: str | ||
numbers_adjacent: list[Number] | ||
|
||
|
||
class Number(TypedDict): | ||
row: int | ||
column_start: int | ||
column_end: int | ||
value: int | ||
symbol_adjacent: list[Symbol] | ||
|
||
|
||
def main(input_file: typer.FileText): | ||
input = input_file.read().split("\n") | ||
|
||
symbols: list[Symbol] = [] | ||
numbers: list[Number] = [] | ||
|
||
for row_num, row in enumerate(input): | ||
row_numbers = re.finditer(r"\d+", row) | ||
|
||
# match all symbols except whitespace, dots, and numbers | ||
row_symbols = re.finditer(r"[^\s\d\.]", row) | ||
|
||
for match in row_symbols: | ||
symbols.append( | ||
{"row": row_num, "column": match.start(), "value": match.group(), "numbers_adjacent": []} | ||
) | ||
|
||
for match in row_numbers: | ||
numbers.append( | ||
{ | ||
"row": row_num, | ||
"column_start": match.start(), | ||
"column_end": match.end(), | ||
"value": int(match.group()), | ||
"symbol_adjacent": [], | ||
} | ||
) | ||
|
||
for number in numbers: | ||
for symbol in symbols: | ||
if ( | ||
symbol["row"] >= number["row"] - 1 | ||
and symbol["row"] <= number["row"] + 1 | ||
and symbol["column"] >= number["column_start"] - 1 | ||
and symbol["column"] <= number["column_end"] | ||
): | ||
number["symbol_adjacent"].append(symbol) | ||
symbol["numbers_adjacent"].append(number) | ||
|
||
# for number in numbers: | ||
# print(f"Number: {number['value']}") | ||
# print(f"Row: {number['row']}") | ||
# print(f"Column start: {number['column_start']}") | ||
# print(f"Column end: {number['column_end']}") | ||
# print(f"Symbols adjacent: {number['symbol_adjacent']}") | ||
# print("") | ||
|
||
partnumber_sum = 0 | ||
for number in numbers: | ||
if number["symbol_adjacent"]: | ||
partnumber_sum += number["value"] | ||
|
||
print(f"Partnumber sum: {partnumber_sum}") | ||
print(f"Num symbols: {len(symbols)}") | ||
unique_symbols = [] | ||
for symbol in symbols: | ||
if symbol["value"] not in unique_symbols: | ||
unique_symbols.append(symbol["value"]) | ||
print(f"Unique symbols: {unique_symbols}") | ||
print(f"Num unique symbols: {len(unique_symbols)}") | ||
print(f"Num numbers: {len(numbers)}") | ||
|
||
gear_ratio_sum = 0 | ||
for symbol in symbols: | ||
if symbol["value"] == "*" and len(symbol["numbers_adjacent"]) == 2: | ||
gear_ratio = ( | ||
symbol["numbers_adjacent"][0]["value"] | ||
* symbol["numbers_adjacent"][1]["value"] | ||
) | ||
gear_ratio_sum += gear_ratio | ||
# print( | ||
# f"Gear ratio of {symbol['numbers_adjacent'][0]['value']} {symbol['numbers_adjacent'][1]['value']}: {gear_ratio}" | ||
# ) | ||
|
||
print(f"Gear ratio sum: {gear_ratio_sum}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |