musifter

AoC 2021 day 8 (smalltalk)

Dec 8th, 2021 (edited)
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     apply: method  [ ^self collect: [:x | x perform: method] ]
  5. ]
  6.  
  7. LookupTable extend [
  8.     reverseMap [
  9.         ^self associations inject: LookupTable new into: [ :acc :map |
  10.             (acc at: map value ifAbsentPut: [Set new]) add: map key.
  11.             acc
  12.         ]
  13.     ]
  14. ]
  15.  
  16. "
  17. | Mainline
  18. "
  19.  
  20. " Table mapping the lights to the digit they produce: "
  21. digits := LookupTable from: {
  22.               'abcefg'  -> $0.
  23.               'cf'      -> $1.
  24.               'acdeg'   -> $2.
  25.               'acdfg'   -> $3.
  26.               'bcdf'    -> $4.
  27.               'abdfg'   -> $5.
  28.               'abdefg'  -> $6.
  29.               'acf'     -> $7.
  30.               'abcdefg' -> $8.
  31.               'abcdfg'  -> $9.
  32.           }.
  33.  
  34. part1 := 0.
  35. part2 := 0.
  36.  
  37. stdin linesDo: [ :line |
  38.     sections := (line substrings: $|) collect: [:pat | pat substrings apply: #asSet ].
  39.  
  40.     "
  41.     | Part 1:
  42.     "
  43.     part1 := part1 + (sections second count: [:s | (s size between: 5 and: 6) not]).
  44.  
  45.     "
  46.     | Part 2:
  47.     "
  48.     " Make table of number of occurences in signal -> Set of letters "
  49.     sigCounts := (sections first inject: Bag new into: [ :acc :sig |
  50.                      acc addAll: sig; yourself
  51.                  ]) contents reverseMap.
  52.  
  53.  
  54.     " Make table of size of signals -> Set of those signals (Set of letters)"
  55.     sigLens := (LookupTable from: (sections first collect: [ :sig |
  56.                    sig -> sig size
  57.                ])) reverseMap.
  58.  
  59.     " The unique signal lengths "
  60.     one   := (sigLens at: 2) anyOne.
  61.     seven := (sigLens at: 3) anyOne.
  62.     four  := (sigLens at: 4) anyOne.
  63.     eight := (sigLens at: 7) anyOne.
  64.  
  65.     " Building mapping: "
  66.     mapping := LookupTable new.
  67.  
  68.     " The unique of lights in all ten digits "
  69.     mapping at: $e put: (sigCounts at: 4).
  70.     mapping at: $b put: (sigCounts at: 6).
  71.     mapping at: $f put: (sigCounts at: 9).
  72.  
  73.     " Some set arithmetic to work out the remaining four "
  74.     mapping at: $c put: (one - (mapping at: $f)).
  75.     mapping at: $a put: (seven - one).
  76.     mapping at: $g put: (eight - four - (mapping at: $a) - (mapping at: $e)).
  77.  
  78.     " Final value calculated by exclusion of all others from 8 "
  79.     mapping at: $d put: (mapping values inject: eight into: [:acc :val | acc - val]).
  80.  
  81.     " Flatten and reverse the map "
  82.     mapping := (mapping apply: #anyOne) reverseMap apply: #anyOne.
  83.  
  84.     " Apply mapping, get digits from table, convert to number "
  85.     output := (sections second collect: [ :sig |
  86.                   digits at: (sig collect: [:let | mapping at: let]) asString sorted
  87.               ]) asString asNumber.
  88.  
  89.     part2 := part2 + output.
  90. ].
  91.  
  92. ('Part 1: %1' % {part1}) displayNl.
  93. ('Part 2: %1' % {part2}) displayNl.
Add Comment
Please, Sign In to add comment