r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/NotVeryCleverOne Dec 12 '18 edited Dec 12 '18

Part 1 in Objective - C

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // Log text for timing purposes
            NSLog(@"Starting now...");
            
    
            NSString *myPath = @"/Users/home.directory/tmp/advent_day5.txt";
            NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];
    
            NSMutableString *myString = [[NSMutableString alloc]initWithString:myFile];
            NSInteger i = 0;
            
            NSLog(@"Starting string count is %ld",[myString length]);
            
            while (i <[myString length]-1) {
                NSString *currentLetter = [myString substringWithRange:NSMakeRange(i, 1)];
                NSString *nextLetter = [myString substringWithRange:NSMakeRange(i+1, 1 )];
                
                if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[currentLetter characterAtIndex:0]] && [currentLetter uppercaseString] == nextLetter ) {
                        [myString deleteCharactersInRange:NSMakeRange(i, 2)];
                        if (i != 0) {
                            i = i -1;
                        }
                } else if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[currentLetter characterAtIndex:0]] && [currentLetter lowercaseString] == nextLetter ) {
                    [myString deleteCharactersInRange:NSMakeRange(i, 2)];
                    if (i != 0) {
                        i = i -1;
                    }
                } else {
                    i++;
                }
    
            }
                NSLog(@"%@,%ld",myString,[myString length]);
                
                
                
                
                return 0;
                
            }
            
        }

1

u/NotVeryCleverOne Dec 12 '18

Part 2...

#import <Foundation/Foundation.h>




int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Log text for timing purposes
        NSLog(@"Starting now...");
        
        // Read file into an array
        NSString *myPath = @"/Users/home.directory/tmp/advent_day5.txt";
        NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];

        
        
        NSInteger i = 0;
        
        NSString *alphabet = @"abcdefghijklmnopqrstuvwxyz";
        NSMutableDictionary *lengthDict = [[NSMutableDictionary alloc]init];
        
        
        for (NSUInteger idx = 0;idx<[alphabet length];idx++) {
            NSMutableString *myString = [[NSMutableString alloc]initWithString:myFile];
            NSString *currentAlphaLetter = [alphabet substringWithRange:NSMakeRange(idx, 1)];
            myString = [[myString stringByReplacingOccurrencesOfString:currentAlphaLetter withString:@""]mutableCopy];
            myString = [[myString stringByReplacingOccurrencesOfString:[currentAlphaLetter uppercaseString] withString:@""]mutableCopy];
            i = 0;
            
            while (i <[myString length]-1) {
                NSString *currentLetter = [myString substringWithRange:NSMakeRange(i, 1)];
                NSString *nextLetter = [myString substringWithRange:NSMakeRange(i+1, 1 )];
                
                if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[currentLetter characterAtIndex:0]] && [currentLetter uppercaseString] == nextLetter ) {
                    [myString deleteCharactersInRange:NSMakeRange(i, 2)];
                    if (i != 0) {
                        i = i -1;
                    }
                } else if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[currentLetter characterAtIndex:0]] && [currentLetter lowercaseString] == nextLetter ) {
                    [myString deleteCharactersInRange:NSMakeRange(i, 2)];
                    if (i != 0) {
                        i = i -1;
                    }
                } else {
                    i++;
                }
                
            }
            
            [lengthDict setObject:[NSNumber numberWithInteger:[myString length]] forKey:[alphabet substringWithRange:NSMakeRange(idx, 1)]];
            
        }
        
        
        NSLog(@"%@",lengthDict);
        
        
        
        
        return 0;
        
    }
    
}