! Copyright (C) 2023 Doug Coleman. ! See https://factorcode.org/license.txt for BSD license. USING: accessors calendar formatting http http.client io io.directories io.encodings.binary io.encodings.utf8 io.files kernel math.order sequences ; IN: aoc2023 CONSTANT: aoc2023-session "paste from browser just the numbers part" : download-aoc2023-input ( url path -- ) utf8 [ aoc2023-session "session" put-cookie [ write ] with-http-request drop ] with-file-writer ; : check-aoc2023-file ( path -- path/f ) dup utf8 file-contents "Please don't repeatedly request" head? [ delete-file f ] when ; : ensure-aoc2023-input-file-ready ( n -- n ) dup [ 2023 12 ] dip 5 >>hour now-utc before=? [ "too early for aoc2023 day %d (opens at 5:00 UTC)" sprintf throw ] unless ; : ?download-aoc2023-input ( n -- string/* ) ensure-aoc2023-input-file-ready "resource:aoc2023/" make-directories [ "https://adventofcode.com/2023/day/%d/input" sprintf ] [ "resource:aoc2023/input%s.txt" sprintf ] bi [ dup file-exists? [ 2drop ] [ download-aoc2023-input ] if ] keep check-aoc2023-file [ utf8 file-contents ] [ "still too early" throw ] if* ;