Skip to content

Instantly share code, notes, and snippets.

@denpatin
Created December 9, 2021 12:59
Show Gist options
  • Save denpatin/f09e293ed0b73cd06cfae8a2ea630187 to your computer and use it in GitHub Desktop.
Save denpatin/f09e293ed0b73cd06cfae8a2ea630187 to your computer and use it in GitHub Desktop.
Day 08 (AoC 2021)
class Day08
getter displays : Array(Array(Array(String)))
def initialize(input)
@displays = input.map &.split("|").map(&.split)
end
def part1
result = 0
displays.each do |display|
_, output = display
result += output.count &.size.in?(2, 4, 3, 7)
end
result
end
def part2
result = 0
displays.each do |display|
input, output = display
pattern = dictionary.chars.each_permutation.map(&.join).find do |p|
input.all? { |i| reference.keys.includes? i.tr(dictionary, p).chars.sort!.join }
end
result += output.map { |o| reference[o.tr(dictionary, pattern.not_nil!).chars.sort!.join] }.join.to_i
end
result
end
private def dictionary
"abcdefg"
end
private def reference
{
"abcefg" => 0,
"cf" => 1,
"acdeg" => 2,
"acdfg" => 3,
"bcdf" => 4,
"abdfg" => 5,
"abdefg" => 6,
"acf" => 7,
"abcdefg" => 8,
"abcdfg" => 9
}
end
end
require "spec"
require "../src/day08"
describe Day08 do
input = <<-INPUT
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce
INPUT
displays = input.split "\n"
describe "#part1" do
it "returns many times digits 1, 4, 7, or 8 appear" do
Day08.new(displays).part1.should eq 26
end
end
describe "#part2" do
it "returns the sum of all the decoded output values" do
Day08.new(displays).part2.should eq 61229
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment