-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.kt
31 lines (26 loc) · 785 Bytes
/
Day01.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
/*
* Copyright (c) 2017 by Todd Ginsberg
*/
package com.ginsberg.advent2017
/**
* AoC 2017, Day 1
*
* Problem Description: http://adventofcode.com/2017/day/1
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2017/day1/
*/
class Day01(private val input: String) {
fun solvePart1(): Int =
(input + input[0]) // Circular list, re-add [0] to the end so they pair up.
.zipWithNext()
.filter { it.first == it.second }
.map { it.first.asDigit() }
.sum()
fun solvePart2(): Int {
val half = input.length / 2
return input
.subSequence(0, half)
.filterIndexed { i, c -> c == input[i + half] }
.map { it.asDigit() * 2 }
.sum()
}
}