-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3.go
More file actions
68 lines (65 loc) · 1.19 KB
/
Copy pathd3.go
File metadata and controls
68 lines (65 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"strconv"
"strings"
)
func (*methods) D3P1(input string) string {
banks := strings.Split(input, "\n")
var total int
for _, bank := range banks {
var maxj, secj int
var maxidx, secidx int
for i, cell := range bank {
j := int(cell) - 48
if j > maxj && i < len(bank)-1 {
maxj = j
maxidx = i
}
}
for i := maxidx + 1; i < len(bank); i++ {
j := int(bank[i]) - 48
if j > secj {
secj = j
secidx = i
}
}
if maxidx > secidx {
total += secj*10 + maxj
} else {
total += maxj*10 + secj
}
}
return strconv.Itoa(total)
}
func (*methods) D3P2(input string) string {
banks := strings.Split(input, "\n")
var total int64
for _, bank := range banks {
var on []int
maxCells := 12
var startidx int
for {
var maxj int
stopidx := len(bank) - maxCells
for i := startidx; i <= stopidx; i++ {
j := int(bank[i]) - 48
if j > maxj {
maxj = j
startidx = i + 1
}
}
on = append(on, maxj)
maxCells--
if maxCells == 0 {
break
}
}
var jS string
for _, j := range on {
jS += strconv.Itoa(j)
}
jolts, _ := strconv.ParseInt(jS, 10, 64)
total += jolts
}
return strconv.FormatInt(total, 10)
}