aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit 2ee04889f1d9832f021cbfb86d49a5149fa536ad
parent 7a0da710b68026ad2096fc9b693c7690849960ff
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue,  1 Dec 2020 13:48:29 +0100

improved helper scripts and added day one

Diffstat:
M.gitignore | 2++
Maoc | 3+++
Aday1/input | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday1/main.zig | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday1/part1 | 28++++++++++++++++++++++++++++
Aday1/part2 | 9+++++++++
Mscripts/prepare | 8+++++---
7 files changed, 316 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,3 @@ data/cookie +main +zig-cache diff --git a/aoc b/aoc @@ -11,6 +11,8 @@ if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then if [ -z "$PS1_PREFIX" ]; then echo "Enabling AoC env.." + export AOCBIN="$REPOROOT/aoc" + export PS1_PREFIX="[🎄]:" export PS1="$PS1_PREFIX$PS1" @@ -27,6 +29,7 @@ if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then export PATH=$(rmprefix "$PATH" "$PATH_PREFIX") export PATH_PREFIX= + export AOCBIN= export REPOROOT= fi else diff --git a/day1/input b/day1/input @@ -0,0 +1,200 @@ +1650 +1174 +1156 +1874 +1958 +1918 +1980 +1588 +1863 +1656 +1843 +1738 +2001 +1883 +1941 +1602 +1881 +1927 +1284 +1474 +1942 +1992 +1925 +1990 +1831 +1907 +1914 +1815 +1921 +1589 +1224 +1148 +1223 +935 +1726 +1828 +1838 +1611 +1960 +1668 +1744 +1566 +1902 +1203 +1975 +1225 +2000 +1678 +1950 +572 +1812 +1568 +1484 +1767 +1509 +1658 +1127 +1870 +1098 +1294 +1310 +1483 +1865 +1967 +1856 +1963 +1929 +1119 +132 +1969 +1094 +1523 +1701 +1896 +1631 +1956 +1910 +1672 +1232 +1285 +1761 +1649 +1931 +1959 +1191 +1846 +1908 +1976 +1500 +1940 +1924 +1521 +1989 +1635 +1102 +1114 +1948 +2007 +1964 +1926 +1590 +1900 +1690 +1880 +1596 +1395 +1373 +1937 +1833 +1845 +1949 +1128 +1218 +1928 +1912 +1893 +1869 +960 +1813 +1645 +1490 +1318 +1934 +1259 +2005 +1522 +1270 +1089 +1674 +1997 +1112 +1954 +1769 +1829 +1814 +1922 +1904 +1894 +1595 +1103 +237 +1943 +1364 +1906 +1971 +1998 +1461 +1606 +1911 +1545 +1952 +1917 +1582 +1994 +1946 +1935 +1844 +1938 +1633 +2004 +1132 +1530 +1915 +1982 +1871 +1852 +1613 +1476 +1216 +1834 +1939 +409 +1895 +1120 +1194 +1135 +1899 +1901 +1439 +485 +1855 +1136 +200 +1887 +250 +1930 +1506 +1945 +1988 +1170 +1575 +1872 +1261 +1137 +1978 +1537 +1897 +1837 +1753 +1913 diff --git a/day1/main.zig b/day1/main.zig @@ -0,0 +1,69 @@ +const std = @import("std"); +const ArrayList = std.ArrayList; +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +const heapalloc = &gpa.allocator; + +const Parts = struct { + a: i32, b: i32 +}; +fn findparts(intlist: ArrayList(i32), sum: i32) !Parts { + var start: usize = 0; + const items = intlist.items; + var end: usize = items.len - 1; + while (start != end) { + const csum = items[start] + items[end]; + if (csum == sum) { + return Parts{ .a = items[start], .b = items[end] }; + } else if (csum > sum) { + end -= 1; + } else { + start += 1; + } + } + return error.Error; +} + +fn part1(intlist: ArrayList(i32), args: [][*:0]u8) !void { + std.sort.sort(i32, intlist.items, {}, comptime std.sort.asc(i32)); + + var parts = try findparts(intlist, 2020); + std.debug.print("{}\n", .{parts.a * parts.b}); +} + +fn part2(intlist: ArrayList(i32), args: [][*:0]u8) !void { + std.sort.sort(i32, intlist.items, {}, comptime std.sort.asc(i32)); + + var third: u32 = 0; + while (third < intlist.items.len) { + var tmp = intlist.items[third]; + intlist.items[third] = -2020; + var parts = findparts(intlist, 2020 - tmp) catch |err| { + intlist.items[third] = tmp; + third += 1; + continue; + }; + std.debug.print("{}\n", .{parts.a * parts.b * tmp}); + return; + } +} + +pub fn main() !void { + if (std.os.argv.len < 2) return; + + var argv = std.os.argv; + const file = try std.fs.cwd().openFile("input", .{}); + const reader = file.reader(); + + var buf: [256]u8 = undefined; + var intlist = ArrayList(i32).init(heapalloc); + + while (try reader.readUntilDelimiterOrEof(buf[0..], '\n')) |result| { + try intlist.append(try std.fmt.parseInt(i32, result, 10)); + } + + if (std.mem.eql(u8, std.mem.span(argv[1]), "1")) { + try part1(intlist, argv[2..]); + } else if (std.mem.eql(u8, std.mem.span(argv[1]), "2")) { + try part2(intlist, argv[2..]); + } +} diff --git a/day1/part1 b/day1/part1 @@ -0,0 +1,28 @@ +--- Day 1: Report Repair --- + +After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. + +The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room. + +To save your vacation, you need to get all fifty stars by December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. + +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. + +For example, suppose your expense report contained the following: + +1721 +979 +366 +299 +675 +1456 + +In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. + +Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? + + diff --git a/day1/part2 b/day1/part2 @@ -0,0 +1,9 @@ +--- Part Two --- + +The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria. + +Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950. + +In your expense report, what is the product of the three entries that sum to 2020? + + diff --git a/scripts/prepare b/scripts/prepare @@ -9,12 +9,14 @@ function load() { echo -n "$1.." $REPOROOT/scripts/scrape ${@:2} rv=$? - [ $rv -eq 0 ] && echo "done" || echo "fail" + if [ $rv -eq 0 ]; then + echo "done" + else + echo "fail" + fi return $rv } -set -e - if [[ "$1" =~ "[0-9]+" ]]; then echo "Not a number" exit 1