day 15 puzzle 1
This commit is contained in:
		
							
								
								
									
										42
									
								
								src/day15/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/day15/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | package day15 | ||||||
|  |  | ||||||
|  | import scala.io.{BufferedSource, Source} | ||||||
|  |  | ||||||
|  | object Puzzle1 { | ||||||
|  |   var values: Array[String] = Array.empty | ||||||
|  |   def loadInput(path: String): Unit = { | ||||||
|  |     val source: BufferedSource = Source.fromFile(path) | ||||||
|  |     val line: String = source.getLines().mkString | ||||||
|  |  | ||||||
|  |     values = line.split(",") | ||||||
|  |  | ||||||
|  |     source.close() | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   def hash(str: String): Int = { | ||||||
|  |     var value: Int = 0 | ||||||
|  |     for (c: Char <- str) { | ||||||
|  |       value += c | ||||||
|  |       value *= 17 | ||||||
|  |       value %= 256 | ||||||
|  |     } | ||||||
|  |     return value | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   def solve(path: String): Int = { | ||||||
|  |     loadInput(path) | ||||||
|  |  | ||||||
|  |     var solution: Int = 0 | ||||||
|  |  | ||||||
|  |     for (value: String <- values) { | ||||||
|  |       solution += hash(value) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return solution | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   def main(args: Array[String]): Unit = { | ||||||
|  |     val solution: Int = solve("./res/day15/input1.txt") | ||||||
|  |     println(solution) | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								tests/day15/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								tests/day15/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | package day15 | ||||||
|  |  | ||||||
|  | import org.scalatest.funsuite.AnyFunSuite | ||||||
|  |  | ||||||
|  | class Puzzle1Test extends AnyFunSuite { | ||||||
|  |   test("Puzzle1.solve") { | ||||||
|  |     assert(Puzzle1.solve("tests_res/day15/input1.txt") == 1320) | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								tests_res/day15/input1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								tests_res/day15/input1.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 | ||||||
		Reference in New Issue
	
	Block a user