-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.kt
38 lines (31 loc) · 1.13 KB
/
Day02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* Copyright (c) 2017 by Todd Ginsberg
*/
package com.ginsberg.advent2017
/**
* AoC 2017, Day 2
*
* Problem Description: http://adventofcode.com/2017/day/2
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2017/day2/
*/
class Day02(stringInput: List<String>) {
private val input: List<List<Int>> = parseInput(stringInput)
fun solvePart1(): Int =
input
.map { it.sorted() }
.sumBy { it.last() - it.first() }
fun solvePart2(): Int =
input
.map { findDivisiblePairForRow(it) }
.sumBy { it.first / it.second }
// Given a row, generate all possible Pair<Int,Int>, filter out
// pairs matched with self, and filter down to the first one
// that has a divisible pair.
private fun findDivisiblePairForRow(row: List<Int>): Pair<Int, Int> =
row
.flatMap { x -> row.map { Pair(x, it) } }
.filter { it.first != it.second }
.first { it.first % it.second == 0 }
private fun parseInput(s: List<String>): List<List<Int>> =
s.map { it.split(Constants.WHITESPACE).map { it.toInt() } }
}