Skip to content

Commit 876cd69

Browse files
committed
Solution day 13
1 parent 950fc27 commit 876cd69

File tree

3 files changed

+470
-1
lines changed

3 files changed

+470
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
|[10](https://adventofcode.com/2022/day/10)|Cathode-Ray Tube|[view](solutions/day10.rb)|⭐ ⭐|
1515
|[11](https://adventofcode.com/2022/day/11)|Monkey in the Middle|[view](solutions/day11.rb)|⭐ ⭐|
1616
|[12](https://adventofcode.com/2022/day/12)|Hill Climbing Algorithm|[view](solutions/day12.rb)|⭐ ⭐|
17-
|[13](https://adventofcode.com/2022/day/13)||[view](solutions/day13.rb)||
17+
|[13](https://adventofcode.com/2022/day/13)|Distress Signal|[view](solutions/day13.rb)|⭐ ⭐|
1818
|[14](https://adventofcode.com/2022/day/14)||[view](solutions/day14.rb)||
1919
|[15](https://adventofcode.com/2022/day/15)||[view](solutions/day15.rb)||
2020
|[16](https://adventofcode.com/2022/day/16)||[view](solutions/day16.rb)||

solutions/day13.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packets = File.read("day13_input.txt").split("\n\n").map {|l| l.split.map {|a| eval a.strip } }
2+
3+
def cmp_arr(a,b)
4+
if !a.kind_of?(Array) && !b.kind_of?(Array)
5+
a <=> b
6+
else
7+
a = [a] unless a.kind_of?(Array)
8+
b = [b] unless b.kind_of?(Array)
9+
a.each_with_index do |ai,i|
10+
break if b[i].nil?
11+
cmp = cmp_arr(ai,b[i])
12+
return cmp unless cmp == 0
13+
end
14+
a.length - b.length
15+
end
16+
end
17+
18+
p packets.map.with_index {|arr,i| arr.sort {|a,b| cmp_arr(a,b) } == arr ? i+1 : 0 }.sum
19+
sorted = (packets + [[[[2]], [[6]]]]).flatten(1).sort {|a,b| cmp_arr(a,b) }
20+
p (sorted.find_index([[2]])+1) * (sorted.find_index([[6]])+1)

0 commit comments

Comments
 (0)