Skip to content

Commit 2605932

Browse files
committed
Advent of Code 2020 in Kotlin
1 parent 4685e1a commit 2605932

File tree

109 files changed

+16751
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+16751
-2
lines changed

2020/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
classes/
2+
output.*.txt

2020/dec1/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Use of this source code is governed by an MIT-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/MIT.
6+
7+
# https://adventofcode.com/2020/day/1
8+
9+
KOTLIN = kotlin
10+
KOTLINC = kotlinc
11+
CLASSES = classes
12+
PACKAGE = dec1
13+
MAIN_CLASS = Dec1Kt
14+
15+
compile: $(CLASSES)/$(PACKAGE)/$(MAIN_CLASS).class
16+
17+
$(CLASSES)/$(PACKAGE)/$(MAIN_CLASS).class: $(PACKAGE).kt
18+
@mkdir -p $(CLASSES)
19+
$(KOTLINC) -d $(CLASSES) $(PACKAGE).kt
20+
21+
output.%.txt: input.%.txt compile
22+
$(KOTLIN) -cp $(CLASSES) $(PACKAGE).$(MAIN_CLASS) < $< | tee $@
23+
24+
out: output.example.txt output.actual.txt
25+
26+
clean:
27+
rm -r $(CLASSES)

2020/dec1/dec1.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package dec1
10+
11+
import kotlin.time.ExperimentalTime
12+
import kotlin.time.TimeSource
13+
import kotlin.time.measureTimedValue
14+
15+
/* Input is integers. Find the two which sum to 2020, print their product. */
16+
object Part1 {
17+
fun solve(lines: Sequence<String>): String {
18+
val seen = mutableSetOf<Int>()
19+
return lines.map(String::toInt)
20+
.filter {
21+
try {
22+
(2020 - it) in seen
23+
} finally {
24+
seen.add(it)
25+
}
26+
}
27+
// .map { it.also(seen::add) } // would incorrectly match 1010 with itself
28+
// .filter { (2020 - it) in seen }
29+
.map { it * (2020 - it) }
30+
.joinToString("\n")
31+
}
32+
}
33+
34+
/* Input is integers. Find three which sum to 2020, print their product. */
35+
object Part2 {
36+
fun solve(lines: Sequence<String>): String {
37+
val seen = mutableSetOf<Int>()
38+
val sums = mutableMapOf<Int, Pair<Int, Int>>()
39+
val results = mutableListOf<Int>()
40+
for (i in lines.map(String::toInt)) {
41+
if ((2020 - i) in sums) {
42+
results.add(sums.getValue(2020 - i).let { i * it.first * it.second })
43+
}
44+
seen.forEach { x: Int -> sums[x + i] = x to i }
45+
seen.add(i)
46+
}
47+
return results.joinToString("\n")
48+
}
49+
}
50+
51+
@ExperimentalTime
52+
@Suppress("UNUSED_PARAMETER")
53+
fun main(args: Array<String>) {
54+
val lines = generateSequence(::readLine).toList()
55+
println("Part 1:")
56+
TimeSource.Monotonic.measureTimedValue { Part1.solve(lines.asSequence()) }.let {
57+
println(it.value)
58+
println("Completed in ${it.duration}")
59+
}
60+
println("Part 2:")
61+
TimeSource.Monotonic.measureTimedValue { Part2.solve(lines.asSequence()) }.let {
62+
println(it.value)
63+
println("Completed in ${it.duration}")
64+
}
65+
}

2020/dec1/input.actual.txt

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
1429
2+
1368
3+
1661
4+
1687
5+
1593
6+
1495
7+
1565
8+
1500
9+
1635
10+
1845
11+
1645
12+
1999
13+
1415
14+
1054
15+
1930
16+
1774
17+
1405
18+
1993
19+
1757
20+
1623
21+
1675
22+
1665
23+
631
24+
1950
25+
1702
26+
1311
27+
1509
28+
1790
29+
1643
30+
1884
31+
226
32+
1455
33+
1679
34+
1746
35+
1284
36+
1342
37+
1684
38+
1543
39+
1396
40+
1806
41+
1523
42+
1363
43+
1011
44+
1577
45+
1767
46+
1287
47+
1885
48+
1517
49+
1556
50+
1722
51+
1260
52+
1624
53+
1466
54+
1263
55+
1162
56+
1688
57+
1202
58+
1913
59+
1964
60+
1385
61+
1970
62+
1976
63+
1431
64+
858
65+
1748
66+
1544
67+
1438
68+
1300
69+
1926
70+
1587
71+
1376
72+
1939
73+
1039
74+
1639
75+
1539
76+
1491
77+
1631
78+
1521
79+
1564
80+
1507
81+
1637
82+
1534
83+
1713
84+
1533
85+
1118
86+
1356
87+
2003
88+
282
89+
1079
90+
1837
91+
1259
92+
1941
93+
1836
94+
1903
95+
1433
96+
1467
97+
1027
98+
1441
99+
1048
100+
1742
101+
1087
102+
1872
103+
1476
104+
1657
105+
1361
106+
1182
107+
1494
108+
1529
109+
1822
110+
1444
111+
1330
112+
1514
113+
1723
114+
1432
115+
1683
116+
1997
117+
1443
118+
1474
119+
1932
120+
1504
121+
1313
122+
1765
123+
19
124+
1784
125+
1619
126+
992
127+
1560
128+
1680
129+
1626
130+
1558
131+
1899
132+
1293
133+
1676
134+
1161
135+
1140
136+
1341
137+
1597
138+
1628
139+
1611
140+
1302
141+
1269
142+
1241
143+
1952
144+
1591
145+
1726
146+
428
147+
1703
148+
1289
149+
1109
150+
1478
151+
1002
152+
1817
153+
1849
154+
1838
155+
1319
156+
1641
157+
583
158+
1920
159+
1453
160+
1411
161+
1870
162+
1763
163+
1469
164+
1646
165+
1719
166+
1213
167+
1462
168+
1545
169+
1682
170+
1711
171+
18
172+
2004
173+
1252
174+
1620
175+
1559
176+
1315
177+
781
178+
1656
179+
1987
180+
1436
181+
1630
182+
1985
183+
1897
184+
1551
185+
1296
186+
1282
187+
1735
188+
1320
189+
1659
190+
1271
191+
1380
192+
1274
193+
1876
194+
1492
195+
1298
196+
1399
197+
1692
198+
1265
199+
1555
200+
1337

2020/dec1/input.example.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1721
2+
979
3+
366
4+
299
5+
675
6+
1456

2020/dec10/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Use of this source code is governed by an MIT-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/MIT.
6+
7+
# https://adventofcode.com/2020/day/10
8+
9+
KOTLIN = kotlin
10+
KOTLINC = kotlinc
11+
CLASSES = classes
12+
PACKAGE = dec10
13+
MAIN_CLASS = Dec10Kt
14+
15+
compile: $(CLASSES)/$(PACKAGE)/$(MAIN_CLASS).class
16+
17+
$(CLASSES)/$(PACKAGE)/$(MAIN_CLASS).class: $(PACKAGE).kt
18+
@mkdir -p $(CLASSES)
19+
$(KOTLINC) -d $(CLASSES) $(PACKAGE).kt
20+
21+
output.%.txt: input.%.txt compile
22+
$(KOTLIN) -cp $(CLASSES) $(PACKAGE).$(MAIN_CLASS) < $< | tee $@
23+
24+
out: output.example.txt output.example2.txt output.actual.txt
25+
26+
clean:
27+
rm -r $(CLASSES)

0 commit comments

Comments
 (0)