Skip to content

Commit c3e2952

Browse files
authored
Merge pull request scalacenter#230 from scalacenter/add-day-25-code
add day 25 code
2 parents c817961 + 5436685 commit c3e2952

File tree

4 files changed

+174
-1
lines changed

4 files changed

+174
-1
lines changed

2022/input/day25

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
22-=0-1-=
2+
1-==1200200=22-1==2=
3+
2-01=--01==01
4+
20-=
5+
21=210=002
6+
12=-0002
7+
1=100-==0
8+
1101
9+
2==-=0=0110-
10+
2=100122==--1-
11+
2=1-2==2--1
12+
1==0=2=0==1100
13+
2=0==-2
14+
1=1222202101--=
15+
1101=2111-11=
16+
1=11=2022-11-0=
17+
1=2=1102-
18+
1--21=1--=
19+
11-
20+
1001-10-
21+
201222-1-2
22+
122--2
23+
2-
24+
11--2-=20=11-2=0
25+
1-000=1221==2-
26+
1=-==-=2-0
27+
121-
28+
1--2-=---0=01
29+
1=00=010-1-20
30+
1=110=2
31+
1110-=01--0-12--0
32+
200022-
33+
2=-=0=-1==2-=2222
34+
20201
35+
11-0
36+
1==
37+
1-=---2==20=01
38+
2=2=-1
39+
1==01-100
40+
1-=----=--=
41+
1-011--20=1-==2121
42+
101---1=-1--0=21--2
43+
221=0100--020=11
44+
1=-121=2=-1-=
45+
11210-
46+
1=0=2=22-==-==-
47+
2=2220210221-202
48+
1--1=---00-11
49+
2=0--
50+
1=-2=-1==2=0
51+
1=0=0=
52+
122012210-=010=1
53+
21-1-220=21-=-00
54+
2-=2==0
55+
1220
56+
20
57+
1=0=22=-21101
58+
2-===0==1-210
59+
1=-220=121=----2
60+
21=0=-=0---0===1=--
61+
202-22112---0-2=
62+
10=0
63+
11
64+
102120--=0-021-121
65+
100--221=
66+
2--011=-=202
67+
1=-
68+
110=0-0-021-
69+
121
70+
1-0210-0
71+
2-10=2
72+
2=-0-1=00--22
73+
121-=2
74+
12-212110-022-2211
75+
1=222==11=-=-01=10
76+
200-0120=
77+
1-00===12=-1=-0--
78+
100-1
79+
102-2201
80+
10-=0-==2=2==2=0
81+
12=01
82+
1012-2
83+
1=0-02=21=1==1
84+
2=021-
85+
1-001-
86+
10202-00=12===11-1
87+
1=
88+
20=11=0112=
89+
2--1=1-1121
90+
2-2=---222
91+
1=-001==-0-
92+
1-=112-=
93+
11011
94+
1=2==1
95+
1-=2=2201
96+
2==222-==
97+
101-0-=2
98+
1-10-0212-0==0-10
99+
1-2-1-=01100
100+
121=0-12-2
101+
20=0=0-0
102+
22000-==0-1-2=0=0=
103+
10=
104+
2101102
105+
1120===10=2-2=02-
106+
1-=10--211
107+
2===-01==1=--===-
108+
1=21-201-==200=
109+
1=-=2=10
110+
220-120=-0-1010
111+
12011
112+
1-0=2
113+
1---01
114+
1=-=1=021=0=0=011
115+
1=10-212-0
116+
2=
117+
10-0=-1=
118+
1-21=22=1101000-1-
119+
11=00222-=100--

2022/input/day25-1

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

2022/src/day02.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def readCode(opponent: String) = opponent match
3030
case "B" => Paper
3131
case "C" => Scissors
3232

33-
def scores(input: String, strategy: (Position, String) => Position): IterableOnce[Int] =
33+
def scores(input: String, strategy: (Position, String) => Position): Iterator[Int] =
3434
for case s"$x $y" <- input.linesIterator yield
3535
val opponent = readCode(x)
3636
score(opponent, strategy(opponent, y))

2022/src/day25.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package day25
2+
3+
import locations.Directory.currentDir
4+
import inputs.Input.loadFileSync
5+
6+
@main def part1: Unit =
7+
println(s"The solution is ${part1(loadInput())}")
8+
9+
def loadInput(): String = loadFileSync(s"$currentDir/../input/day25")
10+
11+
def part1(input: String): String =
12+
challenge(input)
13+
14+
val digitToInt = Map(
15+
'0' -> 0,
16+
'1' -> 1,
17+
'2' -> 2,
18+
'-' -> -1,
19+
'=' -> -2,
20+
)
21+
val intToDigit = digitToInt.map(_.swap)
22+
23+
def showSnafu(value: BigInt): String =
24+
val buf = StringBuilder()
25+
var v = value
26+
while v != 0 do
27+
val digit = (v % 5).toInt match
28+
case 0 => 0
29+
case 1 => 1
30+
case 2 => 2
31+
case 3 => -2
32+
case 4 => -1
33+
buf.append(intToDigit(digit))
34+
v = (v - digit) / 5
35+
buf.reverseInPlace().toString()
36+
37+
def readSnafu(line: String): BigInt =
38+
line.foldLeft(BigInt(0)) { (acc, digit) => acc * 5 + digitToInt(digit) }
39+
40+
def challenge(input: String): String =
41+
showSnafu(value = input.linesIterator.map(readSnafu).sum)

0 commit comments

Comments
 (0)