Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active December 8, 2021 15:08
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 xpqz/6e91d977b0b8b29d94f1dfb8dc694635 to your computer and use it in GitHub Desktop.
Save xpqz/6e91d977b0b8b29d94f1dfb8dc694635 to your computer and use it in GitHub Desktop.
⍝ https://adventofcode.com/2021/day/8
d←⊃⎕NGET'../d/8'1
data←' '(≠⊆⊢)¨⍤1⊢(⎕CSV⍠'Separator' '|')(d)''4
+/∊2 3 4 7 8∘.=≢¨↑data[;1] ⍝ part 1
seg←{ ⍝ find segment mapping from ⍺ and decode ⍵
frq←{≢⍵}⌸{⍵[⍋⍵]}∊obl←⍺[⍋≢¨⍺]
a←'abcdefg'
s←7⍴''
s[0]←⊃(1⊃obl)~0⊃obl
s[1 4 5]←a[frq⍳6 4 9]
s[2]←a[⍸8=frq]~s[0]
s[3]←⊃(2⊃obl)∩a[⍸7=frq]
s[6]←⊃a~s
10⊥s∘decode¨⍵
}
decode←{
enc←(0 1 2 4 5 6)(2 5)(0 2 3 4 6)(0 2 3 5 6)(1 2 3 5)(0 1 3 5 6)(0 1 3 4 5 6)(0 2 5)(0 1 2 3 4 5 6)(0 1 2 3 5 6)
⊃⍸({⍵[⍋⍵]}⍺⍳⍵)∘≡¨enc
}
+/seg/⍤1⊢data ⍝ part 2
@xpqz
Copy link
Author

xpqz commented Dec 8, 2021

We have 10 encoded digits to the left, and 5 encoded digits to the right on each row.

In the below, we assume that the segment numbering is as follows:

 0000
1    2
1    2
 3333
4    5
4    5
 6666

On the left, we have all 10 digits, without repeats. Over the 10 digits, the segment frequencies are as follows (looking at the diagram given in the question):

segment frequency
0 8
1 6
2 8
3 7
4 4
5 9
6 7

We can uniquely identify segments 1, 4 and 5 from this.

Now we sort the patterns in terms of ascending length.

You can now, somewhat tediously, work out the remaining few mappings, a bit like sudoku.

  • Segment 0: the left-over letter if you remove the pattern[0] from pattern[1]
  • Segment 1: from frequency table
  • Segment 2: letter with frequency 8, excluding the letter we just found for segment 0
  • Segment 3: pattern[2] intersected with the letters with frequency 7
  • Segment 4: from frequency table
  • Segment 5: from frequency table
  • Segment 6: the remaining letter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment