Advertisement
Guest User

AoC day 2, Smalltalk

a guest
Dec 2nd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. parse_re := '(\d+)-(\d+) (.): (.*)$' asRegex.
  4.  
  5. part1 := 0.
  6. part2 := 0.
  7.  
  8. stdin linesDo:
  9. [
  10.     :line |
  11.  
  12.     (line =~ parse_re) ifMatched: [ :results |
  13.         " Aliases: "
  14.         low  := results at: 1.
  15.         high := results at: 2.
  16.         let  := results at: 3.
  17.         pass := results at: 4.
  18.  
  19.         " Part 1: "
  20.         " Check for low-high occurrences of the letter in the password. "
  21.         " Makes a regex that looks something like: ^([^a]*a[^a]*){1,3}$ "
  22.         check_re := ('^([^', let, ']*', let, '[^', let, ']*){', low, ',', high, '}$') asRegex.
  23.  
  24.         (pass =~ check_re) ifMatched: [ part1 := part1 + 1 ].
  25.  
  26.         " Part 2: "
  27.         " Check if low xor high position equals letter. "
  28.         (((pass at: low asInteger) asString = let)
  29.             xor: ((pass at: high asInteger) asString = let)) ifTrue:
  30.         [
  31.             part2 := part2 + 1.
  32.         ]
  33.     ]
  34. ].
  35.  
  36. stdout nextPutAll: ('Part 1: ', part1 asString); nl.
  37. stdout nextPutAll: ('Part 2: ', part2 asString); nl.
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement