Compare commits

...

3 Commits

8 changed files with 110 additions and 53 deletions

22
common.cr Normal file
View File

@ -0,0 +1,22 @@
module Enumerable(T)
def count_each(others)
count_map = {} of T => Int32
each do |other|
count_map[other] = (count_map[other]? || 0) + 1
end
return count_map
end
end
class Rectangle
def initialize(@x : Int32, @y : Int32, @width : Int32, @height : Int32)
end
def each_coord(&block)
(@x...(@x + @width)).each do |x|
(@y...(@y + @height)).each do |y|
yield x, y
end
end
end
end

20
day1.cr Normal file
View File

@ -0,0 +1,20 @@
changes = File.read("day1_input").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

View File

@ -1,4 +0,0 @@
puts File.read("day1").split("\n")
.select { |it| !it.empty? }
.map(&.to_i)
.sum

View File

@ -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
View File

@ -0,0 +1,30 @@
require "./common.cr"
lines = File.read("day2_input").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

View File

@ -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

View File

@ -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

38
day3.cr Normal file
View File

@ -0,0 +1,38 @@
require "./common.cr"
lines = File.read("day3_input").split("\n")
lines.pop
REGEX = /#([0-9]+) @ ([0-9]+),([0-9]+): ([0-9]+)x([0-9]+)/
claims = {} of Int32 => Rectangle
lines.each do |line|
match = line.match(REGEX).not_nil!
claim = match[1].to_i32
x = match[2].to_i32
y = match[3].to_i32
width = match[4].to_i32
height = match[5].to_i32
claims[claim] = Rectangle.new(x, y, width, height)
end
claimed_coords = {} of Tuple(Int32, Int32) => Int32
claims.values.each do |claim|
claim.each_coord do |x, y|
coord = {x, y}
claimed_coords[coord] = (claimed_coords[coord]? || 0) + 1
end
end
puts "Overlapping: #{claimed_coords.values.count &.>(1)}"
claims.each do |k, v|
count_overlapped = 0
v.each_coord do |x, y|
count_overlapped += 1 if claimed_coords[{x, y}] > 1
end
if count_overlapped == 0
puts "No overlaps: #{k}"
exit
end
end