include("utils.jl") function parse_input(filename) map(non_empty_lines(filename)) do line notes, output = map(split, split(line, "|")) end end function count_unique(strings) length(filter(x -> x in (2,3,4,7), length.(strings))) end function solution8_1() sum(map(count_unique, getindex.(parse_input("8.data"), 2))) end function solution8_2() summe = 0 for (notes, output) in parse_input("8.data") recordings = [notes;output] # find length 2 => c,f cf = first(filter(x->length(x)==2, recordings)) # find length 3 => c,f,a => a cfa = first(filter(x->length(x)==3, recordings)) a = setdiff(cfa, cf)[1] # find length 4 => c,f,d,b => d,b cfdb = first(filter(x->length(x)==4, recordings)) db = setdiff(cfdb, cf) d = "X" b = "X" f = "X" c = "X" # find length 5 => for fives in filter(x->length(x)==5, recordings) common = intersect(fives, db) if length(common) == 1 # is there one missing d,b? => present is d missing is b d = common[1] b = setdiff(db, d)[1] else # is there one with d AND b => has one of c,f => f (and thus c) f = intersect(fives, cf)[1] c = setdiff(cf, f)[1] end end abcdf = "$a$b$c$d$f" g = "X" for sixes in filter(x->length(x)==6, recordings) new = setdiff(sixes, "$a$b$c$d$f") if length(new) == 1 g = new[1] end end abcdfg = "$a$b$c$d$f$g" e = "x" for anything in recordings new = setdiff(anything, abcdfg) if length(new) == 1 e = new[1] break end end digits = Dict(sort(collect("$a$b$c$e$f$g")) => 0, sort(collect("$c$f")) => 1, sort(collect("$a$c$d$e$g")) => 2, sort(collect("$a$c$d$f$g")) => 3, sort(collect("$b$c$d$f")) => 4, sort(collect("$a$b$d$f$g")) => 5, sort(collect("$a$b$d$e$f$g")) => 6, sort(collect("$a$c$f")) => 7, sort(collect("$a$b$c$d$e$f$g")) => 8, sort(collect("$a$b$c$d$f$g")) => 9) result = 0 power = 0 for digit in reverse(output) value = get(digits, sort(collect(digit)), -1) result += value * 10^power power += 1 end # println("$(join(notes, ' ')) | $(join(output, ' ')): $result") summe += result end summe end solution8_2()