Skip to content

Commit ed61948

Browse files
committed
Generate a Pluto notebook
1 parent b1c694f commit ed61948

File tree

5 files changed

+144
-19
lines changed

5 files changed

+144
-19
lines changed

2023/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.cookie-jar
22
out/
3+
*backup [0-9]*.jl

2023/Runner.jl

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ function expectedfor(inputfile)
5555
end
5656
end
5757

58+
inputfiles(dir=pwd()) = readdir(sort=true) |> filter(x -> occursin(r"^input\..*\.txt$", x))
59+
60+
function inputstats(dir=pwd())
61+
for i in inputfiles()
62+
expect = join(readlines(Runner.expectedfile(i)), " ")
63+
count = length(readlines(i))
64+
println("$i $count lines expected: $expect")
65+
end
66+
end
67+
5868
function format_seconds(sec)
5969
if sec >= 60 * 60
6070
(m, s) = divrem(sec, 60)

2023/generate.jl

+100-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
# Script to generate a skeletal Advent of Code solution in Julia.
99

10+
using UUIDs
11+
1012
function generate_into(daydir)
1113
m = match(r"(\d+)$", daydir)
1214
if isnothing(m)
@@ -26,23 +28,26 @@ function generate_into(daydir)
2628
# Use of this source code is governed by an MIT-style
2729
# license that can be found in the LICENSE file or at
2830
# https://opensource.org/licenses/MIT.
29-
#
30-
# https://adventofcode.com/2023/day/$daynum
3131
32+
\"""# Advent of Code 2023 day $daynum
33+
[Read the puzzle](https://adventofcode.com/2023/day/$daynum)
34+
\"""
3235
module Day$daynum
3336
3437
function part1(lines)
35-
input = map(lines) do line
36-
line
37-
end
38+
input = parseinput(lines)
3839
:TODO
3940
end
4041
4142
function part2(lines)
42-
input = map(lines) do line
43+
input = parseinput(lines)
44+
:TODO
45+
end
46+
47+
function parseinput(lines)
48+
map(lines) do line
4349
line
4450
end
45-
:TODO
4651
end
4752
4853
include("../Runner.jl")
@@ -52,8 +57,95 @@ function generate_into(daydir)
5257
write(jlfile, code)
5358
chmod(jlfile, 0o755)
5459
end
60+
61+
notebook = joinpath(base, "notebook.jl")
62+
if !isfile(notebook)
63+
header_uuid = UUIDs.uuid4()
64+
preamble_uuid = UUIDs.uuid4()
65+
inputstats_uuid = UUIDs.uuid4()
66+
functions_uuid = UUIDs.uuid4()
67+
vars_uuid = UUIDs.uuid4()
68+
resultshead_uuid = UUIDs.uuid4()
69+
results_uuid = UUIDs.uuid4()
70+
code = """
71+
### A Pluto.jl notebook ###
72+
# v0.19.32
73+
#
74+
# Copyright 2023 Google LLC
75+
#
76+
# Use of this source code is governed by an MIT-style
77+
# license that can be found in the LICENSE file or at
78+
# https://opensource.org/licenses/MIT.
79+
80+
using Markdown
81+
using InteractiveUtils
82+
83+
# ╔═╡ $header_uuid
84+
@doc Day$daynum
85+
86+
# ╔═╡ $preamble_uuid
87+
begin
88+
import Pkg
89+
Pkg.activate()
90+
try
91+
@eval using Revise
92+
catch e
93+
@warn "Need to install Revise?" exception=(e)
94+
end
95+
using Day$daynum
96+
using Runner
97+
inputexample = "input.example.txt"
98+
inputactual = "input.actual.txt"
99+
run() = Runner.run_module(Day$daynum, Runner.inputfiles(); verbose=true)
100+
println("Day$daynum ready, just run() or Day$daynum.part1(readlines(inputexample))")
101+
end
102+
103+
# ╔═╡ $inputstats_uuid
104+
Runner.inputstats();
105+
106+
# ╔═╡ $functions_uuid
107+
begin
108+
function parseinput(lines)
109+
Day$daynum.parseinput(lines)
110+
#map(lines) do line
111+
#parse(Int, line)
112+
#if (m = match(r"^(\\S+) (\\S+)\$", line)) !== nothing
113+
# (foo, bar) = m.captures
114+
#end
115+
#end
116+
end
117+
end;
118+
119+
# ╔═╡ $vars_uuid
120+
begin # Useful variables
121+
exampleexpected = Runner.expectedfor(inputexample)
122+
examplelines = readlines(inputexample)
123+
input = parseinput(examplelines)
124+
end
125+
126+
# ╔═╡ $resultshead_uuid
127+
md"## Results"
128+
129+
# ╔═╡ $results_uuid
130+
Runner.run_module(Day$daynum, [
131+
inputexample,
132+
inputactual,
133+
], verbose=true)
134+
135+
# ╔═╡ Cell order:
136+
# ╟─$header_uuid
137+
# ╟─$preamble_uuid
138+
# ╠═$inputstats_uuid
139+
# ╠═$functions_uuid
140+
# ╠═$vars_uuid
141+
# ╟─$resultshead_uuid
142+
# ╠═$results_uuid
143+
"""
144+
write(notebook, code)
145+
end
146+
55147
mkinput(base, "example")
56-
inputdir = joinpath(dirname(PROGRAM_FILE), "input", daynum)
148+
inputdir = joinpath(@__DIR__, "input", daynum)
57149
mkpath(inputdir)
58150
mkinput(inputdir, "actual")
59151
for name in readdir(inputdir)

2023/pluto

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/zsh
2+
# Copyright 2023 Google LLC
3+
#
4+
# Use of this source code is governed by an MIT-style
5+
# license that can be found in the LICENSE file or at
6+
# https://opensource.org/licenses/MIT.
7+
8+
# Script to start Pluto and open a notebook in your web browser.
9+
# Usage: ./pluto day1
10+
11+
set -e
12+
DAY=${1:?missing day}
13+
cd $DAY
14+
export JULIA_LOAD_PATH=".:..:$JULIA_LOAD_PATH"
15+
plutoargs=('notebook="notebook.jl"' 'dismiss_update_notification=true')
16+
17+
if [[ ! -z $SSH_CONNECTION ]]; then
18+
# need to pick our own port in order to use root_url, see
19+
# https://github.com/fonsp/Pluto.jl/issues/2729
20+
((PORT=1234))
21+
while nc -z localhost $PORT >& /dev/null ; do
22+
PORT+=1
23+
done
24+
plutoargs+=('launch_browser=false' \
25+
'host="0.0.0.0"' "port=$PORT" \
26+
"root_url=\"http://$(hostname):$PORT/\"")
27+
fi
28+
29+
echo "Launching Pluto for $DAY"
30+
exec julia -e "import Pluto; Pluto.run(${(j:, :)plutoargs})"

2023/repl

+3-11
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,9 @@ using $MODULE
3434
using Runner
3535
inputexample = "input.example.txt"
3636
inputactual = "input.actual.txt"
37-
inputfiles() = readdir(sort=true) |> filter(x -> occursin(r"^input\..*\.txt$", x))
38-
function inputstats()
39-
for i in inputfiles()
40-
expect = join(readlines(Runner.expectedfile(i)), " ")
41-
count = length(readlines(i))
42-
println("\$i \$count lines expected: \$expect")
43-
end
44-
end
45-
run() = Runner.run_module($MODULE, inputfiles(); verbose=true)
46-
inputstats()
47-
println("$MODULE ready, just run() or $MODULE.part1(readlines(inputexample))")
37+
run() = Runner.run_module($MODULE, Runner.inputfiles(); verbose=true)
38+
Runner.inputstats()
39+
println("$MODULE, just run() or $MODULE.part1(readlines(inputexample))")
4840
EOT
4941

5042
exec julia -e "$STARTUP" -i

0 commit comments

Comments
 (0)