-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21-postgresql.jl
176 lines (162 loc) · 5.35 KB
/
day21-postgresql.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using FunSQL
using LibPQ
using DBInterface
# Make LibPQ compatible with DBInterface.
DBInterface.connect(::Type{LibPQ.Connection}, args...; kws...) =
LibPQ.Connection(args...; kws...)
DBInterface.prepare(conn::LibPQ.Connection, args...; kws...) =
LibPQ.prepare(conn, args...; kws...)
DBInterface.execute(conn::Union{LibPQ.Connection, LibPQ.Statement}, args...; kws...) =
LibPQ.execute(conn, args...; kws...)
const funsql_array_get = FunSQL.Fun."?[?]"
const funsql_as_bigint = FunSQL.Fun."(?::bigint)"
const funsql_gcd = FunSQL.Fun.gcd
const funsql_regexp_matches = FunSQL.Fun.regexp_matches
@funsql begin
parse_jobs() =
begin
from(
regexp_matches(:input, "(\\w+): (?:(\\d+)|(\\w+) (.) (\\w+))", "g"),
columns = [captures])
define(
var => array_get(captures, 1),
val => as_bigint(array_get(captures, 2)),
ref1 => array_get(captures, 3),
op => array_get(captures, 4),
ref2 => array_get(captures, 5))
end
evaluate_step() =
begin
partition()
filter(is_null(min(val, filter = var == "root")))
partition(is_null(val) ? ref1 : var)
define(ref1_val => min(val))
partition(is_null(val) ? ref2 : var)
define(ref2_val => min(val))
define(
val =>
coalesce(
val,
if op == "+"
ref1_val + ref2_val
elseif op == "-"
ref1_val - ref2_val
elseif op == "*"
ref1_val * ref2_val
elseif op == "/"
ref1_val / ref2_val
end))
end
solve_part1() =
begin
from(jobs)
iterate(evaluate_step())
group()
define(part1 => min(val, filter = var == "root"))
end
evaluate_frac_step() =
begin
partition()
filter(is_null(min(a, filter = var == "root")))
partition(is_null(a) ? ref1 : var)
define(
a1 => min(a),
b1 => min(b),
c1 => min(c),
d1 => min(d))
partition(is_null(a) ? ref2 : var)
define(
a2 => min(a),
b2 => min(b),
c2 => min(c),
d2 => min(d))
define(
a =>
coalesce(
a,
if op == "+"
a1 * d2 + b1 * c2 + a2 * d1 + b2 * c1
elseif op == "-"
a1 * d2 + b1 * c2 - a2 * d1 - b2 * c1
elseif op == "*"
a1 * b2 + b1 * a2
elseif op == "/"
a1 * d2 + b1 * c2
end),
b =>
coalesce(
b,
if op == "+"
b1 * d2 + b2 * d1
elseif op == "-"
b1 * d2 - b2 * d1
elseif op == "*"
b1 * b2
elseif op == "/"
b1 * d2
end),
c =>
coalesce(
c,
if op == "+" || op == "-" || op == "*"
c1 * d2 + d1 * c2
elseif op == "/"
c1 * b2 + d1 * a2
end),
d =>
coalesce(
d,
if op == "+" || op == "-" || op == "*"
d1 * d2
elseif op == "/"
d1 * b2
end))
define(g => gcd(gcd(gcd(a, b), c), d))
define(
a => a / g,
b => b / g,
c => c / g,
d => d / g)
end
solve_part2() =
begin
from(jobs)
define(
a => as_bigint(case(var == "humn", 1, is_not_null(val), 0)),
b => as_bigint(case(var == "humn", 0, is_not_null(val), val)),
c => as_bigint(case(var == "humn", 0, is_not_null(val), 0)),
d => as_bigint(case(var == "humn", 1, is_not_null(val), 1)))
iterate(evaluate_frac_step())
partition()
define(
root_ref1 => min(ref1, filter = var == "root"),
root_ref2 => min(ref2, filter = var == "root"))
group()
define(
a1 => min(a, filter = var == root_ref1),
b1 => min(b, filter = var == root_ref1),
c1 => min(c, filter = var == root_ref1),
d1 => min(d, filter = var == root_ref1),
a2 => min(a, filter = var == root_ref2),
b2 => min(b, filter = var == root_ref2),
c2 => min(c, filter = var == root_ref2),
d2 => min(d, filter = var == root_ref2))
select(part2 => - (b1 * d2 - b2 * d1) / (a1 * d2 + b1 * c2 - a2 * d1 - b2 * c1))
end
solve_all() =
begin
solve_part1().cross_join(solve_part2())
with(jobs => parse_jobs())
end
const q = solve_all()
end # @funsql
if isempty(ARGS)
println(FunSQL.render(q, dialect = :postgresql))
else
const db = DBInterface.connect(FunSQL.DB{LibPQ.Connection}, "")
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