Skip to content

Commit 684baf5

Browse files
committed
5
1 parent 6a2c313 commit 684baf5

File tree

5 files changed

+648
-1
lines changed

5 files changed

+648
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.vscode
1+
.vscode
2+
.byebug_history

5/51.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
require 'pp'
4+
5+
lines = File.open("#{__dir__}/input.txt").readlines
6+
7+
max_x = 0
8+
max_y = 0
9+
10+
cords = []
11+
12+
lines.each do |line|
13+
start_cord, end_cord = line.split(' -> ')
14+
start_x, start_y = start_cord.split(',').map(&:to_i)
15+
end_x, end_y = end_cord.split(',').map(&:to_i)
16+
17+
cords << [
18+
start_x, start_y,
19+
end_x, end_y
20+
]
21+
22+
max_x = [max_x, start_x, end_x].max
23+
max_y = [max_y, start_y, end_y].max
24+
end
25+
26+
map = Array.new(max_y + 1) { Array.new(max_x + 1, 0) }
27+
28+
cords.each do |cord|
29+
start_x, start_y, end_x, end_y = cord
30+
31+
if start_x == end_x
32+
if start_y < end_y
33+
start_y.upto(end_y) do |y|
34+
map[y][start_x] += 1
35+
end
36+
else
37+
end_y.upto(start_y) do |y|
38+
map[y][start_x] += 1
39+
end
40+
end
41+
elsif start_y == end_y
42+
if start_x < end_x
43+
start_x.upto(end_x) do |x|
44+
map[start_y][x] += 1
45+
end
46+
else
47+
end_x.upto(start_x) do |x|
48+
map[start_y][x] += 1
49+
end
50+
end
51+
end
52+
end
53+
54+
min_2_overlaps = 0
55+
56+
map.each do |row|
57+
row.each do |col|
58+
min_2_overlaps += 1 if col > 1
59+
end
60+
end
61+
62+
pp min_2_overlaps

5/52.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
require 'pp'
4+
5+
lines = File.open("#{__dir__}/input.txt").readlines
6+
7+
max_x = 0
8+
max_y = 0
9+
10+
cords = []
11+
12+
lines.each do |line|
13+
start_cord, end_cord = line.split(' -> ')
14+
start_x, start_y = start_cord.split(',').map(&:to_i)
15+
end_x, end_y = end_cord.split(',').map(&:to_i)
16+
17+
cords << [
18+
start_x, start_y,
19+
end_x, end_y
20+
]
21+
22+
max_x = [max_x, start_x, end_x].max
23+
max_y = [max_y, start_y, end_y].max
24+
end
25+
26+
map = Array.new(max_y + 1) { Array.new(max_x + 1, 0) }
27+
28+
cords.each do |cord|
29+
start_x, start_y, end_x, end_y = cord
30+
31+
if start_x == end_x
32+
if start_y < end_y # up
33+
start_y.upto(end_y) do |y|
34+
map[y][start_x] += 1
35+
end
36+
else # down
37+
end_y.upto(start_y) do |y|
38+
map[y][start_x] += 1
39+
end
40+
end
41+
elsif start_y == end_y
42+
if start_x < end_x
43+
start_x.upto(end_x) do |x|
44+
map[start_y][x] += 1
45+
end
46+
else
47+
end_x.upto(start_x) do |x|
48+
map[start_y][x] += 1
49+
end
50+
end
51+
else
52+
x_dia = 1
53+
y_dia = 1
54+
55+
x_dia = -1 if start_x > end_x
56+
y_dia = -1 if start_y > end_y
57+
58+
([(start_x - end_x).abs, (start_y - end_y).abs].max + 1).times do |index|
59+
map[start_y + index * y_dia][start_x + index * x_dia] += 1
60+
end
61+
end
62+
end
63+
64+
# pp map
65+
66+
min_2_overlaps = 0
67+
68+
map.each do |row|
69+
row.each do |col|
70+
min_2_overlaps += 1 if col > 1
71+
end
72+
end
73+
74+
pp min_2_overlaps

5/input-sample.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
0,9 -> 5,9
2+
8,0 -> 0,8
3+
9,4 -> 3,4
4+
2,2 -> 2,1
5+
7,0 -> 7,4
6+
6,4 -> 2,0
7+
0,9 -> 2,9
8+
3,4 -> 1,4
9+
0,0 -> 8,8
10+
5,5 -> 8,2

0 commit comments

Comments
 (0)