Find the sum of all digits that match the following digit in a circular list

The name of the pictureThe name of the pictureThe name of the pictureClash 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")








share|improve this question



























    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")








    share|improve this question























      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")








      share|improve this question













      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")










      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 7 at 15:51









      200_success

      123k14143399




      123k14143399









      asked Jun 6 at 19:01









      jrtapsell

      435112




      435112




















          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.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer













                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.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 7 at 8:15









                jrtapsell

                435112




                435112






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation