-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rb
67 lines (60 loc) · 2.21 KB
/
day10.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding: utf-8
# http://AdventOfCode.com/
# --- Day 10: Elves Look, Elves Say ---
#
# Today, the Elves are playing a game called look-and-say. They take turns making sequences by
# reading aloud the previous sequence and using that reading as the next sequence. For example,
# 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s).
#
# Look-and-say sequences are generated iteratively, using the previous value as input for the next
# step. For each step, take the previous value, and replace each run of digits (like 111) with the
# number of digits (3) followed by the digit itself (1).
#
# For example:
#
# 1 becomes 11 (1 copy of digit 1).
# 11 becomes 21 (2 copies of digit 1).
# 21 becomes 1211 (one 2 followed by one 1).
# 1211 becomes 111221 (one 1, one 2, and two 1s).
# 111221 becomes 312211 (three 1s, two 2s, and one 1).
# Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result?
#
# Your puzzle input is 3113322113.
require_relative 'input'
day = /day(\d+)\.rb/.match(__FILE__)[1].to_i
# input = Input.for_day(day)
input = '3113322113'
# star 1
40.times { input = input.gsub(/((.)\2*)/) {|m| "#{$1.length}#{$2}" } }
puts input.length
# star 2
# --- Part Two ---
#
# Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway
# of Conway's Game of Life fame).
# https://www.youtube.com/watch?v=ea7lJkEhytA
#
# Now, starting again with the digits in your puzzle input, apply this process 50 times. What is
# the length of the new result?
10.times { input = input.gsub(/((.)\2*)/) {|m| "#{$1.length}#{$2}" } }
puts input.length
# Saw a "challenge" on reddit:
# https://www.reddit.com/r/adventofcode/comments/3w7yrk/challenge_how_high_can_you_go_in_under_1_minute/
input = '3113322113'
puts "resetting for a 1 minute run"
iters = 0
require 'timeout'
started = Time.now
begin
Timeout::timeout(60) do
loop do
input = input.gsub(/((.)\2*)/) {|m| "#{$1.length}#{$2}" }
iters += 1
end
end
rescue Timeout::ExitException
ended = Time.now
puts "Processed #{iters} iterations in #{ended - started} seconds"
puts "final size is #{input.length}"
end
# changing from scan/map/join to gsub boosted iters in 1min from 54 to 57