Add day one solution.

This commit is contained in:
Danila Fedorin 2018-11-30 22:42:25 -08:00
commit c8f1a23c0b
2 changed files with 22 additions and 0 deletions

3
day1_1.cr Normal file
View File

@ -0,0 +1,3 @@
puts File.read("day1").split("\n")
.map(&.to_i)
.sum

19
day1_2.cr Normal file
View File

@ -0,0 +1,19 @@
count = { 0 => 1 }
acc = 0
changes = File.read("day1").split("\n")
.select { |it| !it.empty? }
.map(&.to_i)
while true
changes.each do |i|
acc += i
old_count = count[acc]? || 0
new_count = old_count + 1
count[acc] = new_count
if new_count == 2
puts acc
exit
end
end
end