From 1254e2946a978ff566fcbd5d3de06baee5e33dce Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 25 Mar 2021 18:35:10 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20premi=C3=A8re=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour14/jour14.m | 45 +++++++++++++++++++++++++++++++++++++++++++++ jour14/raindeer.h | 19 +++++++++++++++++++ jour14/raindeer.m | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 jour14/jour14.m create mode 100644 jour14/raindeer.h create mode 100644 jour14/raindeer.m diff --git a/jour14/jour14.m b/jour14/jour14.m new file mode 100644 index 0000000..a2d52c1 --- /dev/null +++ b/jour14/jour14.m @@ -0,0 +1,45 @@ +#import "raindeer.h" +#import + +int main() +{ + // obligatoire dans un main + NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init]; + NSString *path = @"input"; + NSError *err; + NSString *contentFile = [[NSString alloc] initWithContentsOfFile:path + encoding:NSUTF8StringEncoding + error:&err]; + NSArray *lines = [contentFile componentsSeparatedByString:@"\n"]; + NSMutableArray *listDeer = [[NSMutableArray alloc] init]; + + for (NSString *tmp in lines) + { + NSArray *parsedSpace = [tmp componentsSeparatedByString:@" "]; + if ([parsedSpace count] > 2) + { + Raindeer *rd = [Raindeer initWithName:[parsedSpace objectAtIndex:0] + zatSpeed:[[parsedSpace objectAtIndex:3] integerValue] + boostTime:[[parsedSpace objectAtIndex:6] integerValue] + LunchTime:[[parsedSpace objectAtIndex:13] integerValue]]; + [listDeer addObject: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); + + //finir le main par ça + [contentFile release]; + [listDeer release]; + [myPool drain]; + return 0; +} diff --git a/jour14/raindeer.h b/jour14/raindeer.h new file mode 100644 index 0000000..103b76c --- /dev/null +++ b/jour14/raindeer.h @@ -0,0 +1,19 @@ +#import + +@interface Raindeer : NSObject +{ + NSString *name; + int speed; + int boostTime; + int pause; +} + +// 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; + ++ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)timeSpeed LunchTime:(int)pause; +- (int) calculDistance:(int)time; + +@end diff --git a/jour14/raindeer.m b/jour14/raindeer.m new file mode 100644 index 0000000..177475f --- /dev/null +++ b/jour14/raindeer.m @@ -0,0 +1,40 @@ +#import "raindeer.h" +#import + +@implementation Raindeer + +// permet de créer les getters et setters à partir de property +@synthesize name, speed, boostTime, pause; + ++ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)boostTime LunchTime:(int)pause +{ + Raindeer *new; + if ((new = [[Raindeer alloc] init])) + { + new.name = name; + new.speed = speed; + new.boostTime = boostTime; + new.pause = pause; + } + return new; +} + +- (int) calculDistance:(int) time +{ + int ret = 0; + while (time > 0) + { + int rest = boostTime - time; + ret += (rest > 0) ? speed * rest : speed * boostTime; + time -= boostTime + pause; + } + return ret; +} + +- (void) dealloc +{ + [name release]; + [super dealloc]; +} + +@end