The ---
separator is not squeak syntax, but is presented to distinguish IDE("Browser") code from REPL content ("Workspace").
Last active
December 4, 2020 23:50
-
-
Save fbanados/7e66d74f65730d681ddb0bc959f8e185 to your computer and use it in GitHub Desktop.
Advent Of Code 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| expenses pairs entry | | |
expenses := 'input.txt' asFile contents lines collect:#asNumber. | |
pairs := (expenses collect:[:m | | |
expenses collect:[:n | | |
expenses collect:[:o | {m . n . o}]]]) concatenation concatenation. | |
entry := pairs at: (pairs findFirst:[:p | p first + p second + p third == 2020 ]). | |
entry first * entry second * entry third. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SequenceableCollection>>at: index do: block | |
^self at: index put: (block value:(self at: index)). | |
--- | |
| passwords | | |
passwords := '/Users/fbanados/aoc/input.txt' asFile contents lines collect: | |
[:line | ((line splitBy:'-') collect:[:a | a splitBy:' ']) flattened]. | |
passwords do: | |
[:p | | |
p at:1 do:#asNumber; | |
at:2 do:#asNumber; | |
at:3 do:#allButLast; | |
at:4 do:[:x | (x splitBy:(p at:3)) size - 1 ]]; | |
count: [:p | (p first <= p fourth) and:[p fourth <= p second]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SequenceableCollection>>at: index do: block | |
^self at: index put: (block value:(self at: index)) | |
--- | |
| passwords | | |
passwords := '/Users/fbanados/aoc/input.txt' asFile contents lines collect: | |
[:line | ((line splitBy:'-') collect:[:a | a splitBy:' ']) flattened]. | |
passwords do: | |
[:p | | |
p at:1 do:#asNumber; | |
at:2 do:#asNumber; | |
at:3 do:#allButLast; | |
at:3 do:#asCharacter; | |
at:4 do:[:x | (x at:(p first)) = (p third) xor: (x at:(p second)) = (p third)]]; | |
count: #fourth. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| map next_h total | | |
map := '/Users/fbanados/aoc/input.txt' asFile contents lines allButFirst collect: | |
[:line | line asArray collect:[:a | a = $#]]. | |
next_h := [:h | ((h + 3) \\ (map first size)) + 1]. | |
total := 0 | |
(map inject: 1 into: [:index :row | |k| | |
k :=next_h value: index - 1. | |
(row at: k) ifTrue:[total := total + 1]. | |
k]). | |
total. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object subclass: #Puzzle3 | |
instanceVariableNames: 'map' | |
classVariableNames: '' | |
poolDictionaries: '' | |
category: 'AOC'. | |
Puzzle3>>fromFile: filename | |
map := filename asFile contents lines allButFirst collect: | |
[:line | line asArray collect:[:a | a = $#]]. | |
^ self. | |
Puzzle3>>next_h: h skip: k | |
^ ((h + k - 1) \\ (map first size)) + 1. | |
Puzzle3>>right: right down: down | |
| total src rows k | | |
src := ReadStream on: map. | |
total := 0. | |
k := 1. | |
[src atEnd] whileFalse: [ | |
src skip: down - 1. | |
k := self next_h: k skip: right. | |
(src next at: k) ifTrue:[total := total + 1] | |
]. | |
^ total. | |
Puzzle3>>allSlopes | |
^ ({{1 . 1} . {3 . 1} . {5 . 1} . {7 . 1} . {1 . 2}} | |
collect: [:slope | | |
self right: slope first down: slope second]) | |
inject: 1 into:#*. | |
--- | |
Puzzle3 new fromFile:'/Users/fbanados/aoc/input.txt'; | |
allSlopes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object subclass: #Puzzle4 | |
instanceVariableNames: 'batch allFields requiredFields' | |
classVariableNames: '' | |
poolDictionaries: '' | |
category: 'AOC'. | |
Puzzle4>>fromFile: filename | |
|current str| | |
current := WriteStream on:''. | |
batch := WriteStream on: Array new. | |
str := (ReadStream on: (filename asFile contents lines)). | |
[str atEnd] whileFalse: | |
[|line| | |
line := str next. | |
line isEmpty ifTrue:[batch nextPut: current contents asString. current:= WriteStream on:''] | |
ifFalse:[current << (line,' ')]]. | |
batch nextPut: current contents asString. | |
batch := batch contents collect:[:a | self toDictionary: a]. | |
^ self. | |
Puzzle4>>toDictionary: passport | |
^ Dictionary newFromPairs:(((passport findTokens:' ') collect:[:p | p findTokens:':'])) concatenation. | |
Puzzle4>>requiredFields | |
requiredFields ifNil: | |
[ requiredFields:= Dictionary newFromPairs: | |
{'byr' .'(19[2-9][0-9]|200[0-2])' asRegex . | |
'iyr' . '20(1[0-9]|20)' asRegex . | |
'eyr' . '20(30|(2[0-9]))' asRegex. | |
'hgt' . '(1(([5-8][0-9])|(9[0-3]))cm)|(59|6[0-9]|7[0-6])in' asRegex. | |
'hcl' . '#[0-9a-f]{6}' asRegex. | |
'ecl' . 'amb|blu|brn|gry|grn|hzl|oth' asRegex. | |
'pid' . '[0-9]{9}' asRegex }. "cid can be omitted" ]. | |
^ requiredFields. | |
Puzzle4>>allFields | |
allFields ifNil:[allFields := self requiredFields copy. allFields add:'cid' -> '.*' asRegex]. | |
^ allFields. | |
Puzzle4>>isValid: passport | |
^ ((self requiredFields keys | |
collect:[:key | passport includesKey:key]) | |
inject: true into:#and:) | |
and: [(passport keys collect:[:key| (self allFields at:key) matches: (passport at:key)]) | |
inject: true into:#and:]. | |
Puzzle4>>validPassports | |
^ batch count:[:k | self isValid: k]. | |
--- | |
Puzzle4 new fromFile:'/Users/fbanados/aoc/input.txt'; | |
validPassports |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment