2023.11.30 04:22 AM
It's that time of year again! (the most wonderful time of the year? 🎄 )
Tomorrow kicks off Advent of Code 2023, and if you know the KX community you'll know we love to take part the annual series of challenges set by the brilliant Eric Wastl.
If you want to join in, learn from your peers and partake in a bit of friendly competition, join the KX leaderboard by going to Private Leaderboard - Advent of Code 2023 and using code 1526329-bb0915a5.
Feel free to use your KX Community to ask questions, post suggestions or solutions so we can use this opportunity to improve our coding skills.
Remember, it's just a bit of fun!
Happy Coding 🤶
2023.12.03 07:20 AM - edited 2023.12.03 07:21 AM
I came up with the following for day 1 / part two:
/ Part Two
txt: read0 `:day1/input.txt;
wordNumMap: ("*" ,' ((string 1 + til 9), ("one"; "two"; "three"; "four"; "five"; "six"; "seven"; "eight"; "nine")) ,' "*") ! raze flip 2#' 1 + til 9;
ansTwo: sum "I"$(raze') (raze') string wordNumMap flip ({key[wordNumMap] (where') m @' first each (where') any each' m: x (like/:\:)\: key[wordNumMap]}') ((::; neg) @\: 1 + (til') (count') txt) (#\:')\: txt;
show "Part Two: ", string ansTwo;
The idea was for every line of input, to turn it into a cumulative list from the left and right hand sides e.g.
zlmlk1 ->
z, zl, zlm, zlml, zlmlk, zlmlk1
and
1, 1k, 1kl, 1klm, 1klml, 1klmlz
Cross compare this with "*1*", "*2*", ..., "*one*", "*two*".
For the first of the cumulative lists (both left and right) that has a match, find which number-string it matched against. Map this string back to its numeric form, before concatenating the left and right matches, and summing.
I'd be interested to see other solutions (or any ways to improve the above!) 🙂
2023.12.05 12:08 AM
Here's a solution to Day 1!
Part 1
p: read0 `:problem.txt;
is:where'[p within 48 58];
sum "J"$p @' flip (min'[is];max'[is])
Part 2
p: read0 `:problem.txt;
numbers: ("one";"two";"three";"four";"five";"six";"seven";"eight";"nine");
is: where'[p within 48 58];
e:(count'[p])#\:0N;
e:@[;;:;]'[e;is;"J"$/:/:p @' is];
e: e{@'[;;:;y 0][x;y 1]}/ (1+til 9),'enlist each p ss\:/: numbers;
e: except[;0N]'[e];
sum 10 sv (first'[e];last'[e]);
2023.12.05 03:10 AM
Day Two, too!
p: read0 `:problem.txt
rules: `red`green`blue!12 13 14
p:@'[p;where'[p=";"];:;","];
s:" " vs/:/: ", " vs/: (": " vs/: p)[;1]
game:"J" $ s[;;0] @' group'[`$s[;;1]]
sum 1+where all'[all''[rules >=/: game]]
p: read0 `:problem.txt
p:@'[p;where'[p=";"];:;","];
s:" " vs/:/: ", " vs/: (": " vs/: p)[;1]
game:"J" $ s[;;0] @' group'[`$s[;;1]]
sum prd'[max''[game]]
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.