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. HttpClient Post 提交表单数据

    运行环境 .net 4.6.1 //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 var sslHandler = new HttpClientHandler() { }; ...

  2. 自定义一个JdbcTemplate(增删改数据库中表记录)

    需求: 自定义一个JdbcTemplate模板,实现增删改数据库中表记录的功能 1 package demo03; 2 3 import utils.JDBC_DBCP_Utils; 4 5 impo ...

  3. mysql主从故障跳过错误

    mysql主从故障跳过错误1.从库报错 21,22,23,25无法执行Retrieved_Gtid_Set: 265c6c2a-86ca-11ed-b07a-0242ac120002:1-25Exec ...

  4. Kotlin初学习

    Java和Kotlin数据类型对照表

  5. unittestreport生成报告介绍

    ​ unittestreport主要有以下几个作用 接口用例HTML 测试报告生成. 测试用例失败重运行. 发送测试结果及报告到邮箱. 发送报告到钉钉群. 安装unittestreport: pip3 ...

  6. 开启MySQL数据库远程连接

    为了使其余用户/计算机能访问SQL数据库,需对SQL Server进行以下配置.有以下两种方法: 方法一:bat命令修改. 新建.txt文件,添加以下内容,保存后再修改为.bat,双击.bat文件. ...

  7. VS2022在打开设计器的时候提示某变量未声明或者未赋值

    有可能是在属性中直接添加引用的dll文件,导致dll并不能被该文件稳定引用,可以删除引用,重新添加试试

  8. java 注解基础

    java 内置注解 注解 作用 描述 @Override 将覆盖父类中的方法 作用在子类的方法上 @Deprecated 代码被弃用 使用了被@Deprecated注解的代码则编译器将发出警告 @Su ...

  9. K8S-kubeadm集群安装

    K8S-kubeadm集群安装 一.环境准备 1.服务器信息 2.系统初始(所有服务器) 2.1修改主机名 hostnamectl set-hostname <主机名> 2.2添加主机ho ...

  10. org.aspectj.lang不存在,引入失败。

    问题:添加了依赖,或引入了jar包但是写aspect类时无法引入 解决办法: