lint and analysis
This commit is contained in:
@@ -11,7 +11,7 @@ function processSteps(steps) {
|
||||
const focalLengths = new Map();
|
||||
|
||||
steps.forEach(step => {
|
||||
const [_, label, operation, focalLength] = step.match(/([a-z]+)([=-])(\d)?/);
|
||||
const [, label, operation, focalLength] = step.match(/([a-z]+)([=-])(\d)?/);
|
||||
const hashed = hashLabel(label);
|
||||
const destination = boxes[hashed];
|
||||
|
||||
|
||||
@@ -7,12 +7,13 @@ def hash_algorithm(step):
|
||||
current_value %= 256 # Modulo 256
|
||||
return current_value
|
||||
|
||||
|
||||
def process_file(file_path):
|
||||
"""Process each step in the file and return the sum of HASH values."""
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
data = file.read().replace('\n', '') # Remove newline characters
|
||||
steps = data.split(',') # Split steps by comma
|
||||
with open(file_path, "r") as file:
|
||||
data = file.read().replace("\n", "") # Remove newline characters
|
||||
steps = data.split(",") # Split steps by comma
|
||||
print(f"Processing {len(steps)} steps from {file_path}")
|
||||
|
||||
total_sum = 0
|
||||
@@ -27,12 +28,14 @@ def process_file(file_path):
|
||||
except Exception as e:
|
||||
print(f"Unexpected error occurred: {e}")
|
||||
|
||||
|
||||
def test_algorithm():
|
||||
"""Test the algorithm with the test file."""
|
||||
test_result = process_file("../test.txt")
|
||||
print(f"Test Result: {test_result}")
|
||||
assert test_result == 1320, "Test failed. Expected result is 1320."
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to run the HASH algorithm on the input file."""
|
||||
try:
|
||||
@@ -48,5 +51,6 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"An error occurred in main: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def hash_label(label):
|
||||
"""Compute hash value for a given label."""
|
||||
value = 0
|
||||
@@ -8,11 +9,12 @@ def hash_label(label):
|
||||
value = (value + ord(char)) * 17 % 256
|
||||
return value
|
||||
|
||||
|
||||
def process_file(file_path):
|
||||
"""Process the file and return the total focusing power."""
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
line = file.read().replace('\n', '')
|
||||
with open(file_path, "r") as file:
|
||||
line = file.read().replace("\n", "")
|
||||
|
||||
boxes = defaultdict(list)
|
||||
focal_lengths = {}
|
||||
@@ -44,11 +46,15 @@ def process_file(file_path):
|
||||
except Exception as e:
|
||||
print(f"Unexpected error occurred: {e}")
|
||||
|
||||
|
||||
def test_algorithm():
|
||||
"""Test the algorithm with the test file."""
|
||||
test_result = process_file("../test.txt")
|
||||
print(f"Test Result: {test_result}")
|
||||
assert test_result == 145, f"Test failed. Expected result is 145, got {test_result}."
|
||||
assert (
|
||||
test_result == 145
|
||||
), f"Test failed. Expected result is 145, got {test_result}."
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to run the algorithm on the input file."""
|
||||
@@ -65,5 +71,6 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"An error occurred in main: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -2,7 +2,9 @@ use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
fn hash_label(label: &str) -> usize {
|
||||
label.chars().fold(0, |acc, c| (acc + (c as usize)) * 17 % 256)
|
||||
label
|
||||
.chars()
|
||||
.fold(0, |acc, c| (acc + (c as usize)) * 17 % 256)
|
||||
}
|
||||
|
||||
fn process_steps(steps: Vec<&str>) -> usize {
|
||||
@@ -22,11 +24,11 @@ fn process_steps(steps: Vec<&str>) -> usize {
|
||||
boxes[hashed].push(label.to_string());
|
||||
}
|
||||
focal_lengths.insert(label.to_string(), focal_length);
|
||||
},
|
||||
}
|
||||
'-' => {
|
||||
boxes[hashed].retain(|l| l != label);
|
||||
focal_lengths.remove(label);
|
||||
},
|
||||
}
|
||||
_ => panic!("Invalid operation character: {}", operation),
|
||||
}
|
||||
}
|
||||
@@ -39,9 +41,14 @@ fn process_steps(steps: Vec<&str>) -> usize {
|
||||
}
|
||||
|
||||
fn parse_step(step: &str) -> (&str, char, &str) {
|
||||
let operation_index = step.find(|c: char| c == '=' || c == '-').expect("Invalid step format");
|
||||
let operation_index = step
|
||||
.find(|c: char| c == '=' || c == '-')
|
||||
.expect("Invalid step format");
|
||||
let label = &step[..operation_index];
|
||||
let operation = step.chars().nth(operation_index).expect("Invalid step format");
|
||||
let operation = step
|
||||
.chars()
|
||||
.nth(operation_index)
|
||||
.expect("Invalid step format");
|
||||
let value = &step[operation_index + 1..];
|
||||
|
||||
(label, operation, value)
|
||||
@@ -57,7 +64,11 @@ fn test_algorithm() {
|
||||
let test_result = process_steps(test_steps);
|
||||
|
||||
println!("Test Result: {}", test_result);
|
||||
assert_eq!(test_result, 145, "Test failed. Expected result is 145, got {}.", test_result);
|
||||
assert_eq!(
|
||||
test_result, 145,
|
||||
"Test failed. Expected result is 145, got {}.",
|
||||
test_result
|
||||
);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user