-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializePuzzle.fsx
62 lines (49 loc) · 1.71 KB
/
initializePuzzle.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
open System
open System.IO
open System.Net.Http
let args = fsi.CommandLineArgs |> Array.tail
let now = DateTime.Now
let day =
if args.Length > 0 then
args[0] |> int
else
now.Day
let createIfMissing path contents =
printfn "%s" path
if File.Exists(path) = false then
File.WriteAllText(path, contents)
let getCookie () =
let path = Path.Combine(__SOURCE_DIRECTORY__, "Input", (now.Year.ToString()), "session.cookie")
if File.Exists(path) = false then
failwithf "No session cookie found at %s" path
File.ReadAllText(path)
let downloadPuzzleInput () =
let puzzleUrl = sprintf "https://adventofcode.com/%i/day/%i/input" now.Year day
let client = new HttpClient()
let message = new HttpRequestMessage(HttpMethod.Get, Uri(puzzleUrl))
message.Headers.Add("Cookie", sprintf "session=%s" (getCookie ()))
let response = client.Send(message);
match response.IsSuccessStatusCode with
| true ->
(new StreamReader(response.Content.ReadAsStream())).ReadToEnd()
| false ->
printfn "Could not read puzzle from AoC url %s" puzzleUrl
""
let fsxPath = Path.Combine(__SOURCE_DIRECTORY__, "Puzzles", (now.Year.ToString()), sprintf "Day%02i.fsx" day)
let fsxContents =
sprintf
"""#time "on"
#load "../../Tools.fs"
open System
open System.IO
open AdventOfCode
open Checked
let getInput name = File.ReadAllLines(getInputPath%i name)
let solve1 input =
getInput input |> Dump
solve1 "Day%02i_sample1.txt"
"""
now.Year day
createIfMissing fsxPath fsxContents
let inputPath = Path.Combine(__SOURCE_DIRECTORY__, "Input", (now.Year.ToString()), sprintf "Day%02i.txt" day)
createIfMissing inputPath (downloadPuzzleInput())