Created
January 9, 2020 15:44
-
-
Save sotolf2/806ac65a37972f209bc20fbf3fdecb4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require threading) | |
(define (get-input) | |
(file->lines "day13.txt")) | |
(struct line (p1 p2 val) #:transparent) | |
(define (parse-line str) | |
(define tokens (string-split (string-replace str "." "") " ")) | |
(define p1 (first tokens)) | |
(define p2 (first (reverse tokens))) | |
(define lose (string-contains? str "lose")) | |
(define num (string->number (first (regexp-match #px"\\d+" str)))) | |
(define val (if lose | |
(* -1 num) | |
num)) | |
(line p1 p2 val)) | |
(define (get-people lns) | |
(define (ppl-rec lns res) | |
(if (empty? lns) | |
(set->list res) | |
(ppl-rec (cdr lns) (~> res | |
(set-add (line-p1 (car lns))) | |
(set-add (line-p2 (car lns))))))) | |
(ppl-rec lns (set))) | |
(define (get-pairs lns) | |
(define (pair-rec lns hsh) | |
(if (empty? lns) | |
hsh | |
(pair-rec (cdr lns) | |
(hash-update hsh | |
(set (line-p1 (car lns)) (line-p2 (car lns))) | |
(lambda (val) (+ val (line-val (car lns)))) | |
0)))) | |
(pair-rec lns #hash())) | |
(define (get-permutations lns) | |
(~> (get-people lns) | |
(in-permutations) | |
(sequence->stream))) | |
(define (seating->table lst) | |
(define (make-pairs lst res) | |
(if (< (length lst) 2) | |
res | |
(make-pairs (cdr lst) (cons (set (first lst) (second lst)) res)))) | |
(make-pairs lst (list (set (car lst) (car (reverse lst)))))) | |
(define (table->value seating hsh) | |
(define (calc-value tbl hsh res) | |
(if (empty? tbl) | |
res | |
(calc-value (cdr tbl) hsh (+ res (hash-ref hsh (car tbl) 0))))) | |
(calc-value (seating->table seating) hsh 0)) | |
(define (find-optimal-value lns) | |
(define (search perms hsh sup) | |
(if (stream-empty? perms) | |
sup | |
(search (stream-rest perms) hsh (max sup (table->value (stream-first perms) hsh))))) | |
(define hsh (get-pairs lns)) | |
(define perms (get-permutations lns)) | |
(search perms hsh 0)) | |
(define (part1) | |
(~>> (get-input) | |
(map parse-line) | |
(find-optimal-value))) | |
(define (add-me lns) | |
(cons (line "me" "me" 0) lns)) | |
(define (part2) | |
(~>> (get-input) | |
(map parse-line) | |
(add-me) | |
(find-optimal-value))) | |
(module+ test | |
(require rackunit) | |
(require rackunit/text-ui) | |
(define part-1 | |
(test-suite | |
"Part 1" | |
(check-equal? 1 1 "basic") | |
(check-equal? (parse-line "Frank would lose 87 happiness units by sitting next to Carol.") | |
(line "Frank" "Carol" -87) "parsing lose line") | |
(check-equal? (parse-line "Frank would gain 26 happiness units by sitting next to Mallory.") | |
(line "Frank" "Mallory" 26) "parsing gain line") | |
)) | |
(define part-2 | |
(test-suite | |
"Part 2" | |
(check-equal? 1 1 "basic") | |
)) | |
(run-tests part-1) | |
(run-tests part-2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment