首页 > 其他分享 >Advent of Code 2015: Day 10

Advent of Code 2015: Day 10

时间:2023-02-03 10:34:29浏览次数:48  
标签:count 10 Code seq buffer 2015

JP's Blog

Advent of Code 2015: Day 10

https://blog.jverkamp.com/2015/12/10/advent-of-code-day-10/

2015-12-10

Source

Part 1: Repeat a Look-and-say sequence 40 times. Return the length.

def look_and_say(seq):
    result = ''
    index = 0
    count = 0
    buffer = None

    for c in seq:
        if c == buffer:
            count += 1
        else:
            if buffer:
                result += '{}{}'.format(count, buffer)
            count = 1
            buffer = c

    result += '{}{}'.format(count, buffer)
    return result

def repeat(f, n, seq):
    for i in range(n):
        seq = f(seq)
    return seq

print(len(repeat(look_and_say, int(sys.argv[2]), sys.argv[1])))

I’ve already gone into far more detail on Look and Say sequences before. But this time, we’re doing it iteratively in Python! Basically, we just maintain a bit of state for which character we are currently counting. When we change, output; otherwise, incrememnt the change.

There are some interesting tricks you could do with regular expressions, but I doubt you can find something faster or much more elegant than this.

Part 2: Repeat 50 times.

There’s nothing particular interesting about this one. I’ve already accounted for this by passing in the second paramter on the command line.

------------------------------------------------------------------------------------------
如果你觉得文章有用,欢迎打赏

 

 

 

All posts unless otherwise mentioned are licensed under Creative Commons License

Any source code unless otherwise mentioned is licensed under the 3 clause BSD license

标签:count,10,Code,seq,buffer,2015
From: https://www.cnblogs.com/z-cm/p/17088327.html

相关文章

  • CodeForeces 1202D Print a 1337-string(构造)
    求能组成1337这个序列的串最短的串是什么这道题我们很容易想到组合数,我可以有限考虑选择3,因为只有3是两个,这样可以使这个串尽可能的短。但是选择3是不能满足我们组成任意个......
  • 软测路这样规划,3年拿到别人10年的工作成就
    “每天都是重复、单调的工作,收入不理想,想跳槽无力,学习又没有动力和方向,不知道未来的发展在哪里,甚至想转行·····”做测试久了,很多人都有诸如此类的疑惑,不想一直停留在......
  • HDU1098 Ignatius's puzzle (数学归纳法)
    Description:Ignatiusispooratmath,hefallsacrossapuzzleproblem,sohehasnochoicebuttoappealtoEddy.thisproblemdescribesthat:......
  • UVA 11754 code feat (中国剩余定理+暴力枚举)
    题意:给出C,SC,SC,S,......
  • Codeforces Round #596 D
    找到a[i]*a[j]=x^k符合这个式子的有多少种组合。分解质因子来做就行了AC代码:#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<s......
  • Codeforces-343D Water Tree(树链剖分)
    Description:MadscientistMikehasconstructedarootedtree,whichconsistsof n vertices.Eachvertexisareservoirwhichcanbeeitheremptyorfilledw......
  • Codeforces Round #596 B2. TV Subscriptions
    题意就是让你在N个数中找到D个连续的数,使这D个数中不同的数最小。hard数据较大,优化到nlogn才能过。具体怎么优化看代码吧AC代码:#include<cstdio>#include<cstring>......
  • Codeforces Round #596 C. p-binary
    给定N和p,让你找到满足2^x+p最少有多少不同的项。就把N转成二进制然后枚举P的个数就是答案,昨天特判没写好,今天早上起来发现被卡掉了。rank又出1000了。AC代码:#include<......
  • Educational Codeforces Round 75 D. Salary Changing(二分)
    题意就是:给n个人发工资,总钱数为s。每个人有一个工资范围。要求一个发工资方案,使得工资中位数最大,求这个中位数。考虑到中位数最大,于是我们可以二分。但是每个人的工资......
  • Educational Codeforces Round 75 C Minimize The Integer
    这道题的意思就是给出一个由数字组成的字符串,相邻的数字可以互换位置,但是如果相邻的为同奇同偶这样就不能交换。让我们求交换任意次数可以产生的最小数。这条限制就是说......