diff options
| author | Mistivia <i@mistivia.com> | 2024-02-14 14:55:21 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-02-14 14:55:21 +0800 |
| commit | dc6b5fe160647afcaff88242792113b5802176fe (patch) | |
| tree | fbaf92803820f820716ea584bd951267c9080a2e | |
| parent | 60b0586ddc42cd8060e796e61267fc63a4397712 (diff) | |
refactor advent of code 2023 day 01 from c to racket
| -rw-r--r-- | advent-of-code/2023/01/Makefile | 14 | ||||
| -rw-r--r-- | advent-of-code/2023/01/part1.c | 30 | ||||
| -rw-r--r-- | advent-of-code/2023/01/part1.rkt | 22 | ||||
| -rw-r--r-- | advent-of-code/2023/01/part2.c | 63 | ||||
| -rw-r--r-- | advent-of-code/2023/01/part2.rkt | 36 |
5 files changed, 58 insertions, 107 deletions
diff --git a/advent-of-code/2023/01/Makefile b/advent-of-code/2023/01/Makefile deleted file mode 100644 index 44480b6..0000000 --- a/advent-of-code/2023/01/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -all: run - -run: part1 part2 - ./part1 - ./part2 - -part1: part1.c - gcc -g -I../lib/ ../lib/*.c part1.c -o part1 - -part2: part2.c - gcc -g -I../lib/ ../lib/*.c part2.c -o part2 - -clean: - rm part1 part2 diff --git a/advent-of-code/2023/01/part1.c b/advent-of-code/2023/01/part1.c deleted file mode 100644 index 3aace2a..0000000 --- a/advent-of-code/2023/01/part1.c +++ /dev/null @@ -1,30 +0,0 @@ -#include <stdio.h> -#include <ctype.h> -#include <string.h> - -#include "str.h" - - -int main() { - FILE* fp = fopen("./input", "r"); - int sum = 0; - while (1) { - char *line = str_strip(fgetline(fp)); - if (line == NULL || strlen(line) == 0) { - break; - } - int d1 = -1, d2 = -1; - while (*line != '\0') { - if (isdigit(*line)) { - if (d1 == -1) d1 = *line - '0'; - d2 = *line - '0'; - } - line++; - } - if (d2 == -1) d2 = d1; - sum += d1 * 10 + d2; - } - printf("%d\n", sum); - return 0; -} - diff --git a/advent-of-code/2023/01/part1.rkt b/advent-of-code/2023/01/part1.rkt new file mode 100644 index 0000000..0c69be5 --- /dev/null +++ b/advent-of-code/2023/01/part1.rkt @@ -0,0 +1,22 @@ +#lang racket + +(define fp (open-input-file "input")) +(define sum 0) + +(define (get-lines fp) + (let loop ((lines '())) + (define l (read-line fp)) + (if (eof-object? l) + (reverse lines) + (loop (cons l lines))))) + +(define lines (get-lines fp)) + +(define (extract-number line) + (define number-list (filter char-numeric? (string->list line))) + (set! number-list (map (lambda (c) + (string->number (list->string (list c)))) + number-list)) + (+ (last number-list) (* 10 (car number-list)))) + +(apply + (map extract-number lines)) diff --git a/advent-of-code/2023/01/part2.c b/advent-of-code/2023/01/part2.c deleted file mode 100644 index 13aba12..0000000 --- a/advent-of-code/2023/01/part2.c +++ /dev/null @@ -1,63 +0,0 @@ -#include <stdio.h> -#include <ctype.h> -#include <string.h> -#include <assert.h> - -#include "str.h" - -char *numbers[] = { - "zero", - "one", - "two", - "three", - "four", - "five", - "six", - "seven", - "eight", - "nine" -}; - -int find_number(const char *line) { - int matched = 0; - if (isdigit(*line)) return *line - '0'; - for (int i = 0; i < 10; i++) { - matched = 1; - const char *p1 = numbers[i], *p2 = line; - while (*p1 != '\0' && *p2 != '\0') { - if (*p1 != *p2) { - matched = 0; - break; - } - p1++; - p2++; - } - if (matched && *p1 == '\0') return i; - } - return -1; -} - -int main() { - FILE* fp = fopen("./input", "r"); - int sum = 0; - while (1) { - char *line = str_strip(fgetline(fp)); - if (line == NULL || strlen(line) == 0) { - break; - } - int d1 = -1, d2 = -1; - while (*line != '\0') { - int num = -1; - if ((num = find_number(line)) >= 0) { - if (d1 == -1) d1 = num; - d2 = num; - } - line++; - } - if (d2 == -1) d2 = d1; - sum += d1 * 10 + d2; - } - printf("%d\n", sum); - return 0; -} - diff --git a/advent-of-code/2023/01/part2.rkt b/advent-of-code/2023/01/part2.rkt new file mode 100644 index 0000000..85602da --- /dev/null +++ b/advent-of-code/2023/01/part2.rkt @@ -0,0 +1,36 @@ +#lang racket + +(define fp (open-input-file "input")) +(define sum 0) + +(define (get-lines fp) + (let loop ((lines '())) + (define l (read-line fp)) + (if (eof-object? l) + (reverse lines) + (loop (cons l lines))))) + +(define lines (get-lines fp)) + +(define (replace-digit str) + (set! str (string-replace str "one" "o1ne")) + (set! str (string-replace str "two" "t2wo")) + (set! str (string-replace str "three" "t3hree")) + (set! str (string-replace str "four" "f4our")) + (set! str (string-replace str "five" "f5ive")) + (set! str (string-replace str "six" "s6ix")) + (set! str (string-replace str "seven" "s7even")) + (set! str (string-replace str "eight" "e8ight")) + (set! str (string-replace str "nine" "n9ine")) + str) + +(define (extract-number rawline) + (define line (replace-digit rawline)) + (define number-list (filter char-numeric? (string->list line))) + (set! number-list (map (lambda (c) + (string->number (list->string (list c)))) + number-list)) + (+ (last number-list) (* 10 (car number-list)))) + +(display (apply + (map extract-number lines))) +(newline) |
