Tested one puzzle.
Change-Id: I60720151667814f13881e16868d570282e509c77
This commit is contained in:
		
							parent
							
								
									4201e90672
								
							
						
					
					
						commit
						13a3503196
					
				
							
								
								
									
										19
									
								
								src/Utils.kt
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/Utils.kt
									
									
									
									
									
								
							@ -8,6 +8,25 @@ import java.security.MessageDigest
 | 
			
		||||
fun readInput(name: String) = File("src", "$name.txt")
 | 
			
		||||
    .readLines()
 | 
			
		||||
 | 
			
		||||
fun listOfIntegerLists(input: List<String>): ArrayList<List<Int>> {
 | 
			
		||||
    val output = ArrayList<List<Int>>()
 | 
			
		||||
    var currList = ArrayList<Int>()
 | 
			
		||||
    for(i in input)
 | 
			
		||||
    {
 | 
			
		||||
        if(i.isEmpty()){
 | 
			
		||||
            output.add(currList)
 | 
			
		||||
            currList = ArrayList<Int>()
 | 
			
		||||
        } else {
 | 
			
		||||
            currList.add(i.toInt())
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if(currList.isNotEmpty())
 | 
			
		||||
    {
 | 
			
		||||
        output.add(currList)
 | 
			
		||||
    }
 | 
			
		||||
    return output
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Converts string to md5 hash.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										23
									
								
								src/aoc2022/Day01.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/aoc2022/Day01.kt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
package aoc2022
 | 
			
		||||
 | 
			
		||||
import listOfIntegerLists
 | 
			
		||||
import println
 | 
			
		||||
import readInput
 | 
			
		||||
 | 
			
		||||
fun main() {
 | 
			
		||||
    fun part1(input: List<String>): Int {
 | 
			
		||||
        return listOfIntegerLists(input).map { it.sum() }.max()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun part2(input: List<String>): Int {
 | 
			
		||||
        return listOfIntegerLists(input).map { it.sum() }.sortedDescending().take(3).sum()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // test if implementation meets criteria from the description, like:
 | 
			
		||||
    val testInput = readInput("Day01")
 | 
			
		||||
    check(part1(testInput) == 69836)
 | 
			
		||||
 | 
			
		||||
    val input = readInput("Day01")
 | 
			
		||||
    part1(input).println()
 | 
			
		||||
    part2(input).println()
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user