Skip to content

Instantly share code, notes, and snippets.

@ndnenkov
ndnenkov / aoc_2022_06_regex.rb
Created December 6, 2022 15:02
Regex only solution for Advent of Code 2022, Day 6
View aoc_2022_06_regex.rb
input = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb'
# Part 1
input.match(/(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3)./).end 0 # => 7
# Part 2
input.match(
%r{
(.)(?!.{,12}\1)
(.)(?!.{,11}\2)
@ndnenkov
ndnenkov / aoc_2021_10.rb
Created December 10, 2021 15:24
Regex solution of Advent Of Code 2021 task 10
View aoc_2021_10.rb
INPUT = <<~CODE
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
@ndnenkov
ndnenkov / aoc_2020_4b.rb
Last active December 4, 2020 10:15
Single regex solution for Advent of Code 2020, day 4, part 2
View aoc_2020_4b.rb
# Single regex solution for Advent of Code 2020, day 4, part 2
# https://adventofcode.com/2020/day/4
PASSPORTS = <<~TEXT
eyr:1972 cid:100
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946
@ndnenkov
ndnenkov / merge_overlapping_srt_captions.rb
Last active December 20, 2020 09:36
Merge overlapping srt captions
View merge_overlapping_srt_captions.rb
# https://video.stackexchange.com/q/30220/29745
#
# Usage:
# ruby ./merge_overlapping_srt_captions.rb _input_file_ _output_file_
class Timestamp
include Comparable
attr_reader :hours, :minutes, :seconds, :milliseconds
@ndnenkov
ndnenkov / wtf.rb
Created March 10, 2017 07:47
`foo = foo # => nil` is worse than you thought
View wtf.rb
p binding.local_variables # => [:foo]
p binding.local_variable_get(:foo) # => nil
puts "I'm just a buffer"
foo = foo
View connection_adapter_enum_fix.rb
module ActiveRecord
module ConnectionAdapters
class Column
private
def simplified_type(field_type)
case field_type
when /\bint(?:eger)?\b/i
:integer
when /\b(?:float|double)\b/i
View spec.rb
describe "Asm" do
it "runs an empty program" do
Asm.asm do
end.should eq [0, 0, 0, 0]
end
it "divides positive numbers" do
Asm.asm do
mov ax, 37
mov bx, 6