Skip to content

Commit

Permalink
Cosmetic changes (indentation mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
flvrone committed Dec 24, 2024
1 parent 5aeb840 commit e4d38fa
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 79 deletions.
7 changes: 3 additions & 4 deletions 3/corrupted_mul.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ def parseAndSum(command: String): Int =
sumOfMults(parsePairs(command))

def parsePairs(command: String): List[(Int, Int)] =
MulPattern.findAllMatchIn(command).toList.map(
(patternMatch) =>
(patternMatch.group(1).toInt, patternMatch.group(2).toInt)
)
MulPattern.findAllMatchIn(command).toList.map(ptrnMatch =>
(ptrnMatch.group(1).toInt, ptrnMatch.group(2).toInt)
)

def sumOfMults(pairs: List[(Int, Int)]): Int =
pairs.map((a, b) => a * b).reduceLeft(_ + _)
13 changes: 6 additions & 7 deletions 3/dos_and_donts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ def parseAndSum(command: String): Int =
sumOfMults(parsePairs(filterCommand(command)))

def parsePairs(command: String): List[(Int, Int)] =
MulPattern.findAllMatchIn(command).toList.map(
(patternMatch) =>
(patternMatch.group(1).toInt, patternMatch.group(2).toInt)
)
MulPattern.findAllMatchIn(command).toList.map(ptrnMatch =>
(ptrnMatch.group(1).toInt, ptrnMatch.group(2).toInt)
)

def sumOfMults(pairs: List[(Int, Int)]): Int =
pairs.map((a, b) => a * b).reduceLeft(_ + _)

def filterCommand(command: String): String =
command .split(DoPattern)
.map(_.split(DontPattern, 2).apply(0))
.mkString
command.split(DoPattern)
.map(_.split(DontPattern, 2).apply(0))
.mkString
4 changes: 2 additions & 2 deletions 4/x_cross_mas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def modifyCoordsInDirection(row: Int, col: Int, dir: Direction): (Int, Int) =

@tailrec
def matchMatrixInDirectionFromPos(
word: String, matrix: CharMatrix2D, dir: Direction, row: Int, col: Int
): Boolean =
word: String, matrix: CharMatrix2D, dir: Direction, row: Int, col: Int
): Boolean =
if word.isEmpty() then true
else if matrix.charAtIs(row, col, word.head) then
val (nextRow, nextCol) = modifyCoordsInDirection(row, col, dir)
Expand Down
8 changes: 4 additions & 4 deletions 4/xmas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def matchesInCharMatrix(word: String, matrix: CharMatrix2D): Int =
if char == startChar
do
matchCount += Direction.values.count(dir =>
matchMatrixInDirectionFromPos(word, matrix, dir, row, col)
)
matchMatrixInDirectionFromPos(word, matrix, dir, row, col)
)

matchCount

Expand All @@ -57,8 +57,8 @@ def modifyCoordsInDirection(row: Int, col: Int, dir: Direction): (Int, Int) =

@tailrec
def matchMatrixInDirectionFromPos(
word: String, matrix: CharMatrix2D, dir: Direction, row: Int, col: Int
): Boolean =
word: String, matrix: CharMatrix2D, dir: Direction, row: Int, col: Int
): Boolean =
if word.isEmpty() then true
else if matrix.charAtIs(row, col, word.head) then
val (nextRow, nextCol) = modifyCoordsInDirection(row, col, dir)
Expand Down
18 changes: 9 additions & 9 deletions 5/print_queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def main1(inputFileName: String): Unit =
val queues = parseQueues(queueLines)

println(
queues.filter(isQueueCorrect(_, rulesMap))
.map(middleElement(_))
.reduce(_ + _)
)
queues.filter(isQueueCorrect(_, rulesMap))
.map(middleElement(_))
.reduce(_ + _)
)

def buildRulesMap(rules: Seq[String]): Map[Int, Set[Int]] =
rules.map(_.split('|'))
Expand All @@ -28,11 +28,11 @@ def parseQueues(queues: Seq[String]): List[Vector[Int]] =

def isQueueIncorrect[T](queue: Vector[T], rulesMap: Map[T, Set[T]]): Boolean =
queue.zipWithIndex.exists((elem, idx) =>
if rulesMap.contains(elem) then
val elemsGoingAfter = rulesMap.apply(elem)
queue.take(idx).exists(elemsGoingAfter.apply(_))
else false
)
if rulesMap.contains(elem) then
val elemsGoingAfter = rulesMap.apply(elem)
queue.take(idx).exists(elemsGoingAfter.apply(_))
else false
)

def isQueueCorrect[T](queue: Vector[T], rulesMap: Map[T, Set[T]]): Boolean =
!isQueueIncorrect(queue, rulesMap)
Expand Down
20 changes: 10 additions & 10 deletions 5/queue_order_fixer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def main2(inputFileName: String): Unit =
val isOrderCorrectPredicate = comparisonBuilder(rulesMap)

println(
queues.filter(isQueueIncorrect(_, rulesMap))
.map(_.sortWith(isOrderCorrectPredicate))
.map(middleElement(_))
.reduce(_ + _)
)
queues.filter(isQueueIncorrect(_, rulesMap))
.map(_.sortWith(isOrderCorrectPredicate))
.map(middleElement(_))
.reduce(_ + _)
)

def buildRulesMap(rules: Seq[String]): Map[Int, Set[Int]] =
rules.map(_.split('|'))
Expand All @@ -33,11 +33,11 @@ def parseQueues(queues: Seq[String]): List[Vector[Int]] =

def isQueueIncorrect[T](queue: Vector[T], rulesMap: Map[T, Set[T]]): Boolean =
queue.zipWithIndex.exists((elem, idx) =>
if rulesMap.contains(elem) then
val elemsGoingAfter = rulesMap.apply(elem)
queue.take(idx).exists(elemsGoingAfter.apply(_))
else false
)
if rulesMap.contains(elem) then
val elemsGoingAfter = rulesMap.apply(elem)
queue.take(idx).exists(elemsGoingAfter.apply(_))
else false
)

def isQueueCorrect[T](queue: Vector[T], rulesMap: Map[T, Set[T]]): Boolean =
!isQueueIncorrect(queue, rulesMap)
Expand Down
22 changes: 11 additions & 11 deletions 7/equation_builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def main1(inputFileName: String): Unit =
val lines: Seq[String] = os.read.lines(path)

val equations = (
for line <- lines yield
val pair = line.split(":\\s+", 2)
(
pair.apply(0).toLong,
pair.apply(1).split("\\s+").map(_.toInt).toList
)
).toList
for line <- lines yield
val pair = line.split(":\\s+", 2)
(
pair.apply(0).toLong,
pair.apply(1).split("\\s+").map(_.toInt).toList
)
).toList

println(checkAndSumEquationResults(equations))

Expand All @@ -40,8 +40,8 @@ def calculate(numbers: List[Int], operators: List[Operator]): Long =

@tailrec
def recursiveCalculate(
currentValue: Long, numbers: List[Int], operators: List[Operator]
): Long =
currentValue: Long, numbers: List[Int], operators: List[Operator]
): Long =
if numbers.isEmpty then currentValue
else
val newValue = applyOperator(operators.head, currentValue, numbers.head)
Expand All @@ -58,8 +58,8 @@ def repetitivePermutations[T](length: Int, elements: List[T]): List[List[T]] =
// Returns new lists, created by prepending every element to the container list,
// and then iterating upon those new lists.
def prependEach[T](
elements: List[T], containerList: List[T] = List(), iterations: Int = 1
): List[List[T]] =
elements: List[T], containerList: List[T] = List(), iterations: Int = 1
): List[List[T]] =
val newLists = for
elem <- elements
yield
Expand Down
22 changes: 11 additions & 11 deletions 7/extra_equation_builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def main2(inputFileName: String): Unit =
val lines: Seq[String] = os.read.lines(path)

val equations = (
for line <- lines yield
val pair = line.split(":\\s+", 2)
(
pair.apply(0).toLong,
pair.apply(1).split("\\s+").map(_.toInt).toList
)
).toList
for line <- lines yield
val pair = line.split(":\\s+", 2)
(
pair.apply(0).toLong,
pair.apply(1).split("\\s+").map(_.toInt).toList
)
).toList

println(checkAndSumEquationResults(equations))

Expand All @@ -40,8 +40,8 @@ def calculate(numbers: List[Int], operators: List[Operator]): Long =

@tailrec
def recursiveCalculate(
currentValue: Long, numbers: List[Int], operators: List[Operator]
): Long =
currentValue: Long, numbers: List[Int], operators: List[Operator]
): Long =
if numbers.isEmpty then currentValue
else
val newValue = applyOperator(operators.head, currentValue, numbers.head)
Expand All @@ -59,8 +59,8 @@ def repetitivePermutations[T](length: Int, elements: List[T]): List[List[T]] =
// Returns new lists, created by prepending every element to the container list,
// and then iterating upon those new lists.
def prependEach[T](
elements: List[T], containerList: List[T] = List(), iterations: Int = 1
): List[List[T]] =
elements: List[T], containerList: List[T] = List(), iterations: Int = 1
): List[List[T]] =
val newLists = for
elem <- elements
yield
Expand Down
10 changes: 5 additions & 5 deletions 8/antinode_check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def findAntinodes(positions: List[Coord], matrix: Char2DMatrix): List[Coord] =

@tailrec
def recursiveFindAntinodes(
currentPosition: Coord, positions: List[Coord], matrix: Char2DMatrix,
foundAntinodes: List[Coord] = List()
): List[Coord] =
currentPosition: Coord, positions: List[Coord], matrix: Char2DMatrix,
foundAntinodes: List[Coord] = List()
): List[Coord] =
if positions.isEmpty then foundAntinodes
else
val newAntinodes =
Expand All @@ -70,8 +70,8 @@ def recursiveFindAntinodes(
)

def calculateAntinodesFor(
pos1: Coord, pos2: Coord, matrix: Char2DMatrix
): List[Coord] =
pos1: Coord, pos2: Coord, matrix: Char2DMatrix
): List[Coord] =
val coord1 = Coord(
-2 * (pos1.row - pos2.row) + pos1.row,
-2 * (pos1.col - pos2.col) + pos1.col
Expand Down
16 changes: 8 additions & 8 deletions 8/extra_antinode_check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def findAntinodes(positions: List[Coord], matrix: Char2DMatrix): List[Coord] =

@tailrec
def recursiveFindAntinodes(
currentPosition: Coord, positions: List[Coord], matrix: Char2DMatrix,
foundAntinodes: List[Coord] = List()
): List[Coord] =
currentPosition: Coord, positions: List[Coord], matrix: Char2DMatrix,
foundAntinodes: List[Coord] = List()
): List[Coord] =
if positions.isEmpty then foundAntinodes
else
val newAntinodes =
Expand All @@ -70,15 +70,15 @@ def recursiveFindAntinodes(
)

def calculateAntinodesFor(
pos1: Coord, pos2: Coord, matrix: Char2DMatrix
): List[Coord] =
pos1: Coord, pos2: Coord, matrix: Char2DMatrix
): List[Coord] =
recursiveCalculateAntinodesFor(pos1, pos2, matrix)

@tailrec
def recursiveCalculateAntinodesFor(
pos1: Coord, pos2: Coord, matrix: Char2DMatrix, muliplier: Int = -1,
foundAntinodes: List[Coord] = List()
): List[Coord] =
pos1: Coord, pos2: Coord, matrix: Char2DMatrix, muliplier: Int = -1,
foundAntinodes: List[Coord] = List()
): List[Coord] =
val coord1 = Coord(
muliplier * (pos1.row - pos2.row) + pos1.row,
muliplier * (pos1.col - pos2.col) + pos1.col
Expand Down
4 changes: 2 additions & 2 deletions 9/disk_fragmenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def fragmentMap(map: Vector[Int]): Vector[Int] =

@tailrec
def recursiveFragmentMap(
map: Array[Int], emptyIndex: Int, fileIndex: Int
): Array[Int] =
map: Array[Int], emptyIndex: Int, fileIndex: Int
): Array[Int] =
if emptyIndex >= fileIndex then map
else
map(emptyIndex) = map(fileIndex)
Expand Down
12 changes: 6 additions & 6 deletions 9/disk_space_optimizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def findPrevFileRange(map: Array[Int], currentIndex: Int): Option[Range] =

@tailrec
def findFileRangeFromEnd(
map: Array[Int], currentIndex: Int, foundRangeEnd: Int, fileId: Int
): Range =
map: Array[Int], currentIndex: Int, foundRangeEnd: Int, fileId: Int
): Range =
if currentIndex < 0 then (0 to foundRangeEnd)
else
if map.apply(currentIndex) != fileId then
Expand All @@ -81,8 +81,8 @@ def findFileRangeFromEnd(

@tailrec
def findFreeSpaceForRange(
map: Array[Int], range: Range, currentIndex: Int = 0
): Option[Range] =
map: Array[Int], range: Range, currentIndex: Int = 0
): Option[Range] =
if currentIndex >= range.start then None
else if map.apply(currentIndex) >= 0 then
findFreeSpaceForRange(map, range, currentIndex + 1)
Expand All @@ -96,8 +96,8 @@ def findFreeSpaceForRange(

@tailrec
def findFreeSpaceRangeEnd(
map: Array[Int], currentIndex: Int, rangeStartIndex: Int, maxLength: Int
): Int =
map: Array[Int], currentIndex: Int, rangeStartIndex: Int, maxLength: Int
): Int =
if currentIndex >= map.length then map.length - 1
else if map.apply(currentIndex) >= 0 then currentIndex - 1
else if (rangeStartIndex to currentIndex).length >= maxLength then currentIndex
Expand Down

0 comments on commit e4d38fa

Please sign in to comment.