Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created December 4, 2019 16:28
Show Gist options
  • Save sotolf2/8f4d9f127616834084afffd22f7c840a to your computer and use it in GitHub Desktop.
Save sotolf2/8f4d9f127616834084afffd22f7c840a to your computer and use it in GitHub Desktop.
#lang racket
(require threading)
(define low 171309)
(define high 643603)
(define (gen-passwords from to)
(map number->string (range from (+ 1 to))))
(define (increasing? instr)
(let ([charlist (string->list instr)])
(eq? (sort charlist char<?) charlist)))
(define (counts lst)
(define (count-rec lst counts)
(if (empty? lst)
counts
(if (hash-has-key? counts (car lst))
(count-rec (cdr lst) (hash-update counts (car lst) (λ (x) (+ x 1))))
(count-rec (cdr lst) (hash-set counts (car lst) 1)))))
(count-rec lst #hash()))
(define (repeated? instr)
(~>> (string->list instr)
(counts)
(hash-values)
(filter (λ (x) (> x 1)))
(length)
(< 0)))
(define (has-double? instr)
(~>> (string->list instr)
(counts)
(hash-values)
(filter (λ (x) (= 2 x)))
(length)
(< 0)))
(define passwords (gen-passwords low (+ 1 high)))
(define (part1)
(~>> passwords
(filter increasing?)
(filter repeated?)
(length)))
(define (part2)
(~>> passwords
(filter increasing?)
(filter has-double?)
(length)))
(writeln "Part 1: ")
(writeln (part1))
(writeln "Part 2: ")
(writeln (part2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment