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 

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

Advent of Code 2015: Day 10的更多相关文章

  1. 2015年10个最佳Web开发JavaScript库

    2015年10个最佳Web开发JavaScript库 现在的互联网可谓是无所不有,有大量的JavaScript项目开发工具充斥于网络中.我们可以参考网上的指导来获取构建代码项目的各种必要信息.如果你是 ...

  2. http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

    http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

  3. HDU 5375 Gray code(2015年多校联合 动态规划)

    题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...

  4. #VSTS日志# 2015/12/10 – 终于可以删除工作项了

    最近的更新不少,废话少说,直接上干货 定制工作项字段 本周的更新后,所有的用户都可以在vsts上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...

  5. NCPC 2015 October 10, 2015 Problem D

    NCPC 2015Problem DDisastrous DowntimeProblem ID: downtimeClaus Rebler, cc-by-saYou’re investigating ...

  6. code::blocks(版本10.05) 配置opencv2.4.3

    (1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 具体操作如下: 第一步, 配置compiler, 操作步骤为Settings  -> Compil ...

  7. 2015年10月23日JS笔记

    ECMAScript标准:JavaScript核心语法 微软:Jscript ECMAScript标准:一纸空文 JavaScript和JScritp都号称完全实现了 ECMAScript标准 W3C ...

  8. 2015年10月22日CSS学习笔记

    XHTML1.0对HTML4.0的改进 借鉴了XML的写法,语法更加严格. 把页面的内容和样式分离了,废弃了html4中的表示样式的标签和属性.推荐使用css来描述页面的样式. CSS样式的优先级 ! ...

  9. 2015第10周日CSS—3

    CSS各种居中方法 CSS的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align ...

  10. 2015第10周五CSS—2

    经常遇到的浏览器兼容性有哪些?如何解决? 1.浏览器默认的margin和padding不同.解决方案是加一个全局的*{margin:0;padding:0;}来统一. 2.IE6双边距bug:块属性标 ...

随机推荐

  1. 获取小程序toast控件

    Toast 含义 为了给当前视图显示一个浮动的显示块,与dialog不同它永远不会获得焦点 显示时间有限,根据用户设置的显示时间后自动消失 本身是个系统级别的控件,它归属系统settings,当一个a ...

  2. Android FragmentTabHost底部选项卡实现

    记录一下底部选项卡的实现,很常见的代码,大神勿嘲笑. 说一下思路,在activity底部要放上FragmentTabHost放上选项,几个无所谓,每个选项卡都对应一个fragment,点击选项卡颜色改 ...

  3. iOS开发 调试技巧

    1.普通断点 2.条件断点 3.全局断点 4.analyze 分析器 5.leaks 检查器 6.僵尸对象 7.lldb 命令 8.instruments 9.xcode视图调试

  4. CodeGym自学笔记07——入门Java书籍

    入门Java书籍 Head First Java Java:The Complete Reference,作者:Herbert Schildt   这本书对初学者也很有好处.与前一本书的主要区别在于素 ...

  5. 【面试题】ES6语法五之箭头函数

    ES6特性=>. function foo(x, y){ return x + y } var foo = (x, y) => x + y 包括一个参数列表(零个或多个参数,如果参数不是一 ...

  6. 使用 symfony 框架时 配置运行环境时 报debug 工具栏问题及 No input file specified.

    错误一: 错误二:点击跳转时:No input file specified. 解决方法:配置nginx.conf时 增加以下代码: location / { index app_dev.php; t ...

  7. 【CDH】cdh搭建遇到的坑和解决过程

    本人安装CDH时,使用的是在线安装方式,就是yum install XXX XXX XXX这种.所以安装目录都是默认的目录. Linxu:centos 7 一,启动cloudera-scm-agent ...

  8. 【Linux命令】在Linux服务器上与windows通过SCP命令互传文件时出现的问题排查过程

    1,在linux 执行 scp 1.txt adminitrator@10.10.10.10:/d:/后,报连接超时 原因:windows不支持ssh,可以安装支持SSH服务的工具,如:winsshd ...

  9. spark2.0.1源码编译

    一.编译源码步骤演示详解 . 编译spark环境要求 1.必须在linux系统下编译(以centos6.4为例) 2.编译使用的JDK版本必须是1.6以上(以JDK1.8为例) 3.编译需要使用Mav ...

  10. lucene入门&Solr

    LUCENE 1. 学习计划 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5.查询索引库 6. ...