-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rb
103 lines (88 loc) · 3.13 KB
/
solution.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true
module Year2023
class Day21 < Solution
# @input is available if you need the raw data input
# Call `data` to access either an array of the parsed data, or a single record for a 1-line input file
require 'set'
require 'matrix'
# @return [Array<Array<String>>]
def parse
data.map{_1.chars}
end
# u r d l
@@dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]]
def get_neighbors(r, c, max_r, max_c)
@@dirs.map{|dir|
[r,c].zip(dir).map(&:sum)
}.select{|nr, nc| (0...max_r).include?(nr) and (0...max_c).include?(nc)}
end
def part_1
map = parse
start_point = [0, 0]
for row, r in map.each_with_index
for elem, c in row.each_with_index
if elem == 'S'; start_point = [r, c]; break end
end
end
max_r, max_c = map.size, map[0].size
t = ENV['RB_ENV'] == 'test' ? 6 : 64
poss = Set[start_point]
t.times.each {
poss = Set[*poss.flat_map{get_neighbors(*_1, max_r, max_c)}.filter{|r,c| map[r][c] != '#'}]
}
poss.size
end
def get_duplicate(map); Marshal.load(Marshal.dump map) end
def expand_map(map) # expand outward for 2 grids: 1x1 -> 5x5
new_rows = Array.new(5){Matrix[*(get_duplicate map)]}.reduce(&:hstack).to_a
Array.new(5){Matrix[*(get_duplicate new_rows)]}.reduce(&:vstack).to_a
end
def part_2 # not work with example as it doesn't have empty start row and col
map = parse
start_point = [0, 0]
h, w = map.size, map[0].size
for row, r in map.each_with_index
for elem, c in row.each_with_index
if elem == 'S'
start_point = [r, c]
map[r][c] = '.'
break
end
end
end
# h == w
new_start_point = [h, w].map{_1*2}.zip(start_point).map(&:sum)
expanded_map = expand_map map
eh, ew = expanded_map.size, expanded_map[0].size
num, rem = 26501365.divmod(map.size)
poss = Set[new_start_point]
traversed = Set[new_start_point]
odds = Set[]
# steps amount that reach the end of expanded map
(rem + (h*2)).times.each{|t|
poss = Set[
*poss.flat_map{get_neighbors(*_1, eh, ew)}
.filter{|r,c| expanded_map[r][c] != '#' and !traversed.member?([r,c]) }
]
traversed.merge(poss)
if t%2 == 0; odds.merge(poss) end
}
counter = Hash.new 0
odds.each{|r,c| counter[[r/h, c/w]] += 1}
# in input data, steps expand in diamond shape because of empty start row and col
# calculate only tips on each directions,
# edges of diamonds (outer and inner)
# and middle grids (center and non center)
tips = [[0, 2], [2, 0], [2, 4], [4, 2]].map{counter.dig(_1)}.sum
edge_outer = [[0, 1], [1, 4], [3, 0], [4, 3]].map{counter.dig(_1)}.sum
edge_inner = [[1, 1], [1, 3], [3, 1], [3, 3]].map{counter.dig(_1)}.sum
center = counter.dig([2, 2])
notcenter = counter.dig([2, 3])
tips \
+ (edge_outer * num) \
+ (edge_inner * (num-1)) \
+ (notcenter * (num ** 2)) \
+ (center * ((num-1) ** 2))
end
end
end