prettify and lint optimization
This commit is contained in:
@@ -8,20 +8,23 @@ def parse_game_data(line):
|
||||
Returns:
|
||||
int, dict: Game ID and a dictionary with color counts.
|
||||
"""
|
||||
parts = line.split(': ')
|
||||
game_id = int(parts[0].split(' ')[1])
|
||||
color_counts = {'red': 0, 'green': 0, 'blue': 0}
|
||||
|
||||
subsets = parts[1].split('; ')
|
||||
parts = line.split(": ")
|
||||
game_id = int(parts[0].split(" ")[1])
|
||||
color_counts = {"red": 0, "green": 0, "blue": 0}
|
||||
|
||||
subsets = parts[1].split("; ")
|
||||
for subset in subsets:
|
||||
colors = subset.split(', ')
|
||||
colors = subset.split(", ")
|
||||
for color in colors:
|
||||
count, color_name = color.split(' ')
|
||||
color_name = color_name.strip() # Remove any trailing whitespace or newline characters
|
||||
count, color_name = color.split(" ")
|
||||
color_name = (
|
||||
color_name.strip()
|
||||
) # Remove any trailing whitespace or newline characters
|
||||
color_counts[color_name] = max(color_counts[color_name], int(count))
|
||||
|
||||
|
||||
return game_id, color_counts
|
||||
|
||||
|
||||
def is_game_possible(game_data, red_cubes, green_cubes, blue_cubes):
|
||||
"""
|
||||
Determines if a game is possible given the number of each color of cubes.
|
||||
@@ -35,9 +38,12 @@ def is_game_possible(game_data, red_cubes, green_cubes, blue_cubes):
|
||||
Returns:
|
||||
bool: True if the game is possible, False otherwise.
|
||||
"""
|
||||
return (game_data['red'] <= red_cubes and
|
||||
game_data['green'] <= green_cubes and
|
||||
game_data['blue'] <= blue_cubes)
|
||||
return (
|
||||
game_data["red"] <= red_cubes
|
||||
and game_data["green"] <= green_cubes
|
||||
and game_data["blue"] <= blue_cubes
|
||||
)
|
||||
|
||||
|
||||
def process_games(file_path, red_cubes, green_cubes, blue_cubes):
|
||||
"""
|
||||
@@ -52,7 +58,7 @@ def process_games(file_path, red_cubes, green_cubes, blue_cubes):
|
||||
Returns:
|
||||
int: Sum of IDs of possible games.
|
||||
"""
|
||||
with open(file_path, 'r') as file:
|
||||
with open(file_path, "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
sum_of_ids = 0
|
||||
@@ -63,17 +69,19 @@ def process_games(file_path, red_cubes, green_cubes, blue_cubes):
|
||||
|
||||
return sum_of_ids
|
||||
|
||||
|
||||
def test():
|
||||
print("start testing")
|
||||
# Test the function
|
||||
result = process_games('../test.txt', 12, 13, 14)
|
||||
result = process_games("../test.txt", 12, 13, 14)
|
||||
# Assertion for testing
|
||||
assert result == 8, f"Expected sum of IDs to be 8, but got {result}"
|
||||
print(f"Test Passed: Sum of IDs for possible games is {result}\n")
|
||||
|
||||
|
||||
# Run the test
|
||||
test()
|
||||
|
||||
# Process the actual input file
|
||||
result = process_games('../input.txt', 12, 13, 14)
|
||||
result = process_games("../input.txt", 12, 13, 14)
|
||||
print(f"From input.txt: Sum of IDs for possible games is {result}")
|
||||
|
||||
@@ -8,20 +8,23 @@ def parse_game_data(line):
|
||||
Returns:
|
||||
int, dict: Game ID and a dictionary with color counts.
|
||||
"""
|
||||
parts = line.split(': ')
|
||||
game_id = int(parts[0].split(' ')[1])
|
||||
color_counts = {'red': 0, 'green': 0, 'blue': 0}
|
||||
|
||||
subsets = parts[1].split('; ')
|
||||
parts = line.split(": ")
|
||||
game_id = int(parts[0].split(" ")[1])
|
||||
color_counts = {"red": 0, "green": 0, "blue": 0}
|
||||
|
||||
subsets = parts[1].split("; ")
|
||||
for subset in subsets:
|
||||
colors = subset.split(', ')
|
||||
colors = subset.split(", ")
|
||||
for color in colors:
|
||||
count, color_name = color.split(' ')
|
||||
color_name = color_name.strip() # Remove any trailing whitespace or newline characters
|
||||
count, color_name = color.split(" ")
|
||||
color_name = (
|
||||
color_name.strip()
|
||||
) # Remove any trailing whitespace or newline characters
|
||||
color_counts[color_name] = max(color_counts[color_name], int(count))
|
||||
|
||||
|
||||
return game_id, color_counts
|
||||
|
||||
|
||||
def calculate_power_of_set(game_data):
|
||||
"""
|
||||
Calculates the power of the cube set.
|
||||
@@ -32,7 +35,8 @@ def calculate_power_of_set(game_data):
|
||||
Returns:
|
||||
int: The power of the cube set.
|
||||
"""
|
||||
return game_data['red'] * game_data['green'] * game_data['blue']
|
||||
return game_data["red"] * game_data["green"] * game_data["blue"]
|
||||
|
||||
|
||||
def process_games(file_path):
|
||||
"""
|
||||
@@ -44,7 +48,7 @@ def process_games(file_path):
|
||||
Returns:
|
||||
int: Sum of the power of the minimum cube sets.
|
||||
"""
|
||||
with open(file_path, 'r') as file:
|
||||
with open(file_path, "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
sum_of_powers = 0
|
||||
@@ -55,18 +59,22 @@ def process_games(file_path):
|
||||
|
||||
return sum_of_powers
|
||||
|
||||
|
||||
def test():
|
||||
print("start testing")
|
||||
sum_of_powers = process_games('../test.txt')
|
||||
sum_of_powers = process_games("../test.txt")
|
||||
|
||||
# Assertion for testing
|
||||
assert sum_of_powers == 2286, f"Expected sum of powers to be 2286, but got {sum_of_powers}"
|
||||
assert (
|
||||
sum_of_powers == 2286
|
||||
), f"Expected sum of powers to be 2286, but got {sum_of_powers}"
|
||||
|
||||
print(f"Test Passed: Sum of powers for the minimum cube sets is {sum_of_powers}\n")
|
||||
|
||||
|
||||
# Run the test
|
||||
test()
|
||||
|
||||
# Process the actual input file
|
||||
sum_of_powers = process_games('../input.txt')
|
||||
sum_of_powers = process_games("../input.txt")
|
||||
print(f"From input.txt: Sum of powers for the minimum cube sets is {sum_of_powers}")
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn parse_game_data(line: &str) -> (i32, HashMap<String, i32>) {
|
||||
let parts: Vec<&str> = line.split(": ").collect();
|
||||
let game_id = parts[0].split_whitespace().nth(1).unwrap().parse::<i32>().unwrap();
|
||||
let game_id = parts[0]
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.parse::<i32>()
|
||||
.unwrap();
|
||||
let mut color_counts = HashMap::new();
|
||||
color_counts.insert("red".to_string(), 0);
|
||||
color_counts.insert("green".to_string(), 0);
|
||||
@@ -27,7 +32,9 @@ fn parse_game_data(line: &str) -> (i32, HashMap<String, i32>) {
|
||||
}
|
||||
|
||||
fn calculate_power_of_set(game_data: &HashMap<String, i32>) -> i32 {
|
||||
game_data.get("red").unwrap_or(&0) * game_data.get("green").unwrap_or(&0) * game_data.get("blue").unwrap_or(&0)
|
||||
game_data.get("red").unwrap_or(&0)
|
||||
* game_data.get("green").unwrap_or(&0)
|
||||
* game_data.get("blue").unwrap_or(&0)
|
||||
}
|
||||
|
||||
fn process_games(file_path: &Path) -> i32 {
|
||||
@@ -48,10 +55,20 @@ fn process_games(file_path: &Path) -> i32 {
|
||||
fn main() {
|
||||
// Test the function with test data
|
||||
let test_result = process_games(Path::new("../test.txt"));
|
||||
assert_eq!(test_result, 2286, "Test failed: expected sum of powers to be 2286, but got {}", test_result);
|
||||
println!("Test Passed: Sum of powers for the minimum cube sets is {}\n", test_result);
|
||||
assert_eq!(
|
||||
test_result, 2286,
|
||||
"Test failed: expected sum of powers to be 2286, but got {}",
|
||||
test_result
|
||||
);
|
||||
println!(
|
||||
"Test Passed: Sum of powers for the minimum cube sets is {}\n",
|
||||
test_result
|
||||
);
|
||||
|
||||
// Process the actual input file
|
||||
let result = process_games(Path::new("../input.txt"));
|
||||
println!("From input.txt: Sum of powers for the minimum cube sets is {}", result);
|
||||
println!(
|
||||
"From input.txt: Sum of powers for the minimum cube sets is {}",
|
||||
result
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user