Advent of Code Day 1
I’m not really great at doing challenges at the time you’re supposed to. I find the pressure not fun so I wait and use them as prompts for practice later.
Anyways heres my advent of code solution in ruby. I’m not 100% sure its the best but its just how my brain works. When it comes to programming I’m kinda a brute FORce loop kinda guy. First I created the function with a smaller set of sample code provide. It was like 2 list with like 6 entries each.
Since both lists are going to be matching in size I don’t need to worry about too much with double loops. Instead I looped once, grabbed the position of the current item and applied that index to the second list. Compared which is larger and created the arithmetic that way.
There probably is a less complicated solution but I’m not to worried about it because it got me the right answer. It did the job.
data = File.open('counts.txt', 'r')
list1 = []
list2 = []
data.readlines.each do |line|
list1.append(line.split()[0])
list2.append(line.split()[1])
end
list1 = list1.sort()
list2 = list2.sort()
distance = []
i = 0
for li in list1 do
x = li.to_i
y = list2[i].to_i
if x > y
distance.append(x - y)
else
distance.append(y - x)
end
i += 1
end
print(distance.sum)