Day 1
This commit is contained in:
parent
e35c567186
commit
f079111181
3 changed files with 81 additions and 66 deletions
|
|
@ -1,66 +1,15 @@
|
|||
Object subclass: CharPattern [
|
||||
| pattern assoc next |
|
||||
pattern [ ^pattern ]
|
||||
assoc [ ^assoc ]
|
||||
init: patIn
|
||||
[ pattern := patIn.
|
||||
assoc := patIn tokenize: '\|'.
|
||||
^self ]
|
||||
|
||||
next: nextPat
|
||||
[ nextPat ifNotNil: [
|
||||
next := nextPat.
|
||||
pattern := pattern,'|',nextPat pattern ] ]
|
||||
|
||||
findIn: line
|
||||
[ ^(line searchRegex: pattern) match ]
|
||||
|
||||
interpWord: word
|
||||
[ ^(assoc indexOf: word ifAbsent: [nil])
|
||||
ifNil: [ next interpWord: word ]
|
||||
ifNotNil: [:it | it - 1] ]
|
||||
|
||||
findAndInterp: line
|
||||
[ ^self interpWord: (self findIn: line) ]
|
||||
|
||||
reversed
|
||||
[ ^(CharPattern
|
||||
new: ((assoc collect: [:it | it reverse])
|
||||
join: '|'))
|
||||
next: next ]
|
||||
]
|
||||
|
||||
CharPattern class extend [
|
||||
new: pattern
|
||||
[ | r | r := super new. r init: pattern. ^r ]
|
||||
]
|
||||
|
||||
String extend [
|
||||
calibration: pat rpat: rpat
|
||||
[ | d1 d2 |
|
||||
d1 := pat findAndInterp: self.
|
||||
d2 := rpat findAndInterp: self reverse.
|
||||
^(d1 * 10) + d2 ]
|
||||
]
|
||||
|
||||
OrderedCollection extend [
|
||||
collectPart: pat
|
||||
[ | rpat |
|
||||
rpat := pat reversed.
|
||||
^self collect: [
|
||||
:line |
|
||||
[ line calibration: pat rpat: rpat ]
|
||||
ifError: [ :exn | exn ]
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
pat1 := CharPattern new: '0|1|2|3|4|5|6|7|8|9'.
|
||||
pat2 := CharPattern new: 'zero|one|two|three|four|five|six|seven|eight|nine'.
|
||||
pat2 next: pat1.
|
||||
|
||||
AOC part1: pat1;
|
||||
part2: pat2;
|
||||
arg: [ FileStream stdin toLines ];
|
||||
map: [ :lines :pat | (lines collectPart: pat) sum ];
|
||||
AOC part1: [ :lines | | sorted |
|
||||
sorted := (lines collect: [ :it | it asSortedCollection readStream ]).
|
||||
((sorted at: 1) with: (sorted at: 2)) collect:
|
||||
[ :arr | ((arr at: 1) - (arr at: 2)) abs ] ];
|
||||
part2: [ :lines | | counts | counts := Dictionary new.
|
||||
(lines at: 2) do: [
|
||||
:elt | counts at: elt
|
||||
put: 1 + (counts at: elt ifAbsent: [0]) ].
|
||||
(lines at: 1) collect: [ :elt | elt * (counts at: elt ifAbsent: [0]) ] ];
|
||||
arg: [ | inp l r | inp := FileStream stdin toLines collect: [ :ln | ln scanf: '%d %d' ].
|
||||
l := inp collect: [ :e | e at: 1 ].
|
||||
r := inp collect: [ :e | e at: 2 ].
|
||||
{l . r} ];
|
||||
map: [ :lines :part | (part value: lines) sum ];
|
||||
finish.
|
||||
|
|
|
|||
66
days/interaction.st
Normal file
66
days/interaction.st
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
Object subclass: ExplorerStream [
|
||||
| stream objects | on: aStream
|
||||
[ stream := aStream.
|
||||
objects := OrderedCollection new. ]
|
||||
|
||||
title: title [ stream << '# ' << title ; nl. ]
|
||||
subtitle: block [ stream << '**'. block value. stream << '**'; nl; nl. ]
|
||||
text: text [ text displayOn: stream ]
|
||||
nl [ stream nl ]
|
||||
|
||||
linkTextTo: obj name: text
|
||||
[ |r| r := '[%1][%2]' % {text . objects size}.
|
||||
objects add: obj. ^r ]
|
||||
|
||||
linkTo: obj name: text [ self text: (self linkTextTo: obj name: text). ]
|
||||
|
||||
section: title do: block [ self text: '## '; text: title; nl. block value ]
|
||||
ssection: title do: block [ self text: '### '; text: title; nl. block value ]
|
||||
]
|
||||
ExplorerStream class extend [ on: stream [ ^super new on: stream ] ]
|
||||
|
||||
Object extend [
|
||||
emacsExploreOn: exs
|
||||
[ self basicEmacsExploreOn: exs;
|
||||
emacsExploreMembersOn: exs]
|
||||
|
||||
basicEmacsExploreOn: exs
|
||||
[ exs title: self;
|
||||
subtitle: [
|
||||
exs text: 'of class: ';
|
||||
linkTo: self class name: self class ] ]
|
||||
|
||||
emacsExploreMembersOn: exs []
|
||||
]
|
||||
|
||||
Class extend [
|
||||
emacsExploreOn: exs
|
||||
[ self basicEmacsExploreOn: exs.
|
||||
self superclass ifNotNil: [
|
||||
:sc | exs subtitle: [
|
||||
exs text: 'Superclass: ';
|
||||
linkTo: sc name: sc.
|
||||
] ].
|
||||
self category ifNotNil: [
|
||||
:cat | exs subtitle: [ exs text: 'Category: '; text: cat ] ].
|
||||
self comment
|
||||
ifNotNil: [ :cmnt | exs text: cmnt; nl; nl ]
|
||||
ifNil: [ exs text: 'No comment.'; nl; nl ].
|
||||
|
||||
self subclasses size > 0 ifTrue: [
|
||||
exs subtitle: [
|
||||
exs text: 'Subclasses: '.
|
||||
self subclasses
|
||||
do: [ :sc | exs linkTo: sc name: sc ]
|
||||
separatedBy: [ exs text: ', ' ]. ] ].
|
||||
|
||||
exs section: 'Own members' do:
|
||||
[ self selectors do: [
|
||||
:selector | | method |
|
||||
exs ssection: selector asString do:
|
||||
[ (self sourceCodeAt: selector ifAbsent: [nil])
|
||||
ifNotNil: [ :source | exs text: source ]
|
||||
ifNil: [ exs text: 'No source available.' ] ]; nl; nl.
|
||||
] ]
|
||||
]
|
||||
]
|
||||
2
run.sh
2
run.sh
|
|
@ -5,7 +5,7 @@ cd "$DIR" || exit 1
|
|||
|
||||
if [ -z "$1" ]
|
||||
then DAYN="$(date +%d)"
|
||||
else DAYN="$1"
|
||||
else DAYN="$1"; shift
|
||||
fi
|
||||
DAY="$(echo "$DAYN" | sed 's/^0*//')"
|
||||
DAYP="$(printf "%02d" "$DAY")"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue