This commit is contained in:
Beatrice Szilvasy 2024-12-01 09:08:32 +00:00
parent e35c567186
commit f079111181
3 changed files with 81 additions and 66 deletions

View file

@ -1,66 +1,15 @@
Object subclass: CharPattern [ AOC part1: [ :lines | | sorted |
| pattern assoc next | sorted := (lines collect: [ :it | it asSortedCollection readStream ]).
pattern [ ^pattern ] ((sorted at: 1) with: (sorted at: 2)) collect:
assoc [ ^assoc ] [ :arr | ((arr at: 1) - (arr at: 2)) abs ] ];
init: patIn part2: [ :lines | | counts | counts := Dictionary new.
[ pattern := patIn. (lines at: 2) do: [
assoc := patIn tokenize: '\|'. :elt | counts at: elt
^self ] put: 1 + (counts at: elt ifAbsent: [0]) ].
(lines at: 1) collect: [ :elt | elt * (counts at: elt ifAbsent: [0]) ] ];
next: nextPat arg: [ | inp l r | inp := FileStream stdin toLines collect: [ :ln | ln scanf: '%d %d' ].
[ nextPat ifNotNil: [ l := inp collect: [ :e | e at: 1 ].
next := nextPat. r := inp collect: [ :e | e at: 2 ].
pattern := pattern,'|',nextPat pattern ] ] {l . r} ];
map: [ :lines :part | (part value: lines) sum ];
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 ];
finish. finish.

66
days/interaction.st Normal file
View 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
View file

@ -5,7 +5,7 @@ cd "$DIR" || exit 1
if [ -z "$1" ] if [ -z "$1" ]
then DAYN="$(date +%d)" then DAYN="$(date +%d)"
else DAYN="$1" else DAYN="$1"; shift
fi fi
DAY="$(echo "$DAYN" | sed 's/^0*//')" DAY="$(echo "$DAYN" | sed 's/^0*//')"
DAYP="$(printf "%02d" "$DAY")" DAYP="$(printf "%02d" "$DAY")"