-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.go
71 lines (63 loc) · 1.28 KB
/
day25.go
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
69
70
71
package main
import (
"fmt"
)
func reverse(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
}
return string(chars)
}
var StoN = map[byte]int{'=': -2, '-': -1, '0': 0, '1': 1, '2': 2}
var NtoS = map[int]byte{-2: '=', -1: '-', 0: '0', 1: '1', 2: '2'}
func add_digit(d1, d2 byte, ca int) (string, int) {
n1, n2 := StoN[d1], StoN[d2]
c := 0
d_str := string("")
switch t := n1 + n2 + ca; t {
case 3, 4, 5:
c = 1
d_str = string(NtoS[t-5])
case -3, -4, -5:
c = -1
d_str = string(NtoS[t+5])
default:
c = 0
d_str = string(NtoS[t])
}
return d_str, c
}
func add(s1, s2 string) string {
res := string("")
carry := 0
i := 0
for i = 0; i < Min(len(s1), len(s2)); i++ {
d_str, c := add_digit(s1[i], s2[i], carry)
res = res + d_str
carry = c
}
if len(s2) > len(s1) {
s1, s2 = s2, s1
}
for i < len(s1) {
d_str, c := add_digit(s1[i], '0', carry)
res = res + d_str
carry = c
i++
}
if carry != 0 {
res = res + string(NtoS[carry])
}
return res
}
func main() {
lines := LinesFromFile("input25.txt")
part_a := "0"
nums := make([]string, len(lines))
for i, ln := range lines {
nums[i] = reverse(ln)
part_a = add(part_a, nums[i])
}
fmt.Println("Part A:", reverse(part_a))
}