-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rb
More file actions
184 lines (149 loc) · 3.93 KB
/
utils.rb
File metadata and controls
184 lines (149 loc) · 3.93 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true
require "debug"
require "httparty"
require "stringio"
require "dotenv"
require "active_support/core_ext/object/blank"
Dotenv.load(File.join(__dir__, ".env"))
module AOC
module Input
def self.resolve(argv, data_io)
if argv && argv[0] && argv[1]
year = Integer(argv[0])
day = Integer(argv[1])
fetch_aoc(year, day)
else
data_io
end
end
# Fetch from AoC using session cookie (prefer ENV['AOC_SESSION'])
def self.fetch_aoc(year, day, session = ENV['AOC_SESSION'])
raise "Set AOC_SESSION environment variable with your AoC session cookie." if session.nil? || session.empty?
url = "https://adventofcode.com/#{year}/day/#{day}/input"
resp = HTTParty.get(
url,
headers: {
"Cookie" => "session=#{session}",
"User-Agent" => "aoc-util (ruby) - contact: ndalo@hey.com"
}
)
case resp.code
when 200 then resp.body
when 400..499 then raise "AoC fetch failed (#{resp.code}). Check session, year/day, and that the puzzle is unlocked."
else
raise "AoC fetch failed (#{resp.code})."
end
end
def self.io_for(data_str)
StringIO.new(data_str.dup)
end
end
module Parser
def self._io(data)
data.is_a?(String) ? StringIO.new(data) : data
end
def self.raw_data(data)
io = _io(data)
io.read.chomp
end
def self.graph(data)
io = _io(data)
io.readlines(chomp: true).map(&:chars)
end
def self.lines(data)
io = _io(data)
io.readlines(chomp: true)
end
def self.chars(data)
io = _io(data)
io.readlines(chomp: true).join.chars
end
def self.line_chars(data)
io = _io(data)
io.readlines(chomp: true).map(&:chars)
end
def self.numbers(data)
io = _io(data)
io.readlines(chomp: true).map(&:to_i)
end
def self.line_numbers(data)
io = _io(data)
io = io.readlines(chomp: true).map(&:chars)
io.map { _1.map(&:to_i) }
end
end
DIRECTIONS = [
UP = [-1, 0],
DOWN = [1 , 0],
LEFT = [0 ,-1],
RIGHT = [0 , 1],
UP_LEFT = [-1,-1],
UP_RIGHT = [-1, 1],
DOWN_LEFT = [1 ,-1],
DOWN_RIGHT = [1 , 1]
].freeze
FOUR_DIRECTIONS = [UP, DOWN, LEFT, RIGHT]
Coord = Struct.new(:row, :col) do
def +(other)
Coord.new(row + other.row, col + other.col)
end
def -(other)
Coord.new(row - other.row, col - other.col)
end
def *(scalar)
Coord.new(row * scalar, col * scalar)
end
def inspect
to_a
end
def to_s
"(#{row}, #{col})"
end
def to_a
[row, col]
end
end
class GridWithCoords
attr_reader :grid, :coord
def initialize(grid, coord)
@grid = grid
@coord = coord
unless grid[coord.row][coord.col]
raise "Coordinate #{coord} out of bounds for grid of size #{grid.length}x#{grid[0].length}"
end
end
def current
grid[@coord.row][@coord.col]
end
def inspect
grid.each_with_index.map do |row, r|
row.each_with_index.map do |cell, c|
if r == coord.row && c == coord.col
"█"
else
" #{cell} "
end
end.join
end.join("\n")
end
def update(new_value)
grid[coord.row][coord.col] = new_value
end
def up
@coord += Coord.new(-1, 0) if in_bounds?(@coord + Coord.new(-1, 0))
end
def down
@coord += Coord.new(1, 0) if in_bounds?(@coord + Coord.new(1, 0))
end
def left
@coord += Coord.new(0, -1) if in_bounds?(@coord + Coord.new(0, -1))
end
def right
@coord += Coord.new(0, 1) if in_bounds?(@coord + Coord.new(0, 1))
end
def in_bounds?(coord)
return false if grid.dig(coord.row, coord.col).blank?
coord.row.between?(0, grid.length - 1) && coord.col.between?(0, grid[0].length - 1)
end
end
end