solve_2.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. func main(){
  10. fmt.Println("Advent of Code 2025 - Day 6 - Part 2")
  11. f,_:=os.Open("6/input")
  12. defer f.Close()
  13. scanner:=bufio.NewScanner(f)
  14. var input []string
  15. var grid [][]string
  16. for scanner.Scan(){
  17. line:= scanner.Text()
  18. input = append(input, line)
  19. }
  20. grid = define_columns(input)
  21. operators := extract_operators(input[len(input)-1])
  22. n := len(grid)
  23. m := len(grid[0])
  24. // Calculate the transpose
  25. transpose_operands_matrix := make([][]string, m)
  26. for j:= range m{
  27. transpose_operands_matrix[j] = make([]string, n)
  28. for i:= range n{
  29. transpose_operands_matrix[j][i] = grid[i][j]
  30. }
  31. }
  32. // Calculate with the new digit disposition
  33. new_operands_matrix := make([][]string, m)
  34. for j:= range m{
  35. new_operands_matrix[j] = make([]string,n)
  36. for range n{
  37. new_operands_matrix[j] = align_digits(transpose_operands_matrix[j])
  38. }
  39. }
  40. res := int64(0)
  41. for i,operation:= range new_operands_matrix {
  42. operator := operators[i]
  43. res += calculate(operator, operation)
  44. }
  45. fmt.Println(res)
  46. }
  47. func define_columns(input []string) [][]string{
  48. var res [][]string
  49. operands := input[len(input)-1]
  50. var columns_widths []int
  51. col_width := 0
  52. first := true
  53. for i := range operands {
  54. if operands[i] != ' '{
  55. if (first){
  56. first = false
  57. } else {
  58. columns_widths = append(columns_widths, col_width)
  59. col_width = 0
  60. }
  61. } else {
  62. col_width++
  63. }
  64. if (i==len(operands)-1){
  65. columns_widths = append(columns_widths, col_width+1)
  66. }
  67. }
  68. for _,line := range input[:len(input)-1] {
  69. operation := split_line(line, columns_widths)
  70. res = append(res, operation)
  71. }
  72. return res
  73. }
  74. func split_line(line string, column_widths []int) []string{
  75. var res []string
  76. offset := 0
  77. for _,w := range column_widths {
  78. res = append(res, line[offset:offset+w])
  79. offset += w+1
  80. }
  81. return res
  82. }
  83. func align_digits(operands []string) []string {
  84. var res []string
  85. n := len(operands[0])
  86. for i:=range n {
  87. nop := ""
  88. for _,op := range operands{
  89. nop += string(op[i])
  90. }
  91. res = append(res, nop)
  92. }
  93. return res
  94. }
  95. func extract_operators(ops string) []string{
  96. var res []string
  97. for op := range strings.SplitSeq(ops," ") {
  98. if (len(op) > 0){
  99. res = append(res, op)
  100. }
  101. }
  102. return res
  103. }
  104. func calculate(operator string, operands []string) int64{
  105. var res int64
  106. switch(operator){
  107. case "*":
  108. res = int64(1)
  109. for _,op := range operands{
  110. op = strings.TrimSpace(op)
  111. op_toi,_ := strconv.ParseInt(op,10,64)
  112. res *= op_toi
  113. }
  114. case "+":
  115. res = int64(0)
  116. for _,op := range operands{
  117. op = strings.TrimSpace(op)
  118. op_toi,_ := strconv.ParseInt(op,10,64)
  119. res += op_toi
  120. }
  121. }
  122. return res
  123. }