From 8ba3d25b3d504da4e04df950ad98c970a78bb90b Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 3 Dec 2022 07:37:57 +0100 Subject: [PATCH] Added day 3 solutions. --- input/3.txt | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/s3a.d | 13 +++ src/s3b.d | 16 +++ src/util.d | 49 +++++++++ 4 files changed, 378 insertions(+) create mode 100644 input/3.txt create mode 100644 src/s3a.d create mode 100644 src/s3b.d diff --git a/input/3.txt b/input/3.txt new file mode 100644 index 0000000..253c811 --- /dev/null +++ b/input/3.txt @@ -0,0 +1,300 @@ +PPZTzDhJPLqPhqDTqrwQZZWbmCBMJMcsNmCBFWmMcsNb +vplSlfdfGvfRRGsgNcMglsFWMWMC +jtjvFHdjjwqrwqwL +NSffhsNSjfLjfstsjtjNNjjqMqnpggHngqgHGHCgClGbCzCC +dDPZZDZFdwFWwFZFWZRTFDwGzCMlgnpgCpnzglClHMbg +DTPFZQRcdTVNhbjVbcLc +JZLDcSZSpHHrrLrJcpzBRrhlzgRTmTmvBRmm +qQsQMCbMQWqCVVvmTRhTTRhCRhTg +svbGWPqGPNLJSpZnZpnN +wLtPGCLwfWLflCPtPfLLTSbHMbSgMdtvDHghhHvdgZ +nNsFznJcJqzFFszFqrNnRzdbZDDbRMbMdRHbMdgHvZSd +VczNnjsrFrjcNprqVwTPfjGllWPQBBWlgB +nnGtjFFjFTTTGtBGmWBTWffLcMJMQlzjQPCPcChCQDJzDJJd +SSggbHVbZRgZsHZRHdVhzCcJhzhMzJhQPQ +rHsNSsSZqqrNgpLLWmCfFGqGCBWm +ZnCtCCVZmVBCQBWQnWQNCQMcLrMMgMLqLSwMSSDwjcBD +hTbGJGfTbrSfScmjwj +bbGGlTdlJTdGlFpdFvJdsbmdZWPWtnVCHQvtVHzvtHWtCtVN +pNpCNFMNFhhwDgRVdSVqwgrdmJ +nvHbPZtTHWbntTmdTRrqVRrdmz +vHWPBWvntbWnHLHZLqWtBCjjhBNhCj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MtjnDgfSSbfBjggbqBC +rlrlcppWcGdPrZNccJrSBSmCHqbSqCbSCgCHPq +NWcZgpFFgFpDFnDvtMDFzz +ZpczTjpZcnncHFDn +hSqqJWWWRRhlnFDVSFnQCB +qhNMWfqfMhJJRLRvppDzNZTpPTvwwD +rbSDCwPWwPVcSHcwHspNpNsddszmwGtswN +vBBfgQvBMvMvjLLlllqtBTNNTPPzTztTsRNPTt +vvQnjlMjjjqJqQLSrcDcPWbcHbDrDn +BphwqbwNsjsNsBdDjjhGpsGWzMMTWRTCflfDWSSzfzSlWf +cFnrrHrnZVZQQvFFVvZhRzTCWlSRMSWhZMWt +HvJVvggHvVrmQLqmNqLBbhsssw +NcBqSjGGBjNbZcHwhRVHPwPwPlZR +TvdtdtvFgHwWgdhn +tMJFTvtvFtrLJMHBqNNBzjrQcjqNmB +RrQfLfQvMFdQvLLQVfFcnbSbnwVSnbDslsbSDzDD +PHLjmHqqZCjPglsbwJbqnDbbJl +LNHLhjBHHGRfphphrtfh +FcdMchqcgdchqcfcNWWghNrRrVRjGHFjDJmGFDrRVGJD +zQtvBvtbQzpSzBptzbBznnwZJRDRVmVnGRGrDrHjZGrr +HLspTpQLBqWqLqcNgM +rnqbSSQhsshFqQQnsPSnhbnrzHzzHLNvLHCLFCvVJTlNLJTz +fcffljGBwgmwwftzNdCCdvLvtHNLTH +BjWgMGjjWZMwjjgwgMWpwPlnnqSnqQhnrbPMRDsqQs +hRJhFdNJcrbqbbMF +qZlTTgTvlHnqvllPssQrgQcQwscrQMsf +CClpSnTllZnZCDLHnlNzGSRmRthVmmzqJzhd +DLzSMtDLtzmmlDlMlMDbcrcTDqFvVvVqqTbD +dnZshHpZRChgnszRwRZCpTqqTcwVqFcrVqcBVPqVTr +snRRRdJsZgphCWlWtmJSjWWzjt diff --git a/src/s3a.d b/src/s3a.d new file mode 100644 index 0000000..3739203 --- /dev/null +++ b/src/s3a.d @@ -0,0 +1,13 @@ +module s3a; +import util; + +void main() { + readText("input/3.txt").strip.splitter("\n") + .map!(line => [ + Set!char(line[0 .. $ / 2].to!(char[])), + Set!char(line[$ / 2 .. $].to!(char[])) + ]) + .map!(sets => sets[0].intersectWith(sets[1]).items[0]) + .map!(c => c.isUpper ? c - 'A' + 27 : c - 'a' + 1) + .sum.writeln; +} diff --git a/src/s3b.d b/src/s3b.d new file mode 100644 index 0000000..1e03ed6 --- /dev/null +++ b/src/s3b.d @@ -0,0 +1,16 @@ +module s3b; +import util; + +void main() { + auto sets = readText("input/3.txt").strip.splitter("\n") + .map!(line => Set!char(line.to!(char[]))).array; + ulong sum = 0; + for (int i = 0; i < sets.length / 3; i++) { + char c = sets[i * 3] + .intersectWith(sets[i * 3 + 1]) + .intersectWith(sets[i * 3 + 2]) + .items[0]; + sum += c.isUpper ? c - 'A' + 27 : c - 'a' + 1; + } + sum.writeln; +} \ No newline at end of file diff --git a/src/util.d b/src/util.d index c9d658e..83d5084 100644 --- a/src/util.d +++ b/src/util.d @@ -12,3 +12,52 @@ public import std.path; public import std.uni; public import std.array; public import std.format; + +/** + * Simple set implementation since the stdlib doesn't really have a good one. + */ +struct Set(T) { + T[] items; + this(T[] items) { + foreach (i; items) { + if (!this.contains(i)) this.add(i); + } + } + + bool contains(T element) { + return this.items.canFind(element); + } + + bool add(T element) { + if (!this.items.canFind(element)) { + this.items ~= element; + return true; + } + return false; + } + + bool remove(T element) { + size_t[] indexes; + foreach (e, i; this.items) { + if (e == element) indexes ~= i; + } + this.items = this.items.remove(indexes); + return indexes.length > 0; + } + + Set!T intersectWith(Set!T other) { + T[] intersectingItems; + foreach (e; this.items) { + if (other.contains(e)) intersectingItems ~= e; + } + return Set(intersectingItems); + } + + Set!T unionWith(Set!T other) { + Set!T u = Set(this.items); + foreach (e; other.items) { + u.add(e); + } + return u; + } +}