Advertisement
xosski

Heaped list binary read/write

Dec 25th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "strconv"
  9. "strings"
  10. )
  11.  
  12. type Info struct {
  13. start, end int
  14. }
  15.  
  16. func main() {
  17. // Open the file
  18. f, err := os.Open("tinput.txt")
  19. if err != nil {
  20. log.Fatalf("Error opening file: %s", err)
  21. }
  22. defer f.Close()
  23.  
  24. // Read the entire file into memory
  25. bytes, err := io.ReadAll(f)
  26. if err != nil {
  27. log.Fatalf("Error reading file: %s", err)
  28. }
  29. input := string(bytes)
  30.  
  31. // Split the input by spaces to get the numbers
  32. numsStr := strings.Split(input, " ")
  33.  
  34. // Create a new linked list
  35. list := &LinkedList{}
  36. fmt.Println("Initial Input:", numsStr)
  37.  
  38. // Add numbers to the linked list
  39. for _, numStr := range numsStr {
  40. num, err := strconv.ParseUint(numStr, 10, 32)
  41. if err != nil {
  42. log.Fatalf("Error parsing number: %s", err)
  43. }
  44. list.AddTail(uint32(num))
  45. }
  46.  
  47. // Process the list for 10 iterations
  48. for i := 0; i < 10; i++ {
  49. current := list.head
  50. for current != nil {
  51. if current.value == 0 {
  52. current.value = 1
  53. } else if current.HasEvenDigits() {
  54. list.Replace(current)
  55. } else {
  56. current.value *= 2024
  57. }
  58. current = current.next
  59. }
  60. list.PrintLinkedList()
  61. }
  62. }
  63.  
  64. // Node represents an element in the linked list
  65. type Node struct {
  66. value uint32
  67. previous *Node
  68. next *Node
  69. }
  70.  
  71. // HasEvenDigits checks if the node's value has an even number of digits
  72. func (n *Node) HasEvenDigits() bool {
  73. str := fmt.Sprintf("%d", n.value)
  74. return len(str)%2 == 0
  75. }
  76.  
  77. // Replace splits the node's value into two new nodes
  78. func (n *Node) Replace() (*Node, *Node) {
  79. numStr := fmt.Sprintf("%d", n.value)
  80. mid := len(numStr) / 2
  81. num1, _ := strconv.ParseInt(numStr[:mid], 10, 64)
  82. num2, _ := strconv.ParseInt(numStr[mid:], 10, 64)
  83.  
  84. // Create new nodes for the split value
  85. newNode1 := &Node{value: uint32(num1), previous: n.previous}
  86. if n.previous != nil {
  87. n.previous.next = newNode1
  88. }
  89.  
  90. newNode2 := &Node{value: uint32(num2), next: n.next}
  91. if n.next != nil {
  92. n.next.previous = newNode2
  93. }
  94.  
  95. // Link the new nodes together
  96. newNode1.next = newNode2
  97. newNode2.previous = newNode1
  98. return newNode1, newNode2
  99. }
  100.  
  101. // LinkedList represents a doubly linked list
  102. type LinkedList struct {
  103. head *Node
  104. tail *Node
  105. length int
  106. }
  107.  
  108. // Replace handles replacing a node in the list
  109. func (ll *LinkedList) Replace(current *Node) {
  110. node, node2 := current.Replace()
  111. if ll.head == current {
  112. ll.head = node
  113. } else if ll.tail == current {
  114. ll.tail = node2
  115. }
  116. ll.length++
  117. }
  118.  
  119. // AddTail adds a new node with a given value to the end of the list
  120. func (ll *LinkedList) AddTail(value uint32) {
  121. node := &Node{value: value}
  122. if ll.tail == nil {
  123. ll.head = node
  124. ll.tail = node
  125. } else {
  126. ll.tail.next = node
  127. node.previous = ll.tail
  128. ll.tail = node
  129. }
  130. ll.length++
  131. }
  132.  
  133. // PrintLinkedList prints all the values in the linked list
  134. func (ll LinkedList) PrintLinkedList() {
  135. current := ll.head
  136. for current != nil {
  137. fmt.Printf("%d ", current.value)
  138. current = current.next
  139. }
  140. fmt.Println()
  141. }
  142.  
  143. // ==============================================
  144.  
  145. // BinaryHeap represents a min-heap
  146. type BinaryHeap struct {
  147. array []int
  148. size int
  149. }
  150.  
  151. // NewBinaryHeap creates a new BinaryHeap
  152. func NewBinaryHeap() *BinaryHeap {
  153. return &BinaryHeap{}
  154. }
  155.  
  156. // Insert adds a value to the heap
  157. func (h *BinaryHeap) Insert(value int) {
  158. h.array = append(h.array, value)
  159. h.size++
  160. h.percolateUp(h.size - 1)
  161. }
  162.  
  163. // Delete removes the smallest element from the heap
  164. func (h *BinaryHeap) Delete() int {
  165. if h.size == 0 {
  166. panic("Heap is empty")
  167. }
  168.  
  169. min := h.array[0]
  170. h.array[0] = h.array[h.size-1]
  171. h.array = h.array[:h.size-1]
  172. h.size--
  173. h.percolateDown(0)
  174.  
  175. return min
  176. }
  177.  
  178. // percolateUp moves an element up the heap to restore heap property
  179. func (h *BinaryHeap) percolateUp(index int) {
  180. parentIndex := (index - 1) / 2
  181.  
  182. for index > 0 && h.array[index] < h.array[parentIndex] {
  183. h.array[index], h.array[parentIndex] = h.array[parentIndex], h.array[index]
  184. index = parentIndex
  185. parentIndex = (index - 1) / 2
  186. }
  187. }
  188.  
  189. // percolateDown moves an element down the heap to restore heap property
  190. func (h *BinaryHeap) percolateDown(index int) {
  191. for {
  192. leftChildIndex := 2*index + 1
  193. rightChildIndex := 2*index + 2
  194. smallestIndex := index
  195.  
  196. if leftChildIndex < h.size && h.array[leftChildIndex] < h.array[smallestIndex] {
  197. smallestIndex = leftChildIndex
  198. }
  199.  
  200. if rightChildIndex < h.size && h.array[rightChildIndex] < h.array[smallestIndex] {
  201. smallestIndex = rightChildIndex
  202. }
  203.  
  204. if smallestIndex == index {
  205. break
  206. }
  207.  
  208. h.array[index], h.array[smallestIndex] = h.array[smallestIndex], h.array[index]
  209. index = smallestIndex
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement