This commit is contained in:
Beatrice Szilvasy 2024-12-05 12:44:40 +00:00
parent 75482fe16c
commit 71c3cf9b38
4 changed files with 74 additions and 1 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ input
.tup
out
*.im
vis/

58
days/day05.st Normal file
View file

@ -0,0 +1,58 @@
Dictionary extend [
at: key inA: Type [^self at: key ifAbsentPut: [Type new]]
at: key inA: Type add: elt [ (self at: key inA: Type) add: elt ]
]
Object subclass: Ordering [
| beforeMap |
initialize [beforeMap := Dictionary new]
graphviz
[ AOC visualizeWithExt: '.dot' do: [
:stream |
stream << 'digraph {'.
beforeMap keysAndValuesDo: [
:k :vs | vs do: [
:v | stream << k << ' -> ' << v << ';'. ]].
stream << '}'.
]]
order: page1 before: page2
[ beforeMap at: page2 inA: Set add: page1 ]
addRule: ruleStr
[ ruleStr scanf: '%d|%d' with: [ :x :y | self order: x before: y ] ]
isValid: man
[ | mustNotOccur | mustNotOccur := Set new.
man do: [
:elt |
(mustNotOccur includes: elt) ifTrue: [^false].
mustNotOccur addAll: (beforeMap at: elt inA: Set). ].
^true ]
sort: man
[ ^man asSortedCollection: [ :x :y | ((beforeMap at: y) includes: x) ] ]
]
Collection extend [
middleElement [ ^self at: self size // 2 + 1 ]
]
AOC input: [ (stdin contents tokenize: (String with: $<10> with: $<10>))
letArrayInBlock: [
:rules :seqs | | ord mans |
ord := Ordering new.
rules lines do: [ :rule | ord addRule: rule ].
mans := seqs lines collect: [
:seq | seq chain tokenize: ','; collect: [:x | x asNumber] ].
"ord graphviz."
{ord . mans} ] ];
part1: [ :ord :mans | mans select: [ :man | ord isValid: man ] ];
part2: [ :ord :mans |
mans chain reject: [ :man | ord isValid: man ];
collect: [ :man | ord sort: man ] ];
result: [ :ordAndMans :part |
part chain valueWithArguments: ordAndMans;
collect: [:man | man middleElement];
sum ];
finish.

View file

@ -22,10 +22,21 @@ AOC class extend [
"TODO: save as an image?"
finish
[ | startTime stopTime |
(Smalltalk getenv: 'AOC_RUN') ifNil: [
stdout << 'AOC_RUN not set -- not running.'.
^nil ].
startTime := Time millisecondClock.
self run.
stopTime := Time millisecondClock.
stdout << 'Time elapsed: ' << (stopTime - startTime) << 'ms'; nl. ]
visualizeWithExt: ext do: block
[ | file io |
file := (Smalltalk getenv: 'AOC_VIS') ifNil: [^nil].
file := (file,ext) asFile.
file printNl.
file parent createDirectories.
io := file writeStream. [ block value: io ] ensure: [io close] ]
]
"--- Scanf ---"

3
run.sh
View file

@ -22,4 +22,7 @@ else
fi
fi
export AOC_VIS="$DIR/vis/day$DAYP"
export AOC_RUN=1
exec gst -g "days/utils.st" "days/day$DAYP.st" < "$AOC_INPUT"