Skip to content

Commit 353cf3b

Browse files
committed
feat: solutions for day 25
1 parent c6dca0a commit 353cf3b

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

25/input.txt

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
11-00
2+
2--2==212=12
3+
1=0110121=2
4+
1=012-21-=121
5+
1==0-1121-=200=2212
6+
1=1210===00=
7+
11210021-0001010-
8+
12=1-10-1-
9+
212-2==221-210-21
10+
1=-01=0-02-2-=-222=
11+
1=022000===-22010
12+
1=01=12202
13+
11=
14+
202-1
15+
2-12-2=--1
16+
10-=-00=101=-0-=-0
17+
1112-1
18+
2--1201
19+
1=1==-020=1-
20+
1--0-=2-0022-
21+
1=-1-21-200-102=
22+
11=100021210
23+
1=2=01=2-=01-=
24+
1=0120-11-0-=-2=-2
25+
1=-20221=2=-2=
26+
1-0
27+
1=00=-=11100--=2
28+
22122
29+
1=102222
30+
1210=1=001-=000
31+
1--200102022-=
32+
102=12=-1==
33+
100=
34+
20-02
35+
10
36+
10==-102
37+
11=020-0210-1-2012=
38+
12-11=0-1-=
39+
20=-
40+
212=1-1-10
41+
1=20=1-=2-11=0
42+
1=22
43+
120=2202=12200
44+
1-0-1
45+
211011-11=
46+
2=120=--==0==--0-0
47+
110-0-=-10-1-112
48+
1=2=1-=1-2
49+
1=12==10
50+
10=2110
51+
22222-1=01-1
52+
111-=2=22-==-00
53+
10===2-2=21100-1=
54+
100-21-0-1=021
55+
2-21=0
56+
1===002012=2=-1121
57+
122222-101=1-==2-2=
58+
112=0
59+
2--212=0
60+
110--2-0022--01=-
61+
1-0-0=0-1011-2-=0--2
62+
1=--0-2=1
63+
2=
64+
1=1
65+
1022=1--2---120
66+
2-1=2-22--21=1-212
67+
22=-11-0=-1
68+
2==210022=
69+
11=000=002-2002-1=
70+
2-2=02=0
71+
1=00-=----=
72+
1=121-2
73+
12-2=2-=2=2022
74+
2=110=1=2=02
75+
2
76+
1=0--02-121
77+
1200-2000=0
78+
1==--021--2=-
79+
12-1=22=-02--2-
80+
1=1=1--121-1221
81+
2-0212=
82+
21-==0-201-
83+
20212=1
84+
111--
85+
22-2=-1
86+
112
87+
2=2-==101
88+
1-=1
89+
12211=010=-=10--
90+
2211
91+
21-110-1-22==-10
92+
121-1-
93+
22000=-=2-0=012
94+
212-02
95+
102
96+
2-110=11=--
97+
11=11===0
98+
1=210-0120
99+
1=--1011
100+
2=00022==21-1=-2
101+
21=1-==2---2=0-2
102+
1220-11=022122
103+
21210=01
104+
1==1-2
105+
2=2
106+
11-11122=-
107+
200-0=21202-=
108+
2=-11
109+
2==2=0=1120221
110+
101010
111+
1=2=-=--=-2-0=
112+
111=0=--
113+
1=0121-=2
114+
1==1=1
115+
2-0222=2=-
116+
12=
117+
10-=0-2

25/main.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
"strings"
9+
)
10+
11+
type Coord struct {
12+
x, y int
13+
}
14+
15+
var Zero = Coord{}
16+
17+
func (coord Coord) Move(dir int) Coord {
18+
return coord.Add([]Coord{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}[dir])
19+
}
20+
21+
func (coord Coord) Add(add Coord) Coord {
22+
return Coord{coord.x + add.x, coord.y + add.y}
23+
}
24+
25+
func SnafuToDecimal(snafu string) int {
26+
decimal := 0
27+
for _, ch := range snafu {
28+
var digit int
29+
switch ch {
30+
case '=':
31+
digit = -2
32+
case '-':
33+
digit = -1
34+
default:
35+
digit = int(ch - '0')
36+
}
37+
decimal *= 5
38+
decimal += digit
39+
}
40+
return decimal
41+
}
42+
43+
func DecimalToSnafu(decimal int) string {
44+
switch decimal {
45+
case -2:
46+
return "="
47+
case -1:
48+
return "-"
49+
case 0:
50+
return "0"
51+
}
52+
snafu := ""
53+
for decimal > 0 {
54+
digit := decimal % 5
55+
if digit > 2 {
56+
decimal += 5
57+
}
58+
snafu = []string{"0", "1", "2", "=", "-"}[digit] + snafu
59+
decimal = decimal / 5
60+
}
61+
return snafu
62+
}
63+
64+
func main() {
65+
file, err := os.Open("input.txt")
66+
if err != nil {
67+
log.Fatal(err)
68+
}
69+
defer file.Close()
70+
71+
sum := 0
72+
73+
scanner := bufio.NewScanner(file)
74+
for scanner.Scan() {
75+
line := strings.TrimSpace(scanner.Text())
76+
if len(line) == 0 {
77+
continue
78+
}
79+
sum += SnafuToDecimal(line)
80+
}
81+
if err := scanner.Err(); err != nil {
82+
log.Fatal(err)
83+
}
84+
85+
fmt.Println(DecimalToSnafu(sum))
86+
}

25/sample.txt

+13
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)