cem 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/aoc2018/.gitignore +2 -0
- data/examples/aoc2018/day10.rb +61 -0
- data/examples/aoc2018/day11.rb +66 -0
- data/examples/aoc2018/day12.rb +61 -0
- data/examples/aoc2018/day13.rb +167 -0
- data/examples/aoc2018/day14.rb +31 -0
- data/examples/aoc2018/day14_2.rb +71 -0
- data/examples/aoc2018/day15.rb +271 -0
- data/examples/aoc2018/day16.rb +158 -0
- data/examples/aoc2018/day17.rb +203 -0
- data/examples/aoc2018/day18.rb +113 -0
- data/examples/aoc2018/day18_rspec.rb +131 -0
- data/examples/aoc2018/day19.rb +145 -0
- data/examples/aoc2018/day20.rb +103 -0
- data/examples/aoc2018/day21.rb +158 -0
- data/examples/aoc2018/day21_v2.rb +137 -0
- data/examples/aoc2018/day21_v3.rb +157 -0
- data/examples/aoc2018/day21_v4.rb +141 -0
- data/examples/aoc2018/day22.rb +212 -0
- data/examples/aoc2018/day23.rb +148 -0
- data/examples/aoc2018/day24.rb +156 -0
- data/examples/aoc2018/day25.rb +52 -0
- data/examples/aoc2018/day9.rb +51 -0
- data/examples/aoc2018/day9_circular.rb +125 -0
- data/examples/aoc2018/inputs/day10_input.txt +395 -0
- data/examples/aoc2018/inputs/day11_input.txt +1 -0
- data/examples/aoc2018/inputs/day12_input.txt +34 -0
- data/examples/aoc2018/inputs/day12_input2.txt +16 -0
- data/examples/aoc2018/inputs/day13_input.txt +150 -0
- data/examples/aoc2018/inputs/day13_test.txt +6 -0
- data/examples/aoc2018/inputs/day14_input.txt +1 -0
- data/examples/aoc2018/inputs/day15_input.txt +32 -0
- data/examples/aoc2018/inputs/day15_input10.txt +32 -0
- data/examples/aoc2018/inputs/day15_input11.txt +32 -0
- data/examples/aoc2018/inputs/day15_input12.txt +32 -0
- data/examples/aoc2018/inputs/day15_input13.txt +32 -0
- data/examples/aoc2018/inputs/day15_input14.txt +32 -0
- data/examples/aoc2018/inputs/day15_input2.txt +7 -0
- data/examples/aoc2018/inputs/day15_input3.txt +9 -0
- data/examples/aoc2018/inputs/day15_input4.txt +7 -0
- data/examples/aoc2018/inputs/day15_input5.txt +7 -0
- data/examples/aoc2018/inputs/day15_input6.txt +7 -0
- data/examples/aoc2018/inputs/day15_input7.txt +7 -0
- data/examples/aoc2018/inputs/day15_input8.txt +5 -0
- data/examples/aoc2018/inputs/day15_input9.txt +7 -0
- data/examples/aoc2018/inputs/day15_test.txt +9 -0
- data/examples/aoc2018/inputs/day16_input.txt +3865 -0
- data/examples/aoc2018/inputs/day17_input.txt +2229 -0
- data/examples/aoc2018/inputs/day17_input_test.txt +8 -0
- data/examples/aoc2018/inputs/day18_input.txt +50 -0
- data/examples/aoc2018/inputs/day18_test.txt +10 -0
- data/examples/aoc2018/inputs/day19_input.txt +48 -0
- data/examples/aoc2018/inputs/day20_input.txt +1 -0
- data/examples/aoc2018/inputs/day21_input.txt +32 -0
- data/examples/aoc2018/inputs/day22_input.txt +2 -0
- data/examples/aoc2018/inputs/day23_input.txt +1000 -0
- data/examples/aoc2018/inputs/day23_input2.txt +9 -0
- data/examples/aoc2018/inputs/day24_input.txt +24 -0
- data/examples/aoc2018/inputs/day25_input.txt +1483 -0
- data/examples/aoc2018/inputs/day9_input.txt +1 -0
- data/lib/cem.rb +0 -6
- data/lib/cem/ccommon.rb +45 -0
- data/lib/cem/cruzzles.rb +93 -38
- data/lib/cem/version.rb +1 -1
- metadata +62 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5908c34f0fd1893336216236434958f6c14e743b00fad47deccf7f77c68deb7
|
4
|
+
data.tar.gz: 6fb4688ccaacfe92a0ae7c2a32cf875bbefabd6d4a356a2d2d8d0e26bb0ce150
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f11c382b369c6a1e252582c49eaff7dc5492e65be8069b5b6b326595eea6b78f75d19c825fe48067a3bf3fdb1af239dfbe8588f72285625265fd563c99272dab
|
7
|
+
data.tar.gz: f3a999abed05a4a4fa34ff93e95bc37e37821a3d30b90188cd7ff8973e423da69b5049c425c3447ad2748c42898a044daf177c6e4ccbc72cd8666d78bcf45277
|
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/10 part 1 and part 2
|
4
|
+
#
|
5
|
+
# Demonstrates use of
|
6
|
+
# - Point2D#area returns x*y as if the given point was a representing a rectangle starting at 0,0
|
7
|
+
# - Point2D#+ and #- allow to use normal vector math on points
|
8
|
+
# - Grid.from_points allows to build a Grid view of a set of points
|
9
|
+
#
|
10
|
+
# Also did you know that:
|
11
|
+
# - Numeric#step can receive a block directly to implement an infinite for loop?
|
12
|
+
# - Array#minmax => Find min and max from an array
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'cem'
|
16
|
+
|
17
|
+
input = File.readlines("inputs/day10_input.txt", chomp: true)
|
18
|
+
|
19
|
+
data = []
|
20
|
+
|
21
|
+
input.each { |line|
|
22
|
+
|
23
|
+
if line =~ /^position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>$/
|
24
|
+
p1 = $1.to_i
|
25
|
+
p2 = $2.to_i
|
26
|
+
v1 = $3.to_i
|
27
|
+
v2 = $4.to_i
|
28
|
+
|
29
|
+
data << [Point2D.new(p1,p2), Point2D.new(v1,v2)]
|
30
|
+
else
|
31
|
+
raise line
|
32
|
+
end
|
33
|
+
}
|
34
|
+
|
35
|
+
lastSize = nil
|
36
|
+
|
37
|
+
# Count iterations from 0, because we need to return the iteration number preceeding the last.
|
38
|
+
0.step { |iter|
|
39
|
+
|
40
|
+
# Apply velocity to all points
|
41
|
+
data.map! { |p, v| [p + v, v] }
|
42
|
+
|
43
|
+
min, max = data.map { |p, v| p }.minmax
|
44
|
+
|
45
|
+
size = (max - min).area
|
46
|
+
|
47
|
+
# Terminate the search when the area of all points starts growing again.
|
48
|
+
if lastSize && size > lastSize
|
49
|
+
|
50
|
+
# Go back one iteration
|
51
|
+
puts "Word:\n" + Grid.from_points(data.map { |p, v| p - v }).to_s
|
52
|
+
|
53
|
+
puts "Iterations until word: #{iter}"
|
54
|
+
|
55
|
+
return
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
lastSize = size
|
60
|
+
}
|
61
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/11 part 1 and part 2
|
4
|
+
#
|
5
|
+
# Does not use any cem functions \o/
|
6
|
+
#
|
7
|
+
|
8
|
+
input = 5177
|
9
|
+
|
10
|
+
levels = [0] * (300 * 300)
|
11
|
+
|
12
|
+
(1..300).each { |x|
|
13
|
+
(1..300).each { |y|
|
14
|
+
|
15
|
+
rackId = ( x) + 10
|
16
|
+
powerLevel = rackId * ( y)
|
17
|
+
powerLevel += input
|
18
|
+
powerLevel *= rackId
|
19
|
+
powerLevel /= 100
|
20
|
+
powerLevel -= powerLevel / 10 * 10
|
21
|
+
powerLevel -= 5
|
22
|
+
|
23
|
+
levels[y * 300 + x] = powerLevel
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
max = 0
|
28
|
+
(1..300).each { |gridsize|
|
29
|
+
|
30
|
+
local_max = nil
|
31
|
+
local = nil
|
32
|
+
(1..300-gridsize).each { |x|
|
33
|
+
(1..300-gridsize).each { |y|
|
34
|
+
|
35
|
+
sum = 0
|
36
|
+
|
37
|
+
(0..gridsize-1).each { |dx|
|
38
|
+
(0..gridsize-1).each { |dy|
|
39
|
+
|
40
|
+
sum += levels[(dy + y) * 300 + dx + x]
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
if !local_max || sum > local_max
|
45
|
+
local = "#{x}, #{y}, #{gridsize}: #{sum}"
|
46
|
+
local_max = sum
|
47
|
+
end
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
if gridsize == 3
|
52
|
+
puts "Part 1 result:"
|
53
|
+
end
|
54
|
+
|
55
|
+
print local
|
56
|
+
|
57
|
+
if local_max > max
|
58
|
+
puts "***"
|
59
|
+
max = local_max
|
60
|
+
else
|
61
|
+
puts
|
62
|
+
puts "Previous data is max"
|
63
|
+
exit
|
64
|
+
end
|
65
|
+
}
|
66
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/12 part 1 and part 2
|
4
|
+
#
|
5
|
+
# Does not use any cem functions \o/
|
6
|
+
#
|
7
|
+
|
8
|
+
input = {}
|
9
|
+
|
10
|
+
start = ""
|
11
|
+
File.readlines("inputs/day12_input.txt", chomp: true).each { |line|
|
12
|
+
|
13
|
+
if line =~ /^initial state\: (.*)$/
|
14
|
+
start = $1
|
15
|
+
end
|
16
|
+
|
17
|
+
if line =~ /^(.....) => (.)$/
|
18
|
+
input[$1] = $2
|
19
|
+
end
|
20
|
+
|
21
|
+
}
|
22
|
+
|
23
|
+
result = ''
|
24
|
+
previous_score = nil
|
25
|
+
delta = nil
|
26
|
+
offset = 0
|
27
|
+
max_gen = 50000000000
|
28
|
+
|
29
|
+
(1..max_gen).each { |gen|
|
30
|
+
|
31
|
+
start = "...." + start + "...."
|
32
|
+
offset -= 2
|
33
|
+
|
34
|
+
result = ''
|
35
|
+
(0...start.length - 4).each { |i|
|
36
|
+
result += input.has_key?(start[i,5]) ? input[start[i,5]] : '.'
|
37
|
+
}
|
38
|
+
|
39
|
+
score = 0
|
40
|
+
(0...result.length).each { |i|
|
41
|
+
score += i + offset if result[i] == '#'
|
42
|
+
}
|
43
|
+
|
44
|
+
# puts score
|
45
|
+
if gen == 20
|
46
|
+
puts "Part 1: Generation #{gen} has score #{score}"
|
47
|
+
end
|
48
|
+
|
49
|
+
if delta && (score - previous_score == delta)
|
50
|
+
puts
|
51
|
+
puts "Part 2: Generation #{gen} has score #{score} with same delta as before (#{delta})."
|
52
|
+
puts "If continued to gen #{max_gen} the score would be #{(max_gen - gen) * delta + score}"
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
|
56
|
+
delta = score - previous_score if previous_score
|
57
|
+
previous_score = score
|
58
|
+
|
59
|
+
start = result
|
60
|
+
}
|
61
|
+
|
@@ -0,0 +1,167 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/13 part 1 and 2
|
4
|
+
#
|
5
|
+
# Demonstrates Point2D. Certainly could use Grid!
|
6
|
+
|
7
|
+
require 'cem'
|
8
|
+
|
9
|
+
Cart = Struct.new("Cart", :dir, :pos, :history)
|
10
|
+
carts = []
|
11
|
+
lines = []
|
12
|
+
|
13
|
+
# Read board
|
14
|
+
File.readlines("inputs/day13_input.txt", chomp: true).each { |line|
|
15
|
+
lines << line.rstrip
|
16
|
+
}
|
17
|
+
|
18
|
+
# Read carts
|
19
|
+
lines.each_with_index { |l, y|
|
20
|
+
l.chars.each_with_index { |c, x|
|
21
|
+
case c
|
22
|
+
when '^', 'v'
|
23
|
+
lines[y][x] = '|'
|
24
|
+
carts << Cart.new(c, Point2D.new(x,y), 2)
|
25
|
+
when '>', '<'
|
26
|
+
lines[y][x] = '-'
|
27
|
+
carts << Cart.new(c, Point2D.new(x,y), 2)
|
28
|
+
end
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
def printBoard(lines, carts)
|
33
|
+
|
34
|
+
lines.each_with_index { |l, y|
|
35
|
+
l.chars.each_with_index { |c, x|
|
36
|
+
|
37
|
+
a = carts.find { |cart| cart.pos == Point2D.new(x,y) }
|
38
|
+
if a
|
39
|
+
print a.dir
|
40
|
+
else
|
41
|
+
print c
|
42
|
+
end
|
43
|
+
}
|
44
|
+
print "\n"
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
generation = 0
|
49
|
+
first = true
|
50
|
+
|
51
|
+
loop do
|
52
|
+
|
53
|
+
generation += 1
|
54
|
+
|
55
|
+
if carts.size == 1
|
56
|
+
puts "Last crash at: #{carts[0].pos}"
|
57
|
+
exit
|
58
|
+
end
|
59
|
+
|
60
|
+
# system "cls"
|
61
|
+
# puts generation
|
62
|
+
# puts carts.inspect
|
63
|
+
# printBoard(lines, carts)
|
64
|
+
# gets
|
65
|
+
|
66
|
+
carts.sort_by { |cart| cart.pos.y * 10000 + cart.pos.x }.each { |cart|
|
67
|
+
|
68
|
+
cart.pos += Point2D.from_s(cart.dir)
|
69
|
+
|
70
|
+
if crash = carts.find { |c2| cart != c2 && cart.pos == c2.pos }
|
71
|
+
if first
|
72
|
+
puts "First crash at: #{cart.pos}"
|
73
|
+
first = false
|
74
|
+
end
|
75
|
+
|
76
|
+
carts.delete(crash)
|
77
|
+
carts.delete(cart)
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
track = lines[cart.pos.y][cart.pos.x]
|
82
|
+
|
83
|
+
case cart.dir
|
84
|
+
|
85
|
+
when '>'
|
86
|
+
|
87
|
+
case track
|
88
|
+
when '\\'
|
89
|
+
cart.dir = 'v'
|
90
|
+
when '/'
|
91
|
+
cart.dir = '^'
|
92
|
+
when '+'
|
93
|
+
|
94
|
+
cart.history = (cart.history + 1) % 3
|
95
|
+
case cart.history
|
96
|
+
when 1
|
97
|
+
# nothing
|
98
|
+
when 2
|
99
|
+
cart.dir = 'v'
|
100
|
+
when 0
|
101
|
+
cart.dir = '^'
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
when '<'
|
107
|
+
case track
|
108
|
+
when '\\'
|
109
|
+
cart.dir = '^'
|
110
|
+
when '/'
|
111
|
+
cart.dir = 'v'
|
112
|
+
when '+'
|
113
|
+
|
114
|
+
cart.history = (cart.history + 1) % 3
|
115
|
+
case cart.history
|
116
|
+
when 1
|
117
|
+
# nothing
|
118
|
+
when 2
|
119
|
+
cart.dir = '^'
|
120
|
+
when 0
|
121
|
+
cart.dir = 'v'
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
when '^'
|
126
|
+
case track
|
127
|
+
when '\\'
|
128
|
+
cart.dir = '<'
|
129
|
+
when '/'
|
130
|
+
cart.dir = '>'
|
131
|
+
when '+'
|
132
|
+
|
133
|
+
cart.history = (cart.history + 1) % 3
|
134
|
+
case cart.history
|
135
|
+
when 1
|
136
|
+
# nothing
|
137
|
+
when 2
|
138
|
+
cart.dir = '>'
|
139
|
+
when 0
|
140
|
+
cart.dir = '<'
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
when 'v'
|
145
|
+
case track
|
146
|
+
when '\\'
|
147
|
+
cart.dir = '>'
|
148
|
+
when '/'
|
149
|
+
cart.dir = '<'
|
150
|
+
when '+'
|
151
|
+
|
152
|
+
cart.history = (cart.history + 1) % 3
|
153
|
+
case cart.history
|
154
|
+
when 1
|
155
|
+
# nothing
|
156
|
+
when 2
|
157
|
+
cart.dir = '<'
|
158
|
+
when 0
|
159
|
+
cart.dir = '>'
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
164
|
+
}
|
165
|
+
|
166
|
+
|
167
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/14 part 1
|
4
|
+
#
|
5
|
+
# Does not use any cem functions \o/
|
6
|
+
|
7
|
+
recipes = 30121
|
8
|
+
|
9
|
+
nexta = 3
|
10
|
+
storage = [3, 7]
|
11
|
+
elves = [0, 1]
|
12
|
+
|
13
|
+
i = 2
|
14
|
+
while i < recipes + 10
|
15
|
+
|
16
|
+
r = storage[elves[0]] + storage[elves[1]]
|
17
|
+
|
18
|
+
r.to_s.chars.each { |c|
|
19
|
+
storage << c.to_i
|
20
|
+
i += 1
|
21
|
+
|
22
|
+
if i == recipes + 10
|
23
|
+
break;
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
elves[0] = (1 + elves[0] + storage[elves[0]]) % storage.size
|
28
|
+
elves[1] = (1 + elves[1] + storage[elves[1]]) % storage.size
|
29
|
+
end
|
30
|
+
|
31
|
+
puts storage[-10,15].map { |i| i.to_s}.join
|
@@ -0,0 +1,71 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# https://adventofcode.com/2018/day/14 part 2
|
4
|
+
#
|
5
|
+
# Does not use any cem functions \o/
|
6
|
+
|
7
|
+
|
8
|
+
#recipes = "51589"
|
9
|
+
#recipes = "59414"
|
10
|
+
#recipes = "652601"
|
11
|
+
|
12
|
+
recipes = "030121"
|
13
|
+
|
14
|
+
r2 = recipes.chars.map {|c| c.to_i}
|
15
|
+
|
16
|
+
storage = [0] * 100000000
|
17
|
+
storage[0] = 3
|
18
|
+
storage[1] = 7
|
19
|
+
|
20
|
+
e0 = 0
|
21
|
+
e1 = 1
|
22
|
+
|
23
|
+
comp = [0] * r2.length
|
24
|
+
|
25
|
+
i = 2
|
26
|
+
while true
|
27
|
+
|
28
|
+
puts i if i % 100000 == 0
|
29
|
+
|
30
|
+
s0 = storage[e0]
|
31
|
+
s1 = storage[e1]
|
32
|
+
|
33
|
+
r = s0 + s1
|
34
|
+
|
35
|
+
if r >= 10
|
36
|
+
storage[i] = 1
|
37
|
+
i+=1
|
38
|
+
|
39
|
+
comp << 1
|
40
|
+
comp = comp.drop(1)
|
41
|
+
|
42
|
+
if comp == r2
|
43
|
+
puts i - recipes.length
|
44
|
+
exit
|
45
|
+
end
|
46
|
+
storage[i] = r - 10
|
47
|
+
i+=1
|
48
|
+
comp << r - 10
|
49
|
+
comp = comp.drop(1)
|
50
|
+
|
51
|
+
if comp == r2
|
52
|
+
puts i - recipes.length
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
else
|
56
|
+
storage[i] = r
|
57
|
+
i+=1
|
58
|
+
comp << r
|
59
|
+
comp = comp.drop(1)
|
60
|
+
|
61
|
+
if comp == r2
|
62
|
+
puts i - recipes.length
|
63
|
+
exit
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
e0 = (1 + e0 + s0) % i
|
69
|
+
e1 = (1 + e1 + s1) % i
|
70
|
+
end
|
71
|
+
|