-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (75 loc) · 1.41 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
func part1(input []string) {
defer utils.Timer("part1")()
total := "0"
for _, line := range input {
total = addSnafu(total, line)
}
fmt.Println(total)
}
func part2() {
defer utils.Timer("part2")()
}
func addSnafu(a string, b string) string {
// Pad the string
for len(a) != len(b) {
if len(a) < len(b) {
a = "0" + a
} else {
b = "0" + b
}
}
a = utils.Reverse(a)
b = utils.Reverse(b)
total := ""
remainder := "0"
// Add them together
for i := 0; i < len(a); i++ {
aChar := string(a[i])
bChar := string(b[i])
t, r := addChars(aChar, bChar, remainder)
remainder = r
total += t
}
if remainder != "0" {
total = remainder + total
}
return utils.Reverse(total)
}
func addChars(a string, b string, r string) (string, string) {
// Convert to int
decimalConverter := map[string]int{
"=": -2,
"-": -1,
"0": 0,
"1": 1,
"2": 2,
}
snafuConverter := map[int]string{
-2: "=",
-1: "-",
0: "0",
1: "1",
2: "2",
}
// Add the numbers
total := decimalConverter[a] + decimalConverter[b] + decimalConverter[r]
// Check if we're over
if total > 2 {
return snafuConverter[total-5], "1"
}
// Check if we're under
if total < -2 {
return snafuConverter[total+5], "-"
}
// Otherwise we're in the clear
return snafuConverter[total], "0"
}
func main() {
input := utils.ReadInput("input.txt")
part1(input)
}