Advent of code 2025
Day 1 part 1
p = 50
puts $<.count { |i|
p = (p + (i[0] == ?R ? 1 : -1) * i[1..].to_i) % 100
p.zero?
}
Day 1 part 2
p = 50
puts $<.sum { |i|
r = i[1..].to_i
d = i[0] == ?R ? 1 : -1
c = (r + p * d % 100) / 100
p = (p + d * r) % 100
c
}
Day 2 part 1
puts $<.read.split(',').sum { |r|
a, b = r.split('-').map(&:to_i)
(a..b).sum { |n|
n.to_s =~ /^(.+)\1$/ ? n : 0
}
}
Day 2 part 2
puts $<.read.split(',').sum { |r|
a, b = r.split('-').map(&:to_i)
(a..b).sum { |n|
n.to_s =~ /^(.+)\1+$/ ? n : 0
}
}
Day 3 part 1
puts $<.sum { |l|
l.chars.combination(2).map { |a, b| (a + b).to_i }.max
}
Day 3 part 2
puts $<.read.split.sum { |b|
[].tap { |s|
x = b.size - 12
b.each_char { |c|
s.pop && x -= 1 while x > 0 && s.last&.<(c)
s << c
}
}.take(12).join.to_i
}
Day 4 part 1
R = {}
$<.each_with_index do |l, i|
l.chars.each_with_index do |c, j|
R[i + j*1i] = true if c == ?@
end
end
N = [-1-1i, -1+0i, -1+1i, 0-1i, 0+1i, 1-1i, 1+0i, 1+1i]
p R.keys.count { |z| N.count { |d| R[z + d] } < 4 }
Day 4 part 2
R = {}
$<.each_with_index do |l, i|
l.chars.each_with_index do |c, j|
R[i + j*1i] = true if c == ?@
end
end
N = [-1-1i, -1+0i, -1+1i, 0-1i, 0+1i, 1-1i, 1+0i, 1+1i]
p R.size.tap { nil while R.reject! { |z, _| N.count { |d| R[z + d] } < 4 }} - R.size
Day 5 part 1
r, i = $<.read.split /\n\n/
f = r.scan(/(\d+)-(\d+)/).map { |a, b| a.to_i..b.to_i }
p i.lines.map(&:to_i).count { |y| f.any? { _1 === y } }
Day 5 part 2
p $<.read
.split(/\n\n/)[0]
.scan(/(\d+)-(\d+)/)
.map { _1.map(&:to_i) }
.sort
.reduce([]) { |m, (a, b)|
if !m[0] || m[-1][1] < a - 1
m << [a, b]
else
m[-1][1] = [m[-1][1], b].max
m
end
}
.sum { |a, b| b - a + 1 }