gpt4 book ai didi

ios - Advent Of Code 第 14 天

转载 作者:行者123 更新时间:2023-11-28 21:32:40 25 4
gpt4 key购买 nike

我正在尝试解决 Day14 of Advent of code,我得到的是 2660 而不是 2640。我做错了什么?我查看了其他解决方案,它们似乎都遵循相同的方法。 http://adventofcode.com/day/14

     - (void)day14:(NSArray *)inputs
{
NSMutableDictionary *reindeerDictionary = [[NSMutableDictionary alloc] init];
NSError *error = nil;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\w*) can fly (\\d*) km/s for (\\d*) seconds, but then must rest for (\\d*) seconds." options:0 error:&error];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;

if(error) {
NSLog(@"Error in regex formatting:%@", error);
}

for (NSString *input in inputs)
{
NSArray *matches = [regex matchesInString:input options:0 range:NSMakeRange(0,input.length)];
for (NSTextCheckingResult *result in matches) {
NSMutableDictionary *reindeer = [[NSMutableDictionary alloc] init];
reindeer[@"speed"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:2]]];
reindeer[@"flyTime"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:3]]];
reindeer[@"restTime"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:4]]];
reindeer[@"points"] = @0;
reindeerDictionary[[input substringWithRange:[result rangeAtIndex:1]]] = reindeer;
}
}

int maxSeconds = 1000;
NSNumber *maxDistanceFlown = @0;


for (int i = 0; i <= maxSeconds; i++) {
for (NSString *reindeerName in reindeerDictionary.allKeys) {
NSMutableDictionary *reindeer = reindeerDictionary[reindeerName];

NSNumber *speed = reindeer[@"speed"];
NSNumber *flyingPeriod = reindeer[@"flyTime"];
NSNumber *restPeriod = reindeer[@"restTime"];
int distanceFlown = [reindeer[@"distanceFlown"] intValue];
int relativeSeconds = i % (restPeriod.intValue + flyingPeriod.intValue);

//Check if going at full speed vs rest
if (relativeSeconds < flyingPeriod.intValue) {
distanceFlown += speed.intValue;
reindeer[@"distanceFlown"] = @(distanceFlown);

NSNumber *distanceFlown = reindeer[@"distanceFlown"];
if (distanceFlown > maxDistanceFlown) {
maxDistanceFlown = distanceFlown;
}
}
}
}

NSLog(@"Part 1: Winning Distance: %@\n",maxDistanceFlown);

}

最佳答案

What am I doing wrong?

简短回答:使用迭代(您的 for 循环迭代几秒)

稍微长一点的答案:

问题可以在几行中解决,无需任何迭代。这是一个谜题,所以没有代码,但有一个提示:将一个周期想象成一次飞行 + 休息期。任何行程时间都将由零个或多个完整周期和零个或一个部分周期组成。

HTH

附录

我不相信您的解决方案虽然过于复杂,但会产生错误的答案。是什么让你认为这是错误的?按照问题中的示例运行 1000 秒的行程时间,您的结果匹配。

关于ios - Advent Of Code 第 14 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35282838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com