Merge other solutions into one file.
This commit is contained in:
parent
cf06140366
commit
5ac0c699e5
20
day1.cr
Normal file
20
day1.cr
Normal file
|
@ -0,0 +1,20 @@
|
|||
changes = File.read("day1").split("\n")
|
||||
.select { |it| !it.empty? }
|
||||
.map(&.to_i)
|
||||
|
||||
puts "Final frequency: #{changes.sum}"
|
||||
|
||||
count = { 0 => 1 }
|
||||
acc = 0
|
||||
|
||||
changes.cycle 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
|
|
@ -1,4 +0,0 @@
|
|||
puts File.read("day1").split("\n")
|
||||
.select { |it| !it.empty? }
|
||||
.map(&.to_i)
|
||||
.sum
|
19
day1_2.cr
19
day1_2.cr
|
@ -1,19 +0,0 @@
|
|||
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
|
30
day2.cr
Normal file
30
day2.cr
Normal file
|
@ -0,0 +1,30 @@
|
|||
require "./common.cr"
|
||||
|
||||
lines = File.read("day2").split("\n")
|
||||
lines.pop
|
||||
|
||||
CHARS = ('a'..'z').to_a
|
||||
|
||||
def has_n?(string, n)
|
||||
return string.chars.count_each(CHARS).any?(&.[](1).==(n))
|
||||
end
|
||||
|
||||
def count_changes(s1, s2)
|
||||
pairs = s1.chars.zip s2.chars
|
||||
total = 0
|
||||
pairs.each do |pair|
|
||||
total += 1 if pair[0] != pair[1]
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
two = lines.count { |s| has_n?(s, 2) }
|
||||
three = lines.count { |s| has_n?(s, 3) }
|
||||
puts "\"Hash\": #{two * three}"
|
||||
|
||||
lines.product(lines).each do |pair|
|
||||
if count_changes(pair[0], pair[1]) == 1
|
||||
puts "Strings: #{pair[0]}, #{pair[1]}"
|
||||
exit
|
||||
end
|
||||
end
|
15
day2_1.cr
15
day2_1.cr
|
@ -1,15 +0,0 @@
|
|||
lines = File.read("day2").split("\n")
|
||||
lines.pop
|
||||
|
||||
CHARS = ('a'..'z').to_a
|
||||
|
||||
def has_n?(string, n)
|
||||
CHARS.each do |c|
|
||||
return true if string.count(c) == n
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
two = lines.count { |s| has_n?(s, 2) }
|
||||
three = lines.count { |s| has_n?(s, 3) }
|
||||
puts two * three
|
15
day2_2.cr
15
day2_2.cr
|
@ -1,15 +0,0 @@
|
|||
lines = File.read("day2").split("\n")
|
||||
lines.pop
|
||||
|
||||
def count_changes(s1, s2)
|
||||
pairs = s1.chars.zip s2.chars
|
||||
total = 0
|
||||
pairs.each do |pair|
|
||||
total += 1 if pair[0] != pair[1]
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
lines.product(lines).each do |pair|
|
||||
puts "#{pair[0]}, #{pair[1]}" if count_changes(pair[0], pair[1]) == 1
|
||||
end
|
Loading…
Reference in New Issue
Block a user