2024 day 5 part 2
This commit is contained in:
parent
a7f151d0c9
commit
7c3b4a93e2
@ -9,26 +9,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// type ListNode struct {
|
||||
// prev *ListNode
|
||||
// next *ListNode
|
||||
// num int
|
||||
// }
|
||||
//
|
||||
// type DllList struct {
|
||||
// first *ListNode
|
||||
// last *ListNode
|
||||
// }
|
||||
|
||||
// type Rule struct {
|
||||
// // prev *Rule
|
||||
// // next *Rule
|
||||
// isLeft bool
|
||||
// other int
|
||||
// // left int
|
||||
// // right int
|
||||
// }
|
||||
|
||||
var rules [][2]int
|
||||
var pages [][]int
|
||||
|
||||
@ -73,23 +53,58 @@ func main() {
|
||||
pages = append(pages, list)
|
||||
}
|
||||
}
|
||||
// for i, rule := range rules {
|
||||
// // fmt.Println(i, rule)
|
||||
// }
|
||||
|
||||
total := 0
|
||||
total1 := 0
|
||||
total2 := 0
|
||||
|
||||
for _, page := range pages {
|
||||
pageValid := checkPagesValid(rules, page)
|
||||
if pageValid {
|
||||
if !pageValid {
|
||||
// this is the ugliest solution ever, but it works!
|
||||
newpage := orderPage(rules, page)
|
||||
for x := 0; x < 100; x++ {
|
||||
newpage = orderPage(rules, page)
|
||||
}
|
||||
size := len(newpage)
|
||||
middle := ((size - 1) / 2)
|
||||
total2 += newpage[middle]
|
||||
} else {
|
||||
size := len(page)
|
||||
middle := ((size - 1) / 2)
|
||||
// fmt.Printf("page: %d\tisValid: %t middle: %d\n", page, pageValid, middle)
|
||||
total += page[middle]
|
||||
total1 += page[middle]
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("result part1:", total)
|
||||
fmt.Println("result part1:", total1)
|
||||
fmt.Println("result part2:", total2)
|
||||
}
|
||||
|
||||
func orderPage(rules [][2]int, page []int) []int {
|
||||
var newPage []int = page
|
||||
|
||||
for page_idx, num := range page {
|
||||
for _, rule := range rules {
|
||||
// check if any ints on the left of the current index match, if so it is invalid
|
||||
if rule[0] == num {
|
||||
// fmt.Println("found in left rule...", page_idx)
|
||||
for i := page_idx - 1; i >= 0; i-- {
|
||||
if page[i] == rule[1] {
|
||||
swap(&newPage, i, page_idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
// check if any ints on the right of the current index match, if so it is invalid
|
||||
if rule[1] == num {
|
||||
for i := page_idx; i < len(page); i++ {
|
||||
if page[i] == rule[0] {
|
||||
swap(&newPage, i, page_idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newPage
|
||||
}
|
||||
|
||||
func checkPagesValid(rules [][2]int, page []int) bool {
|
||||
@ -99,16 +114,29 @@ func checkPagesValid(rules [][2]int, page []int) bool {
|
||||
if rule[0] == num {
|
||||
// fmt.Println("found in left rule...", page_idx)
|
||||
for i := page_idx - 1; i >= 0; i-- {
|
||||
if page[i] == rule[1] { return false }
|
||||
if page[i] == rule[1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
// check if any ints on the right of the current index match, if so it is invalid
|
||||
if rule[1] == num {
|
||||
for i := page_idx; i < len(page); i++ {
|
||||
if page[i] == rule[0] { return false }
|
||||
if page[i] == rule[0] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func swap(arr *[]int, a, b int) {
|
||||
// Check if indices are within the bounds of the slice
|
||||
if a < 0 || b < 0 || a >= len(*arr) || b >= len(*arr) {
|
||||
panic("Index out of bounds")
|
||||
}
|
||||
|
||||
// Swap the elements at indices a and b
|
||||
(*arr)[a], (*arr)[b] = (*arr)[b], (*arr)[a]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user