Skip to content

Instantly share code, notes, and snippets.

@NHDaly
Created December 21, 2021 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NHDaly/2d1d2099a63b458ee3668ac9a6002e82 to your computer and use it in GitHub Desktop.
Save NHDaly/2d1d2099a63b458ee3668ac9a6002e82 to your computer and use it in GitHub Desktop.
NHD advent of code 2020 delve
// def input = load_csv["/Users/nathandaly/Documents/delve/adventofcode/day1-input"]
// def nums = input[_, :Numbers]
// ic {count[nums] = 200}
def input_example = {
1721;
979;
366;
299;
675;
1456;
}
def input_real = {
1150;
1579;
1361;
1319;
1201;
1253;
1806;
1783;
1164;
1772;
1920;
1428;
1918;
245;
1504;
1952;
1057;
1977;
704;
1119;
1971;
1200;
1650;
1795;
1877;
1932;
1811;
1981;
1803;
1366;
1580;
1986;
1976;
1063;
1895;
1143;
1991;
1061;
1855;
1947;
1134;
1800;
1898;
1778;
1964;
1949;
1103;
1770;
1321;
2005;
1758;
1181;
1140;
1873;
1946;
1540;
1909;
1710;
1705;
1313;
1196;
1084;
1870;
1610;
1708;
1810;
1133;
1375;
1264;
1921;
1624;
41;
1899;
1226;
1757;
1978;
1485;
1385;
1526;
1653;
1130;
1223;
1577;
1912;
1894;
276;
954;
1269;
1769;
1924;
93;
1165;
1812;
1092;
1402;
1284;
1903;
1884;
1581;
1887;
1963;
1983;
1233;
1445;
1974;
1956;
1691;
1954;
2000;
1469;
1875;
955;
1334;
1116;
1700;
1818;
1790;
1704;
1901;
1072;
1848;
1990;
1724;
1719;
1638;
1311;
1474;
1837;
1801;
1929;
1791;
1317;
1643;
1632;
1813;
1488;
1129;
1998;
1771;
1793;
1074;
1826;
1935;
1462;
1230;
1797;
1878;
1751;
1993;
1437;
1967;
1844;
1438;
1969;
1175;
1823;
1124;
1922;
154;
936;
1117;
1145;
1308;
1320;
1767;
1850;
1809;
1350;
1820;
1082;
1597;
1913;
1766;
1701;
1294;
1556;
2006;
1480;
1953;
1104;
1861;
1966;
1248;
1671;
1955;
1863;
1202;
1356;
1842;
2010;
1288;
1067;
1576;
1295;
1760;
1888;
1639;
1282;
1633;
1619;
}
def input = input_real
def result = a*b*c
from (a,b,c)
where input(a) and input(b) and input(c) and a+b+c = 2020
/*
def num_vars = 3
@inline def nway[1] = input
@inline def nway[n] = nway[1], xs... : nway[n-1](xs...)
def o = nway[2]
*/
/*
def result2 = product[vals]
from vals
where vals = pivot[vars]
from vars...
where
//
count[vals]
*/
def input_real_csv = load_csv["/Users/nathandaly/Documents/delve/adventofcode/day2-input.csv"]
// TODO: Woah, this IC isn't even running
// ic input_csv_len(x) = {count[input_real_csv](x) and x = 1000}
def input_example_csv = {
1, (:MinMax, "1-3"; :Char, "a:"; :Pwd, "abcde");
2, (:MinMax, "1-3"; :Char, "b:"; :Pwd, "cdefg");
3, (:MinMax, "2-9"; :Char, "c:"; :Pwd, "ccccccccc");
}
def expected_violation = 2;3
// ic test_no_false_positives(v){
// valid(v) implies not expected_violation(v)
// }
// ic test_no_misses(v){
// expected_violation(v) implies not valid(v)
// }
def input = input_real_csv
def pwd[i] = input[i][:Pwd]
def pwd_divider[i] = idx :
substring[s, idx, idx] = "-" and range[2,string_length[s],1](idx)
from s where s = input[i][:MinMax]
def pwd_char_min[i] = parse_int[substring[s, 1, pwd_divider[i]-1]] from s where input[i][:MinMax](s)
def pwd_char_max[i] = parse_int[substring[s, pwd_divider[i]+1, string_length[s]]] from s where input[i][:MinMax](s)
def pwd_char[i] = substring[s, 1, 1] from s where input[i][:Char](s)
def valid_min[i] = pwd(i,_),
(if pwd_char[i] = substring[pwd[i], pwd_char_min[i], pwd_char_min[i]] then 1 else 0 end)
def valid_max[i] = pwd(i,_),
(if pwd_char[i] = substring[pwd[i], pwd_char_max[i], pwd_char_max[i]] then 1 else 0 end)
def valid[i] = (valid_min[i] + valid_max[i]) = 1
def total_valid = count[valid]
def input_real_csv = load_csv["/Users/nathandaly/Documents/delve/adventofcode/day2-input.csv"]
// TODO: Woah, this IC isn't even running
// ic input_csv_len(x) = {count[input_real_csv](x) and x = 1000}
def input_example_csv = {
1, (:MinMax, "1-3"; :Char, "a:"; :Pwd, "abcde");
2, (:MinMax, "1-3"; :Char, "b:"; :Pwd, "cdefg"); // Not enough (0)
3, (:MinMax, "2-9"; :Char, "c:"; :Pwd, "ccccccccc");
// Custom added examples
// multi-digit min/max
4, (:MinMax, "10-100"; :Char, "e:"; :Pwd, "eeeeeeeeeeeeeeee");
4, (:MinMax, "10-100"; :Char, "e:"; :Pwd, "eeezeeezeeeezeeeeee");
5, (:MinMax, "10-100"; :Char, "e:"; :Pwd, "ee"); // Not enough
6, (:MinMax, "10-100"; :Char, "e:"; :Pwd, "a"); // Not enough (0)
7, (:MinMax, "1-3"; :Char, "f:"; :Pwd, "ffffff"); // Too many
8, (:MinMax, "1-3"; :Char, "f:"; :Pwd, "fafafafafaf"); // Too many
// Strict requirement
9, (:MinMax, "3-3"; :Char, "f:"; :Pwd, "fff");
10, (:MinMax, "3-3"; :Char, "f:"; :Pwd, "ff"); // Too few
11, (:MinMax, "3-3"; :Char, "f:"; :Pwd, "ffff"); // Too many
12, (:MinMax, "3-3"; :Char, "f:"; :Pwd, "oooff"); // Too few
13, (:MinMax, "3-3"; :Char, "f:"; :Pwd, "oooffff"); // Too many
// border cases
14, (:MinMax, "2-4"; :Char, "f:"; :Pwd, "ooff");
15, (:MinMax, "2-4"; :Char, "f:"; :Pwd, "ooffff");
16, (:MinMax, "2-4"; :Char, "f:"; :Pwd, "oooof"); // Too few (by 1)
17, (:MinMax, "2-4"; :Char, "f:"; :Pwd, "oooofffff"); // Too many (by 1)
}
// def expected_violation = 2;5;6;7;8;10;11;12;13;16;17
// ic test_no_false_positives(v){
// violation(v) implies expected_violation(v)
// }
// ic test_no_misses(v){
// expected_violation(v) implies violation(v)
// }
def input = input_real_csv
def pwd[i] = input[i][:Pwd]
def pwd_divider[i] = idx :
substring[s, idx, idx] = "-" and range[2,string_length[s],1](idx)
from s where s = input[i][:MinMax]
def pwd_char_min[i] = parse_int[substring[s, 1, pwd_divider[i]-1]] from s where input[i][:MinMax](s)
def pwd_char_max[i] = parse_int[substring[s, pwd_divider[i]+1, string_length[s]]] from s where input[i][:MinMax](s)
def pwd_char[i] = substring[s, 1, 1] from s where input[i][:Char](s)
def char_positions[i] = x :
pwd_char[i] = substring[s, x, x] and
x = range[1, string_length[s], 1]
from s where pwd[i](s)
def char_count[i] =
// Default value: count of 0
if char_positions(i,_) then count[char_positions[i]] else (0, pwd(i,_)) end
def violation[i] = char_count[i] < pwd_char_min[i] or char_count[i] > pwd_char_max[i]
def total_valid = count[pwd] - count[violation]
def input_csv = load_csv["/Users/nathandaly/Documents/delve/adventofcode/day3-input.csv"]
def input_lines(i,l) = sort[p : input_csv[p, :lines]](i,p,l) from p
def width = string_length[input_lines[1]]
ic all_lines_same_length(i,l) { input_lines(i,l) implies string_length[l] = width }
def tree(r,c) =
substring[input_lines[r], c, c] = "#" and
range[1, width, 1](c)
@inline def wrap_col[c] = ((c-1) % (width))+1
@inline def is_tree(r,c) =
tree(r,cc) from cc where cc = wrap_col[c]
// ic wrap_tests {
// wrap_col[1] = 1 and
// wrap_col[2] = 2 and
// wrap_col[11] = 11 and
// wrap_col[12] = 1
// }
@inline def slope[mr, mc][i] =
{:r, 1+ mr*i} ; {:c, 1 + mc*i}
@inline
def path_tree[mr,mc](r,c) =
is_tree(r,c) and
slope[mr,mc][i][:r](r) and
slope[mr,mc][i][:c](c)
from i where
range[0, count[input_lines], 1](i)
@inline
def path_count[mr,mc] = count[path_tree[mr,mc]]
def path_counts = path_count[mr,mc]
for (mr,mc) where {
1,1;
1,3;
1,5;
1,7;
2,1;
}(mr,mc)
def final_product = product[path_counts]
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
lines
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
def input_csv = load_csv["/Users/nathandaly/Documents/delve/adventofcode/day3-input.csv"]
def input_lines(i,l) = sort[p : input_csv[p, :lines]](i,p,l) from p
def width = string_length[input_lines[1]]
ic all_lines_same_length(i,l) { input_lines(i,l) implies string_length[l] = width }
def tree(r,c) =
substring[input_lines[r], c, c] = "#" and
range[1, width, 1](c)
@inline def wrap_col[c] = ((c-1) % (width))+1
@inline def is_tree(r,c) =
tree(r,cc) from cc where cc = wrap_col[c]
// ic wrap_tests {
// wrap_col[1] = 1 and
// wrap_col[2] = 2 and
// wrap_col[11] = 11 and
// wrap_col[12] = 1
// }
def path_tree(r,c) =
is_tree(r,c) and
r = 1+i and c = 1 + 3*i
from i where
range[0, count[input_lines], 1](i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment