-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03-sqlite.jl
91 lines (77 loc) · 2.23 KB
/
day03-sqlite.jl
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using FunSQL
using SQLite
const funsql_instr = FunSQL.Fun.instr
const funsql_length = FunSQL.Fun.length
const funsql_mod = FunSQL.Fun.mod
const funsql_substr = FunSQL.Fun.substr
@funsql begin
split_line(text) =
instr($text, "\n") > 0 ? substr($text, 1, instr($text, "\n") - 1) : $text
split_rest(text) =
instr($text, "\n") > 0 ? substr($text, instr($text, "\n") + 1) : ""
split_lines_one_step() =
begin
filter(rest != "")
define(
index => index + 1,
line => split_line(rest),
rest => split_rest(rest))
end
split_lines(text) =
begin
define(
index => 0,
rest => $text)
split_lines_one_step()
iterate(split_lines_one_step())
end
parse_rucksacks() =
split_lines(:input)
from_characters() =
from($([(char = string(ch),
priority = ch in 'a':'z' ? ch - 'a' + 1 : ch - 'A' + 27)
for ch in [('a':'z')..., ('A':'Z')...]]))
solve_part1() =
begin
from(rucksacks)
define(
left => substr(line, 1, length(line) / 2),
right => substr(line, 1 + length(line) / 2))
join(from(characters), on = instr(left, char) && instr(right, char))
group()
define(part1 => sum(priority))
end
solve_part2() =
begin
from(rucksacks)
partition(order_by = [index])
define(
line1 => lag(line, 2),
line2 => lag(line, 1),
line3 => line)
filter(mod(index, 3) == 0)
join(
from(characters),
on = instr(line1, char) && instr(line2, char) && instr(line3, char))
group()
define(part2 => sum(priority))
end
solve_all() =
begin
solve_part1().cross_join(solve_part2())
with(
rucksacks => parse_rucksacks(),
characters => from_characters())
end
const q = solve_all()
end # @funsql
if isempty(ARGS)
println(FunSQL.render(q, dialect = :sqlite))
else
const db = DBInterface.connect(FunSQL.DB{SQLite.DB})
for file in ARGS
input = read(file, String)
output = first(DBInterface.execute(db, q, input = input))
println("[$file] part1: $(output.part1), part2: $(output.part2)")
end
end