r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

49 Upvotes

407 comments sorted by

View all comments

Show parent comment

1

u/FrankRuben27 Dec 02 '18

another (recent) fellow-Racketer:

#lang racket

(module+ test
  (require typed/rackunit))

(define (command-line->strings offset)
  (vector->list (vector-drop (current-command-line-arguments) offset)))

(define (count-same str)
  (let* ((by-chars (group-by length (group-by identity (sort (string->list str) char-ci<?) char=?) =))
         (by-count (map (lambda (item) (cons (length (car item)) (length item))) by-chars)))
    (values (dict-ref by-count 2 0) (dict-ref by-count 3 0))))
(module+ test
  (let-values ([(count-2 count-3) (count-same "bababc")])
    (check-equal? count-2 1)
    (check-equal? count-3 1)))

(define (twice-thrice? str)
  (let-values ([(count-2 count-3) (count-same str)])
    (values (positive? count-2) (positive? count-3))))
(module+ test
  (let-values ([(twice? thrice?) (twice-thrice? "bababc")])
    (check-true twice?)
    (check-true thrice?)))

(define (count-twice-thrice strs)
  (for/fold ([twice 0] [thrice 0])
            ([str strs])
            (let-values ([(twice? thrice?) (twice-thrice? str)])
              (values (if twice?  (add1 twice)  twice)
                      (if thrice? (add1 thrice) thrice)))))

(define (part-1 ids)
  (let-values ([(twice thrice) (count-twice-thrice ids)])
    (* twice thrice)))

(define (same-string str1 str2)
  (let ((same-chars (for/fold ([same-chars '()])
                              ([c1 (in-string str1)]
                               [c2 (in-string str2)])
                              (if (char=? c1 c2)
                                  (cons c1 same-chars)
                                  same-chars))))
    (if (= (- (string-length str1) (length same-chars)) 1)
        (list->string (reverse same-chars))
        #f)))
(module+ test
  (check-false (same-string "abcde" "axcye"))
  (check-equal? (same-string "fghij" "fguij") "fgij"))

(define (part-2 ids)
  (for*/first ([outer-id ids]
               [inner-id (cdr ids)]
               #:when (same-string outer-id inner-id))
              (same-string outer-id inner-id)))

(module+ main
  (define puzzle? (eq? (string->symbol (vector-ref (current-command-line-arguments) 0)) 'pz))
  (define part-1? (= (string->number (vector-ref (current-command-line-arguments) 1)) 1))
  (define from-file? (eq? (string->symbol (vector-ref (current-command-line-arguments) 2)) 'f))
  (let ((ids (if from-file?
                 (file->lines (vector-ref (current-command-line-arguments) 3))
                 (command-line->strings 3)))
        (proc (if part-1? part-1 part-2)))
    (displayln (list (take ids 3) '==> (proc ids)))))

1

u/FrankRuben27 Dec 02 '18

Invocation from make, like this:

.PHONY : -day2-test day2-part1 day2-part2

day2 : -day2-test day2-part1 day2-part2

-day2-test :
	raco test day2.rkt

day2-part1 :
	racket day2.rkt ex 1 s abcdef bababc abbcde abcccd aabcdd abcdee ababab
	@echo expect 5434
	racket day2.rkt pz 1 f day2-pz.txt

day2-part2 :
	@echo expect agimdjvlhedpsyoqfzuknpjwt
	racket day2.rkt pz 2 f day2-pz.txt