diff --git a/jour14/input b/jour14/input new file mode 100644 index 0000000..6cf5489 --- /dev/null +++ b/jour14/input @@ -0,0 +1,9 @@ +Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds. +Cupid can fly 22 km/s for 2 seconds, but then must rest for 41 seconds. +Rudolph can fly 11 km/s for 5 seconds, but then must rest for 48 seconds. +Donner can fly 28 km/s for 5 seconds, but then must rest for 134 seconds. +Dasher can fly 4 km/s for 16 seconds, but then must rest for 55 seconds. +Blitzen can fly 14 km/s for 3 seconds, but then must rest for 38 seconds. +Prancer can fly 3 km/s for 21 seconds, but then must rest for 40 seconds. +Comet can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. +Vixen can fly 18 km/s for 5 seconds, but then must rest for 84 seconds. diff --git a/jour14/jour14.m b/jour14/jour14.m index a2d52c1..f70231e 100644 --- a/jour14/jour14.m +++ b/jour14/jour14.m @@ -1,4 +1,5 @@ #import "raindeer.h" +#import "troupeau.h" #import <Foundation/Foundation.h> int main() @@ -8,10 +9,12 @@ int main() NSString *path = @"input"; NSError *err; NSString *contentFile = [[NSString alloc] initWithContentsOfFile:path - encoding:NSUTF8StringEncoding - error:&err]; + encoding:NSUTF8StringEncoding + error:&err]; + NSArray *lines = [contentFile componentsSeparatedByString:@"\n"]; NSMutableArray *listDeer = [[NSMutableArray alloc] init]; + Troupeau *troupeau = [[Troupeau alloc] init]; for (NSString *tmp in lines) { @@ -22,24 +25,18 @@ int main() zatSpeed:[[parsedSpace objectAtIndex:3] integerValue] boostTime:[[parsedSpace objectAtIndex:6] integerValue] LunchTime:[[parsedSpace objectAtIndex:13] integerValue]]; - [listDeer addObject:rd]; + [troupeau newDeer:rd]; } } - int ret = 0; - int tmp = 0; - for (Raindeer *rd in listDeer) - { - tmp = [rd calculDistance:2503]; - if (tmp > ret) - ret = tmp; - [rd release]; - } - NSLog(@"le résultat est : %d", ret); + const int time = 2503; + NSLog(@"le résultat de la première sélection est : %d", [troupeau courseUne:time]); + NSLog(@"le résultat de la deuxième sélection est : %d", [troupeau courseDeux:time]); //finir le main par ça [contentFile release]; [listDeer release]; + [troupeau dealloc]; [myPool drain]; return 0; } diff --git a/jour14/raindeer.h b/jour14/raindeer.h index 103b76c..376ee97 100644 --- a/jour14/raindeer.h +++ b/jour14/raindeer.h @@ -6,14 +6,21 @@ int speed; int boostTime; int pause; + int points; + + int cooldown; + int hypervitesse; + int distance; } // permet de générer des setters et getter selon des attributs // ici retain, nonatomic et assign @property (retain, nonatomic) NSString *name; -@property (assign) int speed, boostTime, pause; +@property (assign) int speed, boostTime, pause, points, cooldown, hypervitesse, distance; + (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)timeSpeed LunchTime:(int)pause; - (int) calculDistance:(int)time; +- (void) updateDistance; +- (void) updatePoints; @end diff --git a/jour14/raindeer.m b/jour14/raindeer.m index 177475f..105ae21 100644 --- a/jour14/raindeer.m +++ b/jour14/raindeer.m @@ -4,7 +4,7 @@ @implementation Raindeer // permet de créer les getters et setters à partir de property -@synthesize name, speed, boostTime, pause; +@synthesize name, speed, boostTime, pause, points, cooldown, hypervitesse, distance; + (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)boostTime LunchTime:(int)pause { @@ -15,6 +15,10 @@ new.speed = speed; new.boostTime = boostTime; new.pause = pause; + new.points = 1; //pas sur que ce soit la bonne méthode mais chut + new.cooldown = 0; + new.hypervitesse = boostTime; + new.distance = 0; } return new; } @@ -31,6 +35,28 @@ return ret; } +- (void) updateDistance +{ + if (cooldown > 0) + { + cooldown--; + if (cooldown == 0) + hypervitesse = boostTime; + } + else + { + distance = distance + speed; + hypervitesse--; + if (hypervitesse == 0) + cooldown = pause; + } +} + +- (void) updatePoints +{ + points++; +} + - (void) dealloc { [name release]; diff --git a/jour14/troupeau.h b/jour14/troupeau.h new file mode 100644 index 0000000..f29b54f --- /dev/null +++ b/jour14/troupeau.h @@ -0,0 +1,15 @@ +#import "raindeer.h" +#import <Foundation/Foundation.h> + +@interface Troupeau : NSObject +{ + NSMutableArray *troupeau; + NSArray *recetteTriDistance; +} + +- (id) init; +- (void) newDeer:(Raindeer*)rd; +- (int) courseUne:(int)time; +- (int) courseDeux:(int)time; + +@end diff --git a/jour14/troupeau.m b/jour14/troupeau.m new file mode 100644 index 0000000..fbad31e --- /dev/null +++ b/jour14/troupeau.m @@ -0,0 +1,59 @@ +#import "troupeau.h" +#import "raindeer.h" + +@implementation Troupeau + +- (id) init +{ + self = [super init]; + if (self) + { + NSSortDescriptor *recetteTri = [[NSSortDescriptor alloc] initWithKey:@"distance" + ascending:NO]; + recetteTriDistance = [NSArray arrayWithObject:recetteTri]; + troupeau = [[NSMutableArray alloc] init]; + } + return self; +} + +- (void) newDeer:(Raindeer*)rd +{ + [troupeau addObject:rd]; +} + +- (int) courseUne:(int)time +{ + int ret = 0, tmp = 0; + for (Raindeer *rd in troupeau) + { + tmp = [rd calculDistance:time]; + if (tmp > ret) + ret = tmp; + } + return ret; +} + +- (int) courseDeux:(int)time +{ + while (time >= 0) + { + for (Raindeer *rd in troupeau) + [rd updateDistance]; + [troupeau sortUsingDescriptors:recetteTriDistance]; + [[troupeau firstObject] updatePoints]; + time--; + } + NSSortDescriptor *descPoints = [[NSSortDescriptor alloc] initWithKey:@"points" + ascending:NO]; + NSArray *tmp = [NSArray arrayWithObject:descPoints]; + NSArray *sortedPoints = [troupeau sortedArrayUsingDescriptors:tmp]; + return [[sortedPoints firstObject] points]; +} + +- (void) dealloc +{ + [troupeau release]; + [super dealloc]; +} + +@end