Day 2
This commit is contained in:
parent
c5e28c3609
commit
2ff3fbd672
1 changed files with 26 additions and 0 deletions
26
days/day02.st
Normal file
26
days/day02.st
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Collection extend [
|
||||
checkSafeStep: a to: b
|
||||
[ |delta| (a < 1) | (b > self size) ifTrue: [^true].
|
||||
delta := (self at: b) - (self at: a).
|
||||
^(1 <= delta) & (delta <= 3) ]
|
||||
|
||||
safetyFwd
|
||||
[ | dp | dp := Array new: self size + 1.
|
||||
(self size to: 0 by: -1) do: [
|
||||
:i | | v |
|
||||
v := i + 1 to: self size + 1.
|
||||
v := v select: [ :j | self checkSafeStep: i to: j ].
|
||||
v := v collect: [ :j | (dp at: (j + 1) ifAbsent: [0]) + (j - i - 1) ].
|
||||
v := v fold: [ :l :r | l min: r ].
|
||||
dp at: (i + 1) put: v ].
|
||||
^dp at: 1 ]
|
||||
safety [ ^self safetyFwd min: self reverse safetyFwd ]
|
||||
]
|
||||
|
||||
AOC input: [ stdin toLines collect:
|
||||
[ :ln | ((ln tokenize: ' ')
|
||||
collect: [ :lvl | lvl scanf: '%d' with: [:e|e] ])
|
||||
safety ] ];
|
||||
part1: 0; part2: 1;
|
||||
result: [ :reports :damp | reports count: [ :it | it <= damp ] ];
|
||||
finish
|
||||
Loading…
Add table
Reference in a new issue