@@ -10,6 +10,24 @@ import (
1010 "time"
1111)
1212
13+ func doOp (row []int , op string ) int {
14+ switch op {
15+ case "*" :
16+ out := 1
17+ for _ , v := range row {
18+ out *= v
19+ }
20+ return out
21+ case "+" :
22+ out := 0
23+ for _ , v := range row {
24+ out += v
25+ }
26+ return out
27+ }
28+ return 0
29+ }
30+
1331func day6 (file string ) (part1 , part2 int ) {
1432 f , err := os .Open (file )
1533 if err != nil {
@@ -19,11 +37,6 @@ func day6(file string) (part1, part2 int) {
1937 scanner := bufio .NewScanner (f )
2038
2139 cells := [][]int {}
22- cells2 := [][]int {}
23-
24- // ops := map[byte]bool{
25- // '*':true, '+':true,
26- // }
2740 ops := []string {}
2841 grid := [][]byte {}
2942 for scanner .Scan () {
@@ -61,29 +74,14 @@ func day6(file string) (part1, part2 int) {
6174 opCount := len (ops ) - 1
6275a:
6376 for y := len (grid [0 ]) - 1 ; y >= 0 ; y -- {
64- s := ""
77+ i := 0
6578 for x := range len (grid ) - 1 {
6679 if grid [x ][y ] != ' ' {
67- s += string (grid [x ][y ])
80+ i = i * 10 + int (grid [x ][y ]- '0' )
6881 }
6982 }
70- i , _ := strconv .Atoi (s )
7183 if i == 0 {
72- if ops [opCount ] == "*" {
73- out := 1
74- for _ , j := range row2 {
75- out *= j
76- }
77- // fmt.Println("part2", out)
78- part2 += out
79- } else {
80- out := 0
81- for _ , j := range row2 {
82- out += j
83- }
84- // fmt.Println("part2", out)
85- part2 += out
86- }
84+ part2 += doOp (row2 , ops [opCount ])
8785 row2 = []int {}
8886 opCount --
8987 if opCount < 0 {
9391 row2 = append (row2 , i )
9492 }
9593 }
96- if ops [opCount ] == "*" {
97- out := 1
98- for _ , j := range row2 {
99- out *= j
100- }
101- // fmt.Println("part2", out)
102- part2 += out
103- } else {
104- out := 0
105- for _ , j := range row2 {
106- out += j
107- }
108- // fmt.Println("part2", out)
109- part2 += out
110- }
111- _ = cells2
112-
113- // log.Printf("cells %+v\n", cells)
114- // log.Printf("%#v\n", grid)
94+ part2 += doOp (row2 , ops [opCount ])
11595
11696 for col , op := range ops {
11797 a := 0
11898 if op == "*" {
11999 a = 1
120100 }
121101 for i := range cells {
122- // log.Println(i, col, cells[i][col])
123102 if op == "*" {
124103 a *= cells [i ][col ]
125104 } else {
126105 a += cells [i ][col ]
127106 }
128107 }
129- // fmt.Println(a)
130108 part1 += a
131109 }
132110
0 commit comments