mirror of
https://github.com/zebrajr/coding-challenges.git
synced 2025-12-06 12:20:53 +01:00
add day 01, part b
This commit is contained in:
parent
9b24751f70
commit
6fcb639f28
|
|
@ -40,3 +40,6 @@ So, for these example lists, the similarity score at the end of this process is
|
||||||
|
|
||||||
Once again consider your left and right lists. What is their similarity score?
|
Once again consider your left and right lists. What is their similarity score?
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
Puzzle answer is 24941624.
|
||||||
|
|
@ -7,7 +7,8 @@ import (
|
||||||
utils "advent_of_code/utils"
|
utils "advent_of_code/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const input_file_name = "puzzle_input"
|
const INPUT_FILE_NAME = "puzzle_input"
|
||||||
|
const FULL_VERBOSE = false
|
||||||
|
|
||||||
// Input is 2 arrays of lists of equal size
|
// Input is 2 arrays of lists of equal size
|
||||||
// Output is an array with each position distance AND the total of the distances
|
// Output is an array with each position distance AND the total of the distances
|
||||||
|
|
@ -26,15 +27,47 @@ func calculate_total_distance_between_lists(
|
||||||
difference = difference * -1
|
difference = difference * -1
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(list_a[index], list_b[index], difference)
|
if FULL_VERBOSE {
|
||||||
|
fmt.Println(list_a[index], list_b[index], difference)
|
||||||
|
}
|
||||||
far_apart_list = append(far_apart_list, difference)
|
far_apart_list = append(far_apart_list, difference)
|
||||||
total_far_apart += difference
|
total_far_apart += difference
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Total Far Apart:", total_far_apart)
|
||||||
return total_far_apart
|
return total_far_apart
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Outputs a map of [int keys] to int values
|
||||||
|
func create_occurence_map_from_list(
|
||||||
|
values_list []int,
|
||||||
|
) map[int]int {
|
||||||
|
occurence_map := make(map[int]int)
|
||||||
|
for _, number := range values_list {
|
||||||
|
occurence_map[number]++
|
||||||
|
}
|
||||||
|
|
||||||
|
return occurence_map
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculate_similarity_score(
|
||||||
|
list_a []int,
|
||||||
|
similarity_score_map map[int]int,
|
||||||
|
) {
|
||||||
|
|
||||||
|
total_similarity_score := 0
|
||||||
|
for _, element := range list_a {
|
||||||
|
calculated_similarity_score := element * similarity_score_map[element]
|
||||||
|
if FULL_VERBOSE {
|
||||||
|
fmt.Println(calculated_similarity_score)
|
||||||
|
}
|
||||||
|
total_similarity_score += calculated_similarity_score
|
||||||
|
}
|
||||||
|
fmt.Println("Total Similarity Score:", total_similarity_score)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
input_file := utils.Load_input_file(input_file_name)
|
input_file := utils.Load_input_file(INPUT_FILE_NAME)
|
||||||
// Ensure the file is closed afterwards to prevent resource leaks
|
// Ensure the file is closed afterwards to prevent resource leaks
|
||||||
defer input_file.Close()
|
defer input_file.Close()
|
||||||
|
|
||||||
|
|
@ -43,11 +76,12 @@ func main() {
|
||||||
sort.Ints(list_a)
|
sort.Ints(list_a)
|
||||||
sort.Ints(list_b)
|
sort.Ints(list_b)
|
||||||
|
|
||||||
total_far_apart := calculate_total_distance_between_lists(
|
calculate_total_distance_between_lists(
|
||||||
list_a,
|
list_a,
|
||||||
list_b,
|
list_b,
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt.Println("Total Far Apart:", total_far_apart)
|
occurence_map := create_occurence_map_from_list(list_b)
|
||||||
|
calculate_similarity_score(list_a, occurence_map)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user