-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.rb
60 lines (48 loc) · 1.56 KB
/
day23.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
require 'set'
start_time = Time.now
test_run = false
input = File.readlines("input/day23#{test_run ? '_test' : ''}.txt", chomp: true)
elves = Set.new
input.each_with_index do |line, y|
line.chars.each_with_index do |c, x|
elves.add([x, y]) if c == '#'
end
end
NEIGHBOURS = [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]].freeze
DIRECTIONS = [
[[-1, -1], [0, -1], [1, -1]],
[[-1, 1], [0, 1], [1, 1]],
[[-1, -1], [-1, 0], [-1, 1]],
[[1, -1], [1, 0], [1, 1]],
].freeze
start_dir_index = 0
round = 1
loop do
proposed_moves = {}
elves.each do |current_location|
x, y = current_location
next unless NEIGHBOURS.any? { |dx, dy| elves.include?([x + dx, y + dy]) }
4.times do |i|
direction = DIRECTIONS[(start_dir_index + i) % 4]
unless direction.any? { |dx, dy| elves.include?([x + dx, y + dy]) }
new_location = [x + direction[1].first, y + direction[1].last]
proposed_moves.merge!(new_location => current_location) { |_k, _v1, _v2| nil } # clear values for clashing keys
break
end
end
end
break if proposed_moves.empty?
proposed_moves.reject { |_k, v| v.nil? }.each do |new_location, current_location|
elves.delete(current_location)
elves.add(new_location)
end
start_dir_index = (start_dir_index + 1) % 4
if round == 10
xmin, xmax = elves.map(&:first).minmax
ymin, ymax = elves.map(&:last).minmax
puts "Part 1: #{(xmax - xmin + 1) * (ymax - ymin + 1) - elves.length}"
end
round += 1
end
puts "Part 2: #{round}"
puts "Exec time: #{Time.now - start_time}"