Cleaned up my solution more.
This commit is contained in:
		
							parent
							
								
									fc6d6a3e49
								
							
						
					
					
						commit
						6fa33b4e3e
					
				| 
						 | 
				
			
			@ -3,3 +3,5 @@
 | 
			
		|||
This repository has my solutions for this year's Advent of Code challenges. Each day's challenge will be placed into a directory named `day_N`, where `N` is the day of the challenge. Each day will have a solution program named `solution.*` (with the filetype depending on which programming language I used).
 | 
			
		||||
 | 
			
		||||
For D programs, you can simply run them with `rdmd solution.d`.
 | 
			
		||||
 | 
			
		||||
Note that I try and solve the problems as fast as possible, but then I go back and clean up my solution, so what you see isn't really my ugly first attempt.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,21 +5,27 @@ import std.string : splitLines;
 | 
			
		|||
import std.algorithm;
 | 
			
		||||
import std.stdio;
 | 
			
		||||
import std.uni : isNumber;
 | 
			
		||||
import std.functional;
 | 
			
		||||
 | 
			
		||||
/// Parses the first and last digits, using a function F to determine if and
 | 
			
		||||
/// what digit is at a string at a particular index. F should return -1 if no
 | 
			
		||||
/// digit exists.
 | 
			
		||||
int parseDigits(int delegate(string, size_t) F)(string s) {
 | 
			
		||||
    int a = -1, b = -1;
 | 
			
		||||
    for (size_t i = 0; i < s.length; i++) {
 | 
			
		||||
        int an = F(s, i);
 | 
			
		||||
        int bn = F(s, s.length-i-1);
 | 
			
		||||
        if (a == -1 && an != -1) a = an;
 | 
			
		||||
        if (b == -1 && bn != -1) b = bn;
 | 
			
		||||
        if (a != -1 && b != -1) break;
 | 
			
		||||
    }
 | 
			
		||||
    return a * 10 + b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void part1() {
 | 
			
		||||
    readText("part_1_input.txt")
 | 
			
		||||
        .splitLines()
 | 
			
		||||
        .map!((line) {
 | 
			
		||||
            int a = -1, b = -1;
 | 
			
		||||
            for (size_t i = 0; i < line.length; i++) {
 | 
			
		||||
                if (a == -1 && isNumber(line[i])) a = line[i] - '0';
 | 
			
		||||
                if (b == -1 && isNumber(line[$-i-1])) b = line[$-i-1] - '0';
 | 
			
		||||
                if (a != -1 && b != -1) break;
 | 
			
		||||
            }
 | 
			
		||||
            return a * 10 + b;
 | 
			
		||||
        })
 | 
			
		||||
        .sum()
 | 
			
		||||
        .writeln();
 | 
			
		||||
    readText("part_1_input.txt").splitLines()
 | 
			
		||||
        .map!(line => parseDigits!((s, i) => isNumber(s[i]) ? s[i] - '0' : -1)(line))
 | 
			
		||||
        .sum().writeln();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void part2() {
 | 
			
		||||
| 
						 | 
				
			
			@ -27,28 +33,14 @@ void part2() {
 | 
			
		|||
        if (isNumber(s[idx])) return s[idx] - '0';
 | 
			
		||||
        const string[] words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
 | 
			
		||||
        static foreach (size_t i, string word; words) {
 | 
			
		||||
            if (s.length - idx >= word.length && s[idx .. idx + word.length] == word) {
 | 
			
		||||
                return cast(int) i + 1;
 | 
			
		||||
            }
 | 
			
		||||
            if (s.length - idx >= word.length && s[idx .. idx + word.length] == word) return cast(int) i + 1;
 | 
			
		||||
        }
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    readText("part_2_input.txt")
 | 
			
		||||
        .splitLines()
 | 
			
		||||
        .map!((line) {
 | 
			
		||||
            int a = -1, b = -1;
 | 
			
		||||
            for (size_t i = 0; i < line.length; i++) {
 | 
			
		||||
                int an = findDigit(line, i);
 | 
			
		||||
                int bn = findDigit(line, line.length-i-1);
 | 
			
		||||
                if (a == -1 && an != -1) a = an;
 | 
			
		||||
                if (b == -1 && bn != -1) b = bn;
 | 
			
		||||
                if (a != -1 && b != -1) break;
 | 
			
		||||
            }
 | 
			
		||||
            return a * 10 + b;
 | 
			
		||||
        })
 | 
			
		||||
        .sum()
 | 
			
		||||
        .writeln();
 | 
			
		||||
    readText("part_2_input.txt").splitLines()
 | 
			
		||||
        .map!(line => parseDigits!((s, i) => findDigit(s, i))(line))
 | 
			
		||||
        .sum().writeln();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void main() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue