Find the sum of all digits that match the following digit in a circular list
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
The challenge
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
For example:
- 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
- 1111 produces 4 because each digit (all 1) matches the next.
- 1234 produces 0 because no digit matches the next.
- 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
My Logic
I keep track of the first item, the last seen item and the total.
I pass over the list, adding the items that match the last seen item.
I handle the first digit by adding the first to the total if it matches the last of the list.
import kotlinx.cinterop.*
import platform.posix.*
const val FILEPATH = "data.dat"
const val ZERO_CODEPOINT = '0'.toInt()
fun main(args: Array<String>)
withFile(FILEPATH) fd ->
val (first, last, total) = generateSequence
val value = fgetc(fd)
when (value)
-1 -> null
else -> value
.map it - ZERO_CODEPOINT
.foldIndexed(listOf(-1, -1, 0)) index, (first, last, total), value ->
val newTotal = if (last == value) total + value else total
val newFirst = if (first == -1) value else first
listOf(newFirst, value, newTotal)
val result = if (first == last) total + first else total
println(result)
fun <T> withFile(filename: String, block: (CPointer<FILE>) -> T): T
val fd = fopen(filename, "r")
if (fd != null)
val ret = block(fd)
fclose(fd)
return ret
else
throw AssertionError("File not found: $filename")
programming-challenge circular-list kotlin
add a comment |Â
up vote
2
down vote
favorite
The challenge
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
For example:
- 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
- 1111 produces 4 because each digit (all 1) matches the next.
- 1234 produces 0 because no digit matches the next.
- 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
My Logic
I keep track of the first item, the last seen item and the total.
I pass over the list, adding the items that match the last seen item.
I handle the first digit by adding the first to the total if it matches the last of the list.
import kotlinx.cinterop.*
import platform.posix.*
const val FILEPATH = "data.dat"
const val ZERO_CODEPOINT = '0'.toInt()
fun main(args: Array<String>)
withFile(FILEPATH) fd ->
val (first, last, total) = generateSequence
val value = fgetc(fd)
when (value)
-1 -> null
else -> value
.map it - ZERO_CODEPOINT
.foldIndexed(listOf(-1, -1, 0)) index, (first, last, total), value ->
val newTotal = if (last == value) total + value else total
val newFirst = if (first == -1) value else first
listOf(newFirst, value, newTotal)
val result = if (first == last) total + first else total
println(result)
fun <T> withFile(filename: String, block: (CPointer<FILE>) -> T): T
val fd = fopen(filename, "r")
if (fd != null)
val ret = block(fd)
fclose(fd)
return ret
else
throw AssertionError("File not found: $filename")
programming-challenge circular-list kotlin
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
The challenge
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
For example:
- 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
- 1111 produces 4 because each digit (all 1) matches the next.
- 1234 produces 0 because no digit matches the next.
- 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
My Logic
I keep track of the first item, the last seen item and the total.
I pass over the list, adding the items that match the last seen item.
I handle the first digit by adding the first to the total if it matches the last of the list.
import kotlinx.cinterop.*
import platform.posix.*
const val FILEPATH = "data.dat"
const val ZERO_CODEPOINT = '0'.toInt()
fun main(args: Array<String>)
withFile(FILEPATH) fd ->
val (first, last, total) = generateSequence
val value = fgetc(fd)
when (value)
-1 -> null
else -> value
.map it - ZERO_CODEPOINT
.foldIndexed(listOf(-1, -1, 0)) index, (first, last, total), value ->
val newTotal = if (last == value) total + value else total
val newFirst = if (first == -1) value else first
listOf(newFirst, value, newTotal)
val result = if (first == last) total + first else total
println(result)
fun <T> withFile(filename: String, block: (CPointer<FILE>) -> T): T
val fd = fopen(filename, "r")
if (fd != null)
val ret = block(fd)
fclose(fd)
return ret
else
throw AssertionError("File not found: $filename")
programming-challenge circular-list kotlin
The challenge
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
For example:
- 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
- 1111 produces 4 because each digit (all 1) matches the next.
- 1234 produces 0 because no digit matches the next.
- 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
My Logic
I keep track of the first item, the last seen item and the total.
I pass over the list, adding the items that match the last seen item.
I handle the first digit by adding the first to the total if it matches the last of the list.
import kotlinx.cinterop.*
import platform.posix.*
const val FILEPATH = "data.dat"
const val ZERO_CODEPOINT = '0'.toInt()
fun main(args: Array<String>)
withFile(FILEPATH) fd ->
val (first, last, total) = generateSequence
val value = fgetc(fd)
when (value)
-1 -> null
else -> value
.map it - ZERO_CODEPOINT
.foldIndexed(listOf(-1, -1, 0)) index, (first, last, total), value ->
val newTotal = if (last == value) total + value else total
val newFirst = if (first == -1) value else first
listOf(newFirst, value, newTotal)
val result = if (first == last) total + first else total
println(result)
fun <T> withFile(filename: String, block: (CPointer<FILE>) -> T): T
val fd = fopen(filename, "r")
if (fd != null)
val ret = block(fd)
fclose(fd)
return ret
else
throw AssertionError("File not found: $filename")
programming-challenge circular-list kotlin
edited Jul 7 at 15:51
200_success
123k14143399
123k14143399
asked Jun 6 at 19:01
jrtapsell
435112
435112
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
foldIndexed -> fold
The foldIndexed line can be replaced with:
.fold(Carry(-1, -1, 0)) { (first, last, total), value ->
This is because the indexed part is unused
Using a data class
A data class can be used to make the values passed along the fold operation more obvious.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
foldIndexed -> fold
The foldIndexed line can be replaced with:
.fold(Carry(-1, -1, 0)) { (first, last, total), value ->
This is because the indexed part is unused
Using a data class
A data class can be used to make the values passed along the fold operation more obvious.
add a comment |Â
up vote
0
down vote
foldIndexed -> fold
The foldIndexed line can be replaced with:
.fold(Carry(-1, -1, 0)) { (first, last, total), value ->
This is because the indexed part is unused
Using a data class
A data class can be used to make the values passed along the fold operation more obvious.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
foldIndexed -> fold
The foldIndexed line can be replaced with:
.fold(Carry(-1, -1, 0)) { (first, last, total), value ->
This is because the indexed part is unused
Using a data class
A data class can be used to make the values passed along the fold operation more obvious.
foldIndexed -> fold
The foldIndexed line can be replaced with:
.fold(Carry(-1, -1, 0)) { (first, last, total), value ->
This is because the indexed part is unused
Using a data class
A data class can be used to make the values passed along the fold operation more obvious.
answered Jun 7 at 8:15
jrtapsell
435112
435112
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195978%2ffind-the-sum-of-all-digits-that-match-the-following-digit-in-a-circular-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password