Skip to content

Instantly share code, notes, and snippets.

@darkash
Last active December 16, 2020 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkash/b6a20260ceac7bf14d4346faa3759dd6 to your computer and use it in GitHub Desktop.
Save darkash/b6a20260ceac7bf14d4346faa3759dd6 to your computer and use it in GitHub Desktop.
AoC 2020 (Day 2)
input = [
"1-3 a: abcde",
"1-3 b: cdefg",
"2-9 c: ccccccccc"
]
def parse(str)
str.match(/(?<min>\d+)-(?<max>\d+) (?<char>\S): (?<testcase>\S+)/)
end
def validate(ipt)
info = parse(ipt)
min, max, char, testcase = info.values_at(:min, :max, :char, :testcase)
count = testcase.count(char)
count <= max.to_i && count >= min.to_i
end
input.select { |ipt| validate(ipt) === true }.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment