Skip to content

Commit 36b24d6

Browse files
committed
2022/Day 08
1 parent f8ca95a commit 36b24d6

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

src/main/kotlin/me/grison/aoc/Collections.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package me.grison.aoc
22

33
import java.util.*
44
import java.util.concurrent.ConcurrentLinkedDeque
5+
import java.util.function.Predicate
56
import java.util.stream.IntStream
67
import kotlin.collections.ArrayDeque
78
import kotlin.streams.toList
@@ -94,7 +95,7 @@ fun <T> List<List<T>>.transpose(): List<List<T>> {
9495
.toList()
9596
}
9697

97-
fun <T> MutableList<T>.reset(item: T) : MutableList<T> {
98+
fun <T> MutableList<T>.reset(item: T): MutableList<T> {
9899
this.clear()
99100
this.add(item)
100101
return this
@@ -188,3 +189,17 @@ fun List<String>.mapSet() = this.map { it.toSet() }
188189

189190
fun <T> List<T>.rejectAll(elem: T) = this.filter { it != elem }
190191
fun <T> List<T>.mutable() = this.toMutableList()
192+
193+
fun <T> List<T>.countUntil(pred: Predicate<T>) = this.until(pred).count()
194+
fun <T> List<T>.until(pred: Predicate<T>): List<T> {
195+
val l = mutableListOf<T>()
196+
for (x in this) {
197+
l.add(x)
198+
if (pred.test(x)) {
199+
break
200+
}
201+
}
202+
return l
203+
}
204+
205+
fun <K, V> Map<K, V>.at(k: K) = this.getValue(k)

src/main/kotlin/me/grison/aoc/Extensions.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ operator fun Pair<Int, Int>.minus(p: Pair<Int, Int>): Pair<Int, Int> = Pair(firs
174174
/** `*` operator for Pairs. */
175175
operator fun Pair<Int, Int>.times(p: Pair<Int, Int>): Pair<Int, Int> = Pair(first * p.first, second * p.second)
176176

177+
fun bools() = mutableListOf<Boolean>()
178+
fun ints() = mutableListOf<Int>()
179+
180+
fun directions() = listOf(p(-1, 0), p(1, 0), p(0, -1), p(0, 1))
181+
177182
fun Pair<Int, Int>.above() = this.plus(p(0, -1))
178183
fun Pair<Int, Int>.below() = this.plus(p(0, 1))
179184
fun Pair<Int, Int>.left() = this.plus(p(-1, 0))

src/main/kotlin/me/grison/aoc/Graphs.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ private fun <T> Graph<T>.sizedComponent(default: Int, choser: (Int, Int) -> Int)
224224
return sizeval
225225
}
226226

227+
fun <T> Grid<T>.allInDirection(pos: Pair<Int, Int>, dir: Pair<Int, Int>) : List<Pair<Int, Int>> {
228+
var cursor = pos
229+
val list = mutableListOf<Pair<Int, Int>>()
230+
while (true) {
231+
cursor += dir
232+
if (cursor !in this) {
233+
break
234+
}
235+
list.add(cursor)
236+
}
237+
return list
238+
}
239+
227240
fun <T> Grid<T>.islandCount(symbol: T, dimensions: Pair<Int, Int>): Int {
228241
fun explore(grid: Grid<T>, pos: Position, visited: MutableSet<Position>): Boolean {
229242
if (!pos.within(0, 0, dimensions.first, dimensions.second)) return false
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.grison.aoc.y2022
2+
3+
import arrow.syntax.function.memoize
4+
import me.grison.aoc.*
5+
import java.lang.Integer.max
6+
7+
class Day08 : Day(8, 2022) {
8+
override fun title() = "Treetop Tree House"
9+
10+
override fun partOne() = solve().first
11+
12+
override fun partTwo() = solve().second
13+
14+
private val solve = {
15+
val grid = inputList.intGrid(-1)
16+
17+
var (visibleTrees, scenicScore) = p(0, 0)
18+
grid.allPositions().forEach { pos ->
19+
val (visible, seen) = p(bools(), ints())
20+
21+
for (dir in directions()) {
22+
val trees = grid.allInDirection(pos, dir)
23+
visible.append(trees.all { grid.at(it) < grid.at(pos) })
24+
seen.add(trees.countUntil { grid.at(it) >= grid.at(pos) })
25+
}
26+
27+
scenicScore = max(scenicScore, seen.product())
28+
visibleTrees += visible.any { it }.toInt()
29+
}
30+
31+
p(visibleTrees, scenicScore)
32+
}.memoize()
33+
}

src/main/resources/2022/08.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
122202212311200210333041243104410211022242435153422552354101423030331120103122123201123013212020121
2+
212122122000221311323440442444413314215241554351515445144551151103011402331412314230102131310221002
3+
200010233110132032201044033410403402323542453441213225433145441453242244421030303220011011132300002
4+
120101110203333110240120011412225434434121313542334144355225111454523132420142243111113223031311111
5+
120110223201233140434331404320315125532452213233242311511524412523343520430421042000030001030200222
6+
000102322010022341104032321113312242521134214543411111515333425154553221421211441143024103333323012
7+
012132022020312444102430014331221411222235334515444152433144133435532224431441304412411212000133031
8+
120233223310314441323131225313444245412451315432455236445534542223421552522450312102202033233322003
9+
110303300214411413041135513123311333342254632434525246464244653512154122151153240314331404133102032
10+
320333021041401210342112413252521354154654526224624264656643323243514213144525443140321100432322102
11+
213333013400314344445512541224344542425335542246245535342346525455632342143432241440034221113121103
12+
103101323212441144514541213255356562253454324423562254643435566355652443131311521310340011410023010
13+
232231310023333125425145335413643646335436344645256622626244424566265461143132555442033102212102133
14+
230012330403333244113122423525434543453346345442546444536356456656653336522243141515320031310242132
15+
001331331400242344452535542244353454256436245444356344633623323223624634633531345443212332124100001
16+
320213230430015352541121166364225342324465546743674376745764665342525653462642431254322120131320301
17+
030030441321345531435352552565634242236456747547675667333646754662532244322223425143335202013410331
18+
130323430403445155252353542665556632743644346337573354575553473564426335656353332452444512100342303
19+
000124441244513213434222225365536245467565344634335675756354647753423526454264443544455252333100012
20+
303404133431225152343544434244326457656763736537576455577647443546747235534536353521453342020431303
21+
311104212215444221552335243356444654364337535373347655455775663756457363653523325323222244130341434
22+
033113243413224543336252525236557657445633537454545454747556455455777455542442433541422521522303132
23+
032301305335212345536223463445733334466466456857685556646643443634434366755622423221321152153211112
24+
432413234435244244452452522766657337677487457667574884664485837634653735355652265344225135521142200
25+
214204451352332225552233236447335656736454774865747468575666547736456546737626423225514542353132003
26+
033032334245244345353263544373576764747865456884456784664774575446775657546624634322432214245131331
27+
121012311135135242665226463546373455674886866675578444744865445486444436446765632254623534551121111
28+
341323122332344433445227455474347888748648456446654846846776658764776443766657642346253535143552144
29+
324244533333334656636247465677737577755486464656557545574668654666685345546335423322234653144345021
30+
043204235524544223242334547537787887788644766889958956876454754674478577343347433636323431422535420
31+
132053544243564633627466765775657547755548985595865776876887476476586773674345367554624423333443214
32+
434145415151462624423574737777874646757869897568985857768555766747845787346556464366425553353151300
33+
033552122413443664556665676655648855845975596556995967956566699866477545537554553645645553335523524
34+
413544333326325544236357664865456474858595889756579986899779997968486748464773366656464426231155442
35+
004332522265552444763374547484644488886789598575569799777767888975656856667647455453254435233141511
36+
035354414545334263444765576467446658985699666669667988675585776867776657644544534734656653433441431
37+
442122443263522535743777354555686688857759689956668767555979587695855866658755676536345652311311234
38+
231314151362463367455573668565545776596986867687779777769576568988867748688753444556446645553534525
39+
045415334236364244375435664665477567587759896768777886876768796985898556758446336336326243331233454
40+
342353524362463357737675678645755585686559698888978699669989566777695575467773733463432243352222445
41+
244133134532623377737577688575658856867977866698779767897688987856558847648874553573743553546523534
42+
021553556544442474674534678684779989779667798977898967696766875855597978664786474743343545232141545
43+
045425316553544766676736774475658985557769887677898986998789979958865786565747357474546425542554235
44+
123511255462335663763334745464955879987988867676898776768888899576578976678476464757366443234425311
45+
325533243442663433734555785478977676679868889789787989899689977975777578786878557777434464652443351
46+
341155355546553356347757544788568965668697968997877777976676899896768799766576567645554342336334233
47+
421521345226644475636665485646689879769887867898789798988788768788759989487445577633675662663234325
48+
225431534432355354466644477659579957678999798898887898798887799686577775655556855457566334643614134
49+
254522334642647433556585856548559868968877679888988778897876767896886779646466464564775265336232441
50+
312113532345553345433444768455667856989979687989987899997988898888658869478687547476363665422513141
51+
152555423534245354346564447446976597699676889998999887989796789797775977768544533373735436335515423
52+
341213542653444767556388768885878975698787998799977787779899987685979877545764473357337423424514315
53+
322113552445453574657574755545799689867898669877988877997779898765669858668546477767666242656344212
54+
523331354363325454365675645869975977878797967999887779897978887888798999548865636336353426535355452
55+
523523423432344663765375787446557887679766968878979887789796777987687596555667445376536663356525141
56+
551114235346366563637355556866885895767877798798797989787886976886787698548687646557754363564525313
57+
251223245223334447766584755447956898986768669897979798997768689676988975874646433446733346424411335
58+
214241355255455766365674476646975968859866869967978898867967666685895878644875637565576656255154544
59+
455352455253232666575668454867955886956998987678966896777867699977589755646464755747533332342424541
60+
444515424223524645435648587576677675775989869686879869776776888598575688877564767577434234326235545
61+
431313414332525265734668687845775796567769886777989778777997885578687756877567364366345652533512134
62+
333555123263656354334546784878748996895956799868879687768998999597665865666673657464554445565315153
63+
234251415355332356354555748857549756797677788669666779776896855789587868886666535745644366551433223
64+
425342555454555443357573485485875677656895597978676766678885768675668576658854377547455252453332433
65+
323212322142542547356336654885588587756876988878668876996855885757567666665664655447625566342455323
66+
145453124156454236753637566754554586888986779659899875656867888757855674866346635564564335311444142
67+
241532355322446443663343474577685666578989895777658988767886579994465878877675775646446644411214331
68+
144121414316432226357635465574878648885659856586566979685759558986575567664637365426532646212254243
69+
110513511415656625443556556674684868678895878785759566769757697468556564753375577435453525411543113
70+
010054252444265234547356444555575676545757579699968766586568964546866767534346564224265623245525511
71+
120212515152224263635534376453556487856658876995968869757756754456584754356376564445254454534225340
72+
334403322452135643232646334553464468686466757956767796875866688444464456337547673543644223334124002
73+
424345231535522355445635437664744565688755754586858454858554458475568756654735523654265235423342312
74+
031443144424522463542544764444765767644457666757567678447484574545767543635774453342345411351542313
75+
321403155315156656344554665775567644878587754546578686767855867554637477555573424362546151514131330
76+
100300432342253325546433443636475644557666885556847555458755555684535636335545343542245513545400031
77+
241103243345314352532434567355636677768477667655874858784887748363644434657254223342542521512523103
78+
013432002255524532425326442376655436757756476784578755546658775744775577372453335234234123311334110
79+
224312132351342113543625426364775474475555435685668887764654565767573537642634534534253412535033121
80+
342013333254343554666262433657555734443433335545744436575736345777633344224223656361344225330312223
81+
234442324252344452264263235643446676735646567555767567465473767566476572456525232422431125340421040
82+
030302023035452131512323435555347556466757645454545634364636667436454662255465636151444354004144240
83+
002004133214113555342264554265222355765774744775436734535556664773753352634233435412243552441100341
84+
011422341232151553353542265363535632735746674664776744464473335435625662443566132542533542101243120
85+
203021201322125111143244446544365445365774633366633765656576533325536225455355341443312221224423103
86+
311021234200144512534253453355352253535542456754747757743743435344545565465655544322254011444341222
87+
101203222013410332153151244543442433553342325366345652255264423465522455665453535113251410414031300
88+
103101032340424335231434112162665465333242654445464443552552544226533654451145222233401204012240023
89+
300000234304043032354531115322565425623354453256432525255636642566343622125151231523130220222300331
90+
320320110121204431154322441252432253656645325532363344542646546553223512543244125154443233034012021
91+
223211323021301334023554115315242234323456322565626332455546252636654333515412114103233412041213321
92+
020331330004113222113544411442545513454442262444225546542423526322541134153211543210202143401031310
93+
033131211214044030334411533413134451124346654653645545223652313534244341353254324212241333001202010
94+
011223212020313313300223232314353315524525531133464633645121312251455142242212211423301012011010012
95+
202131322330230012310003211315154334411525215354321214224515115324133412515133121340021221223011311
96+
102233123000103132110110422414142252322414331525452331522255224253512324323332322332002033332200121
97+
010112331222023221443321410332323442443211235352422543351522342441341212433312114143012011012301012
98+
102001321100332034112010340431405521533133132514423245132455311232555303030103043321011110131121202
99+
102100020233112322130014030443133042115515544424443514351353232214340221142434234013000032002122000

0 commit comments

Comments
 (0)