day 4 puzzle 1
This commit is contained in:
		
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							| @@ -5,11 +5,11 @@ This repo contains my attempt at this year's Advent of Code (2023) | ||||
| I will try and do some problems (probably not all of them) in Scala | ||||
|  | ||||
| ## Progress: | ||||
| #### Stars: 6 / 50 | ||||
| | Mon | Tue | Wed | Thu |        Fri        |        Sat        |        Sun        | | ||||
| |:---:|:---:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:| | ||||
| |     |     |     |     | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: | | ||||
| |  4  |  5  |  6  |  7  |         8         |         9         |        10         | | ||||
| | 11  | 12  | 13  | 14  |        15         |        16         |        17         | | ||||
| | 18  | 19  | 20  | 21  |        22         |        23         |        24         | | ||||
| | 25  |     |     |     |                   |                   |                   | | ||||
| #### Stars: 7 / 50 | ||||
| |     Mon     | Tue | Wed | Thu |        Fri        |        Sat        |        Sun        | | ||||
| |:-----------:|:---:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:| | ||||
| |             |     |     |     | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: | | ||||
| | 4<br>:star: |  5  |  6  |  7  |         8         |         9         |        10         | | ||||
| |     11      | 12  | 13  | 14  |        15         |        16         |        17         | | ||||
| |     18      | 19  | 20  | 21  |        22         |        23         |        24         | | ||||
| |     25      |     |     |     |                   |                   |                   | | ||||
							
								
								
									
										44
									
								
								src/day4/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/day4/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| package day4 | ||||
|  | ||||
| import scala.io.{BufferedSource, Source} | ||||
|  | ||||
| object Puzzle1 { | ||||
|   case class Card(validNumbers: Array[Int], myNumbers: Array[Int]) | ||||
|  | ||||
|   var cards: Array[Card] = Array.empty | ||||
|  | ||||
|   def loadInput(path: String): Unit = { | ||||
|     val source: BufferedSource = Source.fromFile(path) | ||||
|  | ||||
|     val lines: Array[String] = source.getLines().toArray | ||||
|     cards = new Array(lines.length) | ||||
|  | ||||
|     for ((line: String, i: Int) <- lines.zipWithIndex) { | ||||
|       val parts: Array[String] = line.split(": +")(1).split(" +\\| +") | ||||
|       val validNumbers: Array[Int] = parts(0).split(" +").map(n => n.toInt) | ||||
|       val myNumbers: Array[Int] = parts(1).split(" +").map(n => n.toInt) | ||||
|       cards(i) = Card(validNumbers, myNumbers) | ||||
|     } | ||||
|  | ||||
|     source.close() | ||||
|   } | ||||
|  | ||||
|   def computeGain(): Int = { | ||||
|     var gain: Int = 0 | ||||
|     for (card: Card <- cards) { | ||||
|       val validCount: Int = card.validNumbers.intersect(card.myNumbers).length | ||||
|       if (validCount >= 1) { | ||||
|         gain += Math.pow(2, validCount-1).toInt | ||||
|       } | ||||
|     } | ||||
|     return gain | ||||
|   } | ||||
|   def solve(path: String): Int = { | ||||
|     loadInput(path) | ||||
|     return computeGain() | ||||
|   } | ||||
|   def main(args: Array[String]): Unit = { | ||||
|     val solution: Int = solve("res/day4/input1.txt") | ||||
|     println(solution) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										9
									
								
								tests/day4/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								tests/day4/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| package day4 | ||||
|  | ||||
| import org.scalatest.funsuite.AnyFunSuite | ||||
|  | ||||
| class Puzzle1Test extends AnyFunSuite { | ||||
|   test("Puzzle1.solve") { | ||||
|     assert(Puzzle1.solve("tests_res/day4/input1.txt") == 13) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										6
									
								
								tests_res/day4/input1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								tests_res/day4/input1.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53 | ||||
| Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 | ||||
| Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1 | ||||
| Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83 | ||||
| Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 | ||||
| Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 | ||||
		Reference in New Issue
	
	Block a user