diff --git a/.replit b/.replit new file mode 100644 index 0000000..d409ef8 --- /dev/null +++ b/.replit @@ -0,0 +1,2 @@ +language = "nodejs" +run = "npm start" \ No newline at end of file diff --git a/solutions/day1/input.txt b/solutions/day1/input.txt new file mode 100644 index 0000000..261c1cc --- /dev/null +++ b/solutions/day1/input.txt @@ -0,0 +1,200 @@ +1865 +1179 +1328 +1390 +322 +1999 +1713 +1808 +1380 +1727 +1702 +1304 +1481 +1334 +1728 +1953 +1413 +1753 +1723 +1379 +1532 +1918 +1490 +71 +1388 +1519 +807 +1427 +1729 +1952 +970 +1405 +1500 +1782 +1899 +1501 +1720 +1832 +1706 +1658 +1333 +486 +1670 +1674 +1290 +1893 +1382 +1761 +1945 +1607 +1319 +1508 +1464 +1733 +1917 +1483 +1620 +1677 +1835 +1810 +1385 +1840 +1705 +1994 +1695 +1599 +1681 +1566 +1153 +1672 +1373 +1679 +1714 +1283 +1950 +1197 +1696 +1936 +1218 +1910 +1786 +958 +1565 +1583 +1914 +1853 +1682 +1267 +1543 +1322 +1965 +1406 +860 +1754 +1867 +1393 +1404 +1191 +1861 +2007 +1613 +1938 +1340 +1227 +1628 +1826 +1638 +1970 +1993 +1748 +496 +1661 +1736 +1593 +1298 +1882 +1763 +1616 +1848 +92 +1338 +1707 +1587 +1996 +1708 +700 +1460 +1673 +1450 +1815 +1537 +1825 +1683 +1743 +1949 +1933 +1289 +1905 +1307 +1845 +1855 +1955 +1554 +1570 +1931 +1780 +1929 +1980 +1978 +1998 +1371 +1981 +1817 +1722 +1545 +1561 +1352 +1935 +1553 +1796 +1847 +1640 +1414 +1198 +1669 +1909 +1879 +1744 +1783 +1367 +1632 +1990 +1937 +1919 +1431 +2002 +1603 +1948 +1317 +1282 +1349 +1839 +1575 +1730 +1849 +1959 +1971 +2009 +1641 +1402 +1924 +1757 +1605 +1693 +1762 +283 +1525 +1964 +1715 +1602 diff --git a/solutions/day1/solution.js b/solutions/day1/solution.js new file mode 100644 index 0000000..1ea580d --- /dev/null +++ b/solutions/day1/solution.js @@ -0,0 +1,45 @@ +const path = require('path') +const { read, position } = require('promise-path') +const fromHere = position(__dirname) +const report = (...messages) => console.log(`[${require(fromHere('../../package.json')).logName} / ${__dirname.split(path.sep).pop()}]`, ...messages) + +async function run () { + const input = (await read(fromHere('input.txt'), 'utf8')).trim().split('\n').map(x => parseInt(x, 10)) + + await solveForFirstStar(input) + await solveForSecondStar(input) +} + +function findSum (array, startIndex, sum, count = 2) { + for (let i = 0; i < array.length; i++) { + const n = array[i] + if (count > 2) { + const searchResult = findSum(array, i + 1, sum - n, count - 1) + if (searchResult) { + return [n, ...searchResult] + } + } else if (count === 2) { + for (let j = startIndex + 1; j < array.length; j++) { + if (n + array[j] === sum) { + return [n, array[j]] + } + } + } else { + throw new Error('findSum: Invalid count') + } + } + return null +} + +async function solveForFirstStar (input) { + const solution = findSum(input, 0, 2020, 2).reduce((acc, cur) => acc * cur, 1) + report('Input:', input) + report('Solution 1:', solution) +} + +async function solveForSecondStar (input) { + const solution = findSum(input, 0, 2020, 3).reduce((acc, cur) => acc * cur, 1) + report('Solution 2:', solution) +} + +run() diff --git a/solutions/day1/viewer.html b/solutions/day1/viewer.html new file mode 100644 index 0000000..d583fae --- /dev/null +++ b/solutions/day1/viewer.html @@ -0,0 +1,43 @@ + + +
+For interesting problems; this page can be used as a dynamic viewer.
+{{ inputText }}
+ {{ solutionText }}
+