|
|
@@ -0,0 +1,136 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "bufio"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+func main(){
|
|
|
+ fmt.Println("Advent of Code 2025 - Day 6 - Part 2")
|
|
|
+ f,_:=os.Open("6/input")
|
|
|
+ defer f.Close()
|
|
|
+ scanner:=bufio.NewScanner(f)
|
|
|
+ var input []string
|
|
|
+ var grid [][]string
|
|
|
+ for scanner.Scan(){
|
|
|
+ line:= scanner.Text()
|
|
|
+ input = append(input, line)
|
|
|
+ }
|
|
|
+
|
|
|
+ grid = define_columns(input)
|
|
|
+ operators := extract_operators(input[len(input)-1])
|
|
|
+
|
|
|
+ n := len(grid)
|
|
|
+ m := len(grid[0])
|
|
|
+
|
|
|
+ // Calculate the transpose
|
|
|
+ transpose_operands_matrix := make([][]string, m)
|
|
|
+ for j:= range m{
|
|
|
+ transpose_operands_matrix[j] = make([]string, n)
|
|
|
+ for i:= range n{
|
|
|
+ transpose_operands_matrix[j][i] = grid[i][j]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Calculate with the new digit disposition
|
|
|
+ new_operands_matrix := make([][]string, m)
|
|
|
+ for j:= range m{
|
|
|
+ new_operands_matrix[j] = make([]string,n)
|
|
|
+ for range n{
|
|
|
+ new_operands_matrix[j] = align_digits(transpose_operands_matrix[j])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ res := int64(0)
|
|
|
+ for i,operation:= range new_operands_matrix {
|
|
|
+ operator := operators[i]
|
|
|
+ res += calculate(operator, operation)
|
|
|
+ }
|
|
|
+ fmt.Println(res)
|
|
|
+}
|
|
|
+
|
|
|
+func define_columns(input []string) [][]string{
|
|
|
+ var res [][]string
|
|
|
+ operands := input[len(input)-1]
|
|
|
+ var columns_widths []int
|
|
|
+ col_width := 0
|
|
|
+ first := true
|
|
|
+ for i := range operands {
|
|
|
+ if operands[i] != ' '{
|
|
|
+ if (first){
|
|
|
+ first = false
|
|
|
+ } else {
|
|
|
+ columns_widths = append(columns_widths, col_width)
|
|
|
+ col_width = 0
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ col_width++
|
|
|
+ }
|
|
|
+ if (i==len(operands)-1){
|
|
|
+ columns_widths = append(columns_widths, col_width+1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _,line := range input[:len(input)-1] {
|
|
|
+ operation := split_line(line, columns_widths)
|
|
|
+ res = append(res, operation)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func split_line(line string, column_widths []int) []string{
|
|
|
+ var res []string
|
|
|
+ offset := 0
|
|
|
+ for _,w := range column_widths {
|
|
|
+ res = append(res, line[offset:offset+w])
|
|
|
+ offset += w+1
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func align_digits(operands []string) []string {
|
|
|
+ var res []string
|
|
|
+ n := len(operands[0])
|
|
|
+ for i:=range n {
|
|
|
+ nop := ""
|
|
|
+ for _,op := range operands{
|
|
|
+ nop += string(op[i])
|
|
|
+ }
|
|
|
+ res = append(res, nop)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func extract_operators(ops string) []string{
|
|
|
+ var res []string
|
|
|
+ for op := range strings.SplitSeq(ops," ") {
|
|
|
+ if (len(op) > 0){
|
|
|
+ res = append(res, op)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func calculate(operator string, operands []string) int64{
|
|
|
+ var res int64
|
|
|
+ switch(operator){
|
|
|
+ case "*":
|
|
|
+ res = int64(1)
|
|
|
+ for _,op := range operands{
|
|
|
+ op = strings.TrimSpace(op)
|
|
|
+ op_toi,_ := strconv.ParseInt(op,10,64)
|
|
|
+ res *= op_toi
|
|
|
+ }
|
|
|
+ case "+":
|
|
|
+ res = int64(0)
|
|
|
+ for _,op := range operands{
|
|
|
+ op = strings.TrimSpace(op)
|
|
|
+ op_toi,_ := strconv.ParseInt(op,10,64)
|
|
|
+ res += op_toi
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|