solve_1.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 1")
  11. f,_:=os.Open("6/input")
  12. defer f.Close()
  13. scanner:=bufio.NewScanner(f)
  14. var grid [][]string
  15. for scanner.Scan(){
  16. line:= scanner.Text()
  17. var operands []string
  18. for op := range strings.SplitSeq(line," ") {
  19. if (len(op) > 0){
  20. operands = append(operands, op)
  21. }
  22. }
  23. grid = append(grid, operands)
  24. }
  25. operators := grid[len(grid)-1]
  26. operands_matrix := grid[:len(grid)-1]
  27. n := len(operands_matrix)
  28. m := len(operands_matrix[0])
  29. transpose_operands_matrix := make([][]string, m)
  30. for j:= range m{
  31. transpose_operands_matrix[j] = make([]string, n)
  32. for i:= range n{
  33. transpose_operands_matrix[j][i] = operands_matrix[i][j]
  34. }
  35. }
  36. res := int64(0)
  37. for i,operation:= range transpose_operands_matrix {
  38. res += calculate(operators[i], operation)
  39. }
  40. fmt.Println(res)
  41. }
  42. func calculate(operator string, operands []string) int64{
  43. var res int64
  44. switch(operator){
  45. case "*":
  46. res = int64(1)
  47. for _,op := range operands{
  48. op_toi,_ := strconv.ParseInt(op,10,64)
  49. res *= op_toi
  50. }
  51. case "+":
  52. res = int64(0)
  53. for _,op := range operands{
  54. op_toi,_ := strconv.ParseInt(op,10,64)
  55. res += op_toi
  56. }
  57. }
  58. return res
  59. }