Compare commits

..

No commits in common. "a170fb9b50f68a9ab0b47a59b271a99b48a3d4a1" and "86ca2c95af88fb269f97db9928d71ae7f132f452" have entirely different histories.

8 changed files with 8 additions and 235 deletions

View File

@ -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 I will try and do some problems (probably not all of them) in Scala
## Progress: ## Progress:
#### Stars: 14 / 50 #### Stars: 12 / 50
| Mon | Tue | Wed | Thu | Fri | Sat | Sun | | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:| |:-----------------:|:-----------------:|:-----------------:|:---:|:-----------------:|:-----------------:|:-----------------:|
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: | | | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8 | 9 | 10 | | 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | | | 25 | | | | | | |

View File

@ -1,47 +0,0 @@
package day7
import scala.collection.mutable
class Hand(var cards: Array[Int], val bid: Int) {
def getType(): Int = {
val cardsMap: mutable.Map[Int, Int] = new mutable.HashMap()
for (card: Int <- cards) {
if (!cardsMap.contains(card)) {
cardsMap(card) = 0
}
cardsMap(card) += 1
}
val keys: Array[Int] = cardsMap.keys.toArray
val values: Array[Int] = cardsMap.values.toArray
if (keys.length == 1) {
return 6
}
if (keys.length == 2) {
if (values.contains(4)) return 5
if (values.contains(3) && values.contains(2)) return 4
}
if (values.contains(3)) return 3
if (values.contains(2)) {
if (values.count(v => v == 2) == 2) return 2
return 1
}
return 0
}
def isBetter(hand: Hand): Boolean = {
val t1: Int = getType()
val t2: Int = hand.getType()
if (t1 > t2) return true
if (t1 < t2) return false
for ((c1: Int, c2: Int) <- cards.zip(hand.cards)) {
if (c1 != c2) {
return c1 > c2
}
}
return false
}
override def toString: String = s"Hand(${cards.mkString}, $bid, ${getType()})"
}

View File

@ -1,65 +0,0 @@
package day7
import scala.collection.mutable
class Hand2(var cards: Array[Int], val bid: Int) {
def getType(): Int = {
val cardsMap: mutable.Map[Int, Int] = new mutable.HashMap()
val jokers: Int = cards.count(c => c == 0)
for (card: Int <- cards) {
if (card != 0) {
if (!cardsMap.contains(card)) {
cardsMap(card) = 0
}
cardsMap(card) += 1
}
}
val keys: Array[Int] = cardsMap.keys.toArray
val values: Array[Int] = cardsMap.values.toArray
if (jokers == 5) return 6
// Five of a kind
for (value: Int <- values) {
if (value + jokers == 5) return 6
}
// Four of a kind
for (value: Int <- values) {
if (value + jokers == 4) return 5
}
// Full house
val sortedValues: Array[Int] = values.sorted(Ordering.Int.reverse)
if (sortedValues(0) + sortedValues(1) + jokers >= 5) return 4
// Three of a kind
if (sortedValues(0) + jokers >= 3) return 3
// Two pairs
if (sortedValues(0) + sortedValues(1) + jokers >= 4) return 2
// One pair
if (sortedValues(0) + jokers >= 2) return 1
return 0
}
def isBetter(hand: Hand2): Boolean = {
val t1: Int = getType()
val t2: Int = hand.getType()
if (t1 > t2) return true
if (t1 < t2) return false
for ((c1: Int, c2: Int) <- cards.zip(hand.cards)) {
if (c1 != c2) {
return c1 > c2
}
}
return false
}
override def toString: String = s"Hand(${cards.mkString}, $bid, ${getType()})"
}

View File

@ -1,46 +0,0 @@
package day7
import scala.io.{BufferedSource, Source}
object Puzzle1 {
val CARDS: Array[Int] = Array('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A')
var hands: Array[Hand] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val lines: Array[String] = source.getLines().toArray
hands = new Array(lines.length)
for ((line: String, i: Int) <- lines.zipWithIndex) {
val parts: Array[String] = line.split(" ")
val cards: Array[Int] = parts(0).map(c => CARDS.indexOf(c)).toArray
val bid: Int = parts(1).toInt
hands(i) = new Hand(cards, bid)
}
source.close()
}
def sortHands(): Unit = {
hands = hands.sortWith((h1: Hand, h2: Hand) => h2.isBetter(h1))
}
def solve(path: String): Int = {
loadInput(path)
sortHands()
var solution: Int = 0
for ((hand: Hand, i: Int) <- hands.zipWithIndex) {
solution += hand.bid * (i+1)
}
return solution
}
def main(args: Array[String]): Unit = {
val solution: Int = solve("./res/day7/input1.txt")
println(solution)
}
}

View File

@ -1,46 +0,0 @@
package day7
import scala.io.{BufferedSource, Source}
object Puzzle2 {
val CARDS: Array[Int] = Array('J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A')
var hands: Array[Hand2] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val lines: Array[String] = source.getLines().toArray
hands = new Array(lines.length)
for ((line: String, i: Int) <- lines.zipWithIndex) {
val parts: Array[String] = line.split(" ")
val cards: Array[Int] = parts(0).map(c => CARDS.indexOf(c)).toArray
val bid: Int = parts(1).toInt
hands(i) = new Hand2(cards, bid)
}
source.close()
}
def sortHands(): Unit = {
hands = hands.sortWith((h1: Hand2, h2: Hand2) => h2.isBetter(h1))
}
def solve(path: String): Int = {
loadInput(path)
sortHands()
var solution: Int = 0
for ((hand: Hand2, i: Int) <- hands.zipWithIndex) {
solution += hand.bid * (i+1)
}
return solution
}
def main(args: Array[String]): Unit = {
val solution: Int = solve("./res/day7/input1.txt")
println(solution)
}
}

View File

@ -1,9 +0,0 @@
package day7
import org.scalatest.funsuite.AnyFunSuite
class Puzzle1Test extends AnyFunSuite {
test("Puzzle1.solve") {
assert(Puzzle1.solve("tests_res/day7/input1.txt") == 6440)
}
}

View File

@ -1,9 +0,0 @@
package day7
import org.scalatest.funsuite.AnyFunSuite
class Puzzle2Test extends AnyFunSuite {
test("Puzzle2.solve") {
assert(Puzzle2.solve("tests_res/day7/input1.txt") == 5905)
}
}

View File

@ -1,5 +0,0 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483