From 7bd6da2c29c3f70817a1f4c9847da42a0209dd0b Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Sun, 4 Dec 2022 16:23:33 +0100
Subject: [PATCH] Add 2017 - Day 2 - Part 1

---
 2017/02/input.txt         | 16 ++++++++++++++++
 2017/02/main.sql          | 13 +++++++++++++
 2017/utils/procedures.sql |  2 +-
 README.md                 |  2 +-
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 2017/02/input.txt
 create mode 100644 2017/02/main.sql

diff --git a/2017/02/input.txt b/2017/02/input.txt
new file mode 100644
index 0000000..ae1a5ca
--- /dev/null
+++ b/2017/02/input.txt
@@ -0,0 +1,16 @@
+414	382	1515	319	83	1327	116	391	101	749	1388	1046	1427	105	1341	1590
+960	930	192	147	932	621	1139	198	865	820	597	165	232	417	19	183
+3379	987	190	3844	1245	1503	3151	3349	2844	4033	175	3625	3565	179	3938	184
+116	51	32	155	102	92	65	42	48	91	74	69	52	89	20	143
+221	781	819	121	821	839	95	117	626	127	559	803	779	543	44	369
+199	2556	93	1101	122	124	2714	625	2432	1839	2700	2636	118	2306	1616	2799
+56	804	52	881	1409	47	246	1368	1371	583	49	1352	976	400	1276	1240
+1189	73	148	1089	93	76	3205	3440	3627	92	853	95	3314	3551	2929	3626
+702	169	492	167	712	488	357	414	187	278	87	150	19	818	178	686
+140	2220	1961	1014	2204	2173	1513	2225	443	123	148	580	833	1473	137	245
+662	213	1234	199	1353	1358	1408	235	917	1395	1347	194	565	179	768	650
+119	137	1908	1324	1085	661	1557	1661	1828	1865	432	110	658	821	1740	145
+1594	222	4140	963	209	2782	180	2591	4390	244	4328	3748	4535	192	157	3817
+334	275	395	128	347	118	353	281	430	156	312	386	160	194	63	141
+146	1116	153	815	2212	2070	599	3018	2640	47	125	2292	165	2348	2694	184
+1704	2194	1753	146	2063	1668	1280	615	163	190	2269	1856	150	158	2250	2459
diff --git a/2017/02/main.sql b/2017/02/main.sql
new file mode 100644
index 0000000..381cd60
--- /dev/null
+++ b/2017/02/main.sql
@@ -0,0 +1,13 @@
+CALL aoc_text_file('02');
+
+UPDATE "02.output" SET result = (
+	SELECT SUM(row.difference)
+	FROM (
+		SELECT MAX(cell::INTEGER) - MIN(cell::INTEGER) AS difference
+		FROM "02.input"
+		CROSS JOIN REGEXP_SPLIT_TO_TABLE(input, '\t') cell
+		GROUP BY input
+	) row
+) WHERE part = 1;
+
+SELECT aoc_results('02');
diff --git a/2017/utils/procedures.sql b/2017/utils/procedures.sql
index dcf0b4e..90f9b12 100644
--- a/2017/utils/procedures.sql
+++ b/2017/utils/procedures.sql
@@ -40,6 +40,6 @@ CREATE OR REPLACE PROCEDURE aoc_text_file(day TEXT) AS
 $$
 BEGIN
 	CALL aoc_setup_tables(day, 'input TEXT');
-	EXECUTE FORMAT('COPY %I FROM ''/aoc/%s/input.txt''', day || '.input', day);
+	EXECUTE FORMAT('COPY %I FROM ''/aoc/%s/input.txt'' WITH DELIMITER E''\1''', day || '.input', day);
 END
 $$ LANGUAGE plpgsql;
diff --git a/README.md b/README.md
index 607bec0..0822065 100644
--- a/README.md
+++ b/README.md
@@ -70,7 +70,7 @@ The versions should not matter, but I used Visual Studio 2019 with `MSVC v142 (1
 | Year | Day | Language |  /  | Year | Day | Language   |  /  | Year | Day | Language |
 |-----:|----:|----------|-----|-----:|----:|------------|-----|-----:|----:|----------|
 | 2015 |  01 | NASM x64 |  /  | 2017 |  01 | PostgreSQL |  /  | 2020 |  01 | Rust     |
-| 2015 |  02 | NASM x64 |  /  |      |     |            |  /  | 2020 |  02 | Rust     |
+| 2015 |  02 | NASM x64 |  /  | 2017 |  02 | PostgreSQL |  /  | 2020 |  02 | Rust     |
 | 2015 |  03 | NASM x64 |  /  |      |     |            |  /  | 2020 |  03 | Rust     |
 |      |     |          |  /  |      |     |            |  /  | 2020 |  04 | Rust     |
 |      |     |          |  /  |      |     |            |  /  | 2020 |  05 | Rust     |