Skip to content

Commit ba776fe

Browse files
committed
Day 14 - Puzzel 2 - Attempt 1
1 parent 10840c1 commit ba776fe

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lib/solutions/day_14.rb

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,46 @@ def part_one(input, w = 101, h = 103)
2323
quadrants.inject(:*)
2424
end
2525

26-
def part_two(input)
27-
0
26+
def part_two(input, w = 101, h = 103)
27+
bots = input.split("\n")
28+
.map { |line| line.scan(/(-?\d+)/).flatten }
29+
.map { |values| Robot.new(values[0].to_i, values[1].to_i, values[2].to_i, values[3].to_i, w, h) }
30+
31+
moves = 1
32+
loop do
33+
bots.map(&:move!)
34+
coords = bots.map(&:p)
35+
36+
if all_semetrical?(w, h, coords)
37+
puts "--- Move #{moves} ---"
38+
display_bots(w, h, bots.map(&:p))
39+
puts ''
40+
break
41+
end
42+
43+
moves += 1
44+
end
45+
end
46+
47+
def all_semetrical?(w, h, coords)
48+
grid = Array.new(h) { Array.new(w, '.') }
49+
coords.each do |x, y|
50+
grid[y][x] = '#'
51+
end
52+
grid.all? { |row| row == row.reverse }
53+
end
54+
55+
def display_bots(w, h, coords)
56+
grid = Array.new(h) { Array.new(w, '.') }
57+
coords.each do |x, y|
58+
grid[y][x] = '#'
59+
end
60+
grid.each do |row|
61+
puts row.join
62+
end
63+
end
64+
65+
def all_semetrical(coords)
2866
end
2967
end
3068

0 commit comments

Comments
 (0)