Skip to content

Commit b67a62b

Browse files
committed
25th day
1 parent 56fbd06 commit b67a62b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

day25/day25.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
type DataType []string
11+
12+
func parseData(data string) DataType {
13+
return strings.Split(data, "\n")
14+
}
15+
16+
func encode(n int) string {
17+
result := []byte{}
18+
19+
for i := 0; ; i++ {
20+
result = append(result, '2')
21+
if decode(string(result)) >= n {
22+
break
23+
}
24+
}
25+
26+
for i := range result {
27+
for _, option := range []byte{'=', '-', '0', '1', '2'} {
28+
result[i] = option
29+
if decode(string(result)) >= n {
30+
break
31+
}
32+
}
33+
}
34+
35+
return string(result)
36+
}
37+
38+
func decode(s string) (rc int) {
39+
decoder := map[byte]int{'2': 2, '1': 1, '0': 0, '-': -1, '=': -2}
40+
41+
for i := range s {
42+
rc += Pow(5, i) * decoder[s[len(s)-i-1]]
43+
}
44+
45+
return
46+
}
47+
48+
func solvePart1(data DataType) (rc string) {
49+
digitalSum := 0
50+
for _, s := range data {
51+
digitalSum += decode(s)
52+
}
53+
54+
return encode(digitalSum)
55+
}
56+
57+
func solvePart2(data DataType) (rc string) {
58+
return "Thank you Eric for another wonderful year of AoC!"
59+
}
60+
61+
func main() {
62+
data := parseData(FetchInputData(23))
63+
fmt.Println(solvePart1(data))
64+
fmt.Println(solvePart2(data))
65+
}

day25/day25_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
func ExpectedPart1() string {
11+
return "2=-1=0"
12+
}
13+
14+
func ExpectedPart2() string {
15+
return "⭐️⭐️"
16+
}
17+
18+
var data DataType
19+
20+
func init() {
21+
input, _ := os.ReadFile("../examples/25.txt")
22+
data = parseData(string(input))
23+
}
24+
25+
func TestPart1(t *testing.T) {
26+
AssertTestPartX(t, ExpectedPart1(), solvePart1(data))
27+
}
28+
29+
func TestPart2(t *testing.T) {
30+
AssertTestPartX(t, ExpectedPart2(), solvePart2(data))
31+
}

examples/25.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
1=-0-2
2+
12111
3+
2=0=
4+
21
5+
2=01
6+
111
7+
20012
8+
112
9+
1=-1=
10+
1-12
11+
12
12+
1=
13+
122

0 commit comments

Comments
 (0)