Skip to content

Commit 611cc31

Browse files
committed
day 15
1 parent 0786a81 commit 611cc31

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

2020/day13/main.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def find_pair_match(result, b, offset)
3333
{ first: time, period: a * b }
3434
end
3535

36-
class Day12Test < Test::Unit::TestCase
36+
class Day13Test < Test::Unit::TestCase
3737
def test_star_1
3838
data = extract_data('data_test.txt')
3939
assert_equal 295, resolve_1(data)

2020/day14/main.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def extract_data(name)
77
File.read(name).split("\n")
88
end
99

10-
class Day12
10+
class Day14
1111
def initialize
1212
@mask = ''
1313
@memory = {}
@@ -43,9 +43,9 @@ def sum
4343
end
4444

4545

46-
class Day12Test < Test::Unit::TestCase
46+
class Day14Test < Test::Unit::TestCase
4747
def test_star_1
48-
d = Day12.new
48+
d = Day14.new
4949
assert_equal(
5050
73,
5151
d.exec_mask(
@@ -57,28 +57,28 @@ def test_star_1
5757

5858
def test_star_1_test
5959
data = extract_data('data_test.txt')
60-
d = Day12.new
60+
d = Day14.new
6161
data.each { |line| d.play(line, "*") }
6262
assert_equal 165, d.sum
6363
end
6464

6565
def test_star_1
6666
data = extract_data('data.txt')
67-
d = Day12.new
67+
d = Day14.new
6868
data.each { |line| d.play(line, "*") }
6969
assert_equal 4297467072083, d.sum
7070
end
7171

7272
def test_star_2
7373
data = extract_data('data_test2.txt')
74-
d = Day12.new
74+
d = Day14.new
7575
data.each { |line| d.play(line, "**") }
7676
assert_equal 208, d.sum
7777
end
7878

7979
def test_star_2_final
8080
data = extract_data('data.txt')
81-
d = Day12.new
81+
d = Day14.new
8282
data.each { |line| d.play(line, "**") }
8383
assert_equal 5030603328768, d.sum
8484
end

2020/day15/main.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/ruby
2+
require 'matrix'
3+
require 'test/unit'
4+
require 'pry'
5+
class Day15
6+
def initialize(list)
7+
@previous = list
8+
@memory = {}
9+
list.each_with_index do |e, index|
10+
@memory[e] ||= []
11+
@memory[e] << index + 1
12+
end
13+
end
14+
15+
def loop(times)
16+
(times - @previous.count).times { |_| self.step }
17+
self
18+
end
19+
20+
def step
21+
previous = @previous[-1]
22+
if @memory[previous]
23+
@memory[previous] << @previous.length
24+
@previous << @memory[previous].last(2).inject(:-).abs
25+
else
26+
@memory[previous] = [@previous.length]
27+
@previous << 0
28+
end
29+
end
30+
31+
def result
32+
@previous[-1]
33+
end
34+
end
35+
36+
class Day12Test < Test::Unit::TestCase
37+
def test_star_1_test_2
38+
assert_equal 436, Day15.new([0,3,6]).loop(2020).result
39+
assert_equal 1, Day15.new([1,3,2]).loop(2020).result
40+
assert_equal 10, Day15.new([2,1,3]).loop(2020).result
41+
assert_equal 27, Day15.new([1,2,3]).loop(2020).result
42+
assert_equal 78, Day15.new([2,3,1]).loop(2020).result
43+
assert_equal 438, Day15.new([3,2,1]).loop(2020).result
44+
assert_equal 1836, Day15.new([3,1,2]).loop(2020).result
45+
end
46+
47+
def test_star_1_final
48+
assert_equal 289, Day15.new([0,8,15,2,12,1,4]).loop(2020).result
49+
end
50+
51+
def test_star_2_final
52+
assert_equal 1505722, Day15.new([0,8,15,2,12,1,4]).loop(30000000).result
53+
end
54+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy