Skip to content

Commit

Permalink
Solved day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianCassayre committed Dec 10, 2024
1 parent dce5c95 commit f15c913
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _My solutions to the 2024 edition of [Advent of Code](https://adventofcode.com/2
| **[07](https://adventofcode.com/2024/day/7)** | [solution](src/main/scala/adventofcode/solutions/Day07.scala) |
| **[08](https://adventofcode.com/2024/day/8)** | [solution](src/main/scala/adventofcode/solutions/Day08.scala) |
| **[09](https://adventofcode.com/2024/day/9)** | [solution](src/main/scala/adventofcode/solutions/Day09.scala) |
| **[10](https://adventofcode.com/2024/day/10)** | [](src/main/scala/adventofcode/solutions/Day10.scala) |
| **[10](https://adventofcode.com/2024/day/10)** | [solution](src/main/scala/adventofcode/solutions/Day10.scala) |
| **[11](https://adventofcode.com/2024/day/11)** | [](src/main/scala/adventofcode/solutions/Day11.scala) |
| **[12](https://adventofcode.com/2024/day/12)** | [](src/main/scala/adventofcode/solutions/Day12.scala) |
| **[13](https://adventofcode.com/2024/day/13)** | [](src/main/scala/adventofcode/solutions/Day13.scala) |
Expand Down
55 changes: 55 additions & 0 deletions input/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
4308765476541010123498898543423458767898432123454387210
5219002389532309835567387612210569678763010036767896523
6578111001654318756765487606787634569854321879879265674
7869223210789223643890394565890123452987412969778104985
8954354378901134532431213064321210981236503458763203876
7321269457432001121345302154760107870145783234654312101
3030378766589112030236034565871012565057890145576447878
2109432852348743940107125678965143456760761076485436969
3278501901257657854398324989854234327871252980390125458
4567687823768567761295413070123349818982343101287834301
0123596314899658980786502110543056700178987232396965210
7874105405658743801657876325632198765230876540105456789
6965012308756752102340965434789430124321965431234305678
3453210219567801234561234478996521035898012320123212501
4354234789458943107876874567667830546734343211210789410
3269105656327652106988965413454945675125254302323654321
0178654569810785435843214302123986789076167019454789012
4323763678987696524356903212012675896985098108545643422
5016892109012587813247872456903504765430789217655652101
6727654068103423902136501347865413890121894327896766501
5898343877678014321045696219870322934578765436521897432
0981232966549165012136787900761231025689456943430198125
1270301051032078983023129871234012310780367852391789034
2567410145121189674510036700965567498891234761087076540
3458547236010234567612445212873498567890345678156105421
2189678987321109878102344343962145656765432109343218432
5078981087455610969201257652450034565654501656734569849
6789672398534701454382568701321127674985432349823678958
5010965467629812345699879811232078981276543036910589867
4127854102018790456781236760103465210300187127832430150
3236543201126781065400145407632564367210296676541321041
8745696012337652878312439818541673458341345587560345632
9656787763018943969223456729230982109452213495671256765
2345669854325676852108987634101065432969302104380189854
1105678760134980345670104545432178341878454213298989923
0234389321101671238983203692345899250545563210101876310
2109489430238980108974312781056787167436778921032365400
3478076589845671067065693676176596078929879780123456321
4567189871234567652108787676785454321010785691434234542
8983287860043458943239654587792309451021094396560147623
7654896952154367438740543297871218762136789287678988012
1012345443763219829651270106980367643445672110589689123
5101056339821006710154389015454454324934101023478765434
4298763232100145603455676723303465815873205192107685445
5675610103243234912369879854212556906763216783298596326
4384324234352107809878766765189657875454345690123487015
3295435435461234762703454101078703965234432101094509876
2126306326970345671012963210987612050125105678987610745
4017217017889789982167878456876543140156234783234321030
3018918523778650173450189387945678231287105990125238921
2127809456234043268943201296532169874397654891076149865
3456912387105123457652100145632054365698923781089056774
9320149896696143210781763236541065251207210650198456783
8013234756787056945690854167632107100316540143232343892
7654105665432167832101941018943298765423434234321014901
1 change: 1 addition & 0 deletions output/10-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
698
1 change: 1 addition & 0 deletions output/10-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1436
39 changes: 39 additions & 0 deletions src/main/scala/adventofcode/solutions/Day10.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package adventofcode.solutions

import adventofcode.Definitions.*

@main def Day10 = Day(10) { (input, part) =>

case class Vec(i: Int, j: Int):
infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j)

val grid = input.toLines.map(_.map(_.asDigit))

val zeros = grid.indices.flatMap(i => grid(i).indices.filter(j => grid(i)(j) == 0).map(j => Vec(i, j)))

val adjacent =
for
i <- -1 to 1
j <- -1 to 1
if i.abs + j.abs == 1
yield Vec(i, j)

def inBounds(vec: Vec): Boolean = grid.indices.contains(vec.i) && grid(vec.i).indices.contains(vec.j)

def explore(nodes: Iterable[Vec], i: Int): Int =
if i < 10 then
val nextNodes = nodes
.flatMap(n => adjacent.map(n + _))
.filter(inBounds)
.filter(v => grid(v.i)(v.j) == i)
explore(nextNodes, i + 1)
else
nodes.size

def count(factory: Vec => Iterable[Vec]): Int = zeros.map(zero => explore(factory(zero), 1)).sum

part(1) = count(Set.apply(_))

part(2) = count(Seq.apply(_))

}

0 comments on commit f15c913

Please sign in to comment.