mirror of
https://github.com/zebrajr/coding-challenges.git
synced 2025-12-06 00:20:19 +01:00
add solver for part b
This commit is contained in:
parent
cb0f783097
commit
67fd0ebc9e
|
|
@ -8,7 +8,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var puzzle_file_name = "puzzle_sample.txt"
|
||||
var puzzle_file_name = "puzzle_input.txt"
|
||||
|
||||
var pages [][]int
|
||||
var page []int
|
||||
|
|
@ -88,10 +88,59 @@ func check_pages_for_problem_part_1(){
|
|||
fmt.Println("Final Sum of Valid Middle Values: ", sum_of_valid_middle_values)
|
||||
}
|
||||
|
||||
func fix_incorrect_pages(){
|
||||
last_order := ""
|
||||
sum_of_invalid_middle_values := 0
|
||||
|
||||
for _, page := range pages{
|
||||
last_order = ""
|
||||
was_page_bad := false
|
||||
//fmt.Println("Trying Page: ", idx, page)
|
||||
for i := 0 ; i < len(page); i++{
|
||||
order_as_string := strconv.Itoa(page[i])
|
||||
rule_to_check_for := order_as_string + last_order
|
||||
_, ok := rule_book[rule_to_check_for]
|
||||
if ok {
|
||||
current_index := i
|
||||
old_index := i - 1
|
||||
|
||||
if old_index < 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// fmt.Println("Checking Rule: ", rule_to_check_for, " - Have: ", page[current_index], page[old_index], " - Should have: ", correct_before_value, correct_after_value)
|
||||
new_current_value := page[old_index]
|
||||
new_old_value := page[current_index]
|
||||
was_page_bad = true
|
||||
//fmt.Println("Switching values for Page:", page, " - Switching: ", new_current_value, new_old_value)
|
||||
page[current_index] = new_current_value
|
||||
page[old_index] = new_old_value
|
||||
// reset the index on the for loop
|
||||
i = 0
|
||||
last_order = order_as_string
|
||||
}
|
||||
if !ok{
|
||||
last_order = order_as_string
|
||||
}
|
||||
}
|
||||
|
||||
if was_page_bad {
|
||||
slice_length := len(page)
|
||||
middle_index := slice_length / 2
|
||||
middle_value := page[middle_index]
|
||||
sum_of_invalid_middle_values += middle_value
|
||||
//fmt.Println("Final Order for Page: ", idx, page)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Final Sum on Invalid Middle Values: ", sum_of_invalid_middle_values)
|
||||
}
|
||||
|
||||
func main(){
|
||||
load_data_from_file(puzzle_file_name)
|
||||
// fmt.Println(pages)
|
||||
// fmt.Println(rule_book)
|
||||
|
||||
check_pages_for_problem_part_1()
|
||||
fix_incorrect_pages()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user