Skip to content

Instantly share code, notes, and snippets.

@alvinncx
Created December 8, 2021 15:10
Show Gist options
  • Save alvinncx/a7cb9d65cc0413649743e6258903671b to your computer and use it in GitHub Desktop.
Save alvinncx/a7cb9d65cc0413649743e6258903671b to your computer and use it in GitHub Desktop.
Advent Of Code 2021 - Day 8 Solution (Part 2)
# By far the most tedious puzzle to get the 2nd ⭐️
# Display class -> this is where most of the logic happens. Use different combinations of digit segments to determine
# the correct mapping
class Digit
attr_accessor :segments, :length
def initialize(raw)
@raw = raw
@segments = raw.split('')
@length = @segments.length
end
def is_1
@length == 2
end
def is_7
@length == 3
end
def is_4
@length == 4
end
def is_8
@length == 7
end
def is_2_3_5
@length == 5
end
def is_0_6_9
@length == 6
end
end
class Output
def initialize(raw)
@raw = raw
@segments = raw.split('')
@length = @segments.length
end
def value(mapping)
extracted = extract_segments(mapping).sort
return 0 if is_0(extracted)
return 1 if is_1
return 2 if is_2(extracted)
return 3 if is_3(extracted)
return 4 if is_4
return 5 if is_5(extracted)
return 6 if is_6(extracted)
return 7 if is_7
return 8 if is_8
return 9 if is_9(extracted)
end
def extract_segments(mapping)
mapping.select {|k,v| @segments.include?(k) }.values
end
def is_1
@length == 2
end
def is_2(arr)
arr == [1,3,4,5,7]
end
def is_3(arr)
arr == [1,3,4,6,7]
end
def is_4
@length == 4
end
def is_5(arr)
arr == [1,2,4,6,7]
end
def is_6(arr)
arr == [1,2,4,5,6,7]
end
def is_7
@length == 3
end
def is_8
@length == 7
end
def is_9(arr)
arr == [1,2,3,4,6,7]
end
def is_0(arr)
arr == [1,2,3,5,6,7]
end
end
class Display
def initialize(input, output)
@digits = input.split(' ').map {|o| Digit.new(o) }
@outputs = output.split(' ').map {|o| Output.new(o) }
segment_two, segment_five = segment_two_and_five
@mapping = {
1 => segment_one,
2 => segment_two,
5 => segment_five
}
@mapping[4] = segment_four
@mapping[7] = segment_seven
@mapping[3] = segment_three
@mapping[6] = segment_six
end
def decode
inverted_mapping = @mapping.invert
@outputs.map { |o| o.value(inverted_mapping).to_s }.join('').to_i
end
# Segment mapping
# 111
# 2 3
# 444
# 5 6
# 777
def segment_one
seven = @digits.find(&:is_7)
one = @digits.find(&:is_1)
(seven.segments - one.segments).first
end
def segment_two_and_five
grouped = @digits.find.select(&:is_2_3_5).map(&:segments).flatten.group_by(&:itself)
singles = grouped.select { |k,v| v.length == 1 }.values.flatten
five = singles - @digits.find(&:is_4).segments
two = singles & @digits.find(&:is_4).segments
return [two, five].flatten
end
def segment_four
four = @digits.find(&:is_4)
one = @digits.find(&:is_1)
(four.segments - one.segments - [@mapping[2]]).first
end
def segment_seven
eight = @digits.find(&:is_8)
four = @digits.find(&:is_4)
(eight.segments - [@mapping[1], @mapping[2], @mapping[4], @mapping[5]] - four.segments).first
end
def segment_three
grouped = @digits.find.select(&:is_0_6_9).map(&:segments).flatten.group_by(&:itself)
segment_3_4_5 = grouped.select { |k,v| v.length == 2 }.keys
(segment_3_4_5 - [@mapping[4], @mapping[5]]).first
end
def segment_six
one = @digits.find(&:is_1)
(one.segments - [@mapping[3]]).first
end
end
class Day8
def self.run(inputs)
values = inputs.split("\n").map do |line|
display = Display.new(*line.split(' | '))
display.decode
end
p values.sum
end
end
Day8.run('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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment