davidktw
Arch-Supremacy Member
- Joined
- Apr 15, 2010
- Messages
- 13,497
- Reaction score
- 1,255
It is a leetcode like xmas entertainment!bro ... your website?
Yup...I am using it to learn new languageJust done for the day. Nice problems!
Have fun guys!
$total=0;
while (<>) {
chomp;
s/[^0-9]//g;
$total += substr($_,0,1).substr($_,-1,1)
}
print $total,"\n"
$total=0;
while (<>) {
chomp;
#$old=$_;
s/one/o1e/g;
s/two/t2o/g;
s/three/t3e/g;
s/four/f4r/g;
s/five/f5e/g;
s/six/s6x/g;
s/seven/s7n/g;
s/eight/e8t/g;
s/nine/n9e/g;
s/[^0-9]//g;
$total += substr($_,0,1).substr($_,-1,1)
}
print $total,"\n"
$TOTAL = 0;
%MAX = (red => 12, green => 13, blue => 14);
EACHGAME:
while (<>) {
($gameid) = /^Game (\d+):/;
@picks = split(/\s*;\s*/);
foreach (@picks) {
while (/(\d+)\s+(blue|green|red)/g) {
next EACHGAME if ($1 > $MAX{$2});
}
}
$TOTAL += $gameid;
}
print "$TOTAL\n";
$TOTAL = 0;
while (<>) {
%MAX = (red => 0, green => 0, blue => 0);
@picks = split(/\s*;\s*/);
foreach (@picks) {
while (/(\d+)\s+(blue|green|red)/g) {
$MAX{$2} = $1 if $MAX{$2} < $1;
}
}
$POWER = 1;
map { $POWER *= $_ } values(%MAX);
$TOTAL += $POWER;
}
print "$TOTAL\n";
How you deal with overlaps?Allow me to show my Perl solution for
AOC 2023 DAY 1
Perl:$total=0; while (<>) { chomp; s/[^0-9]//g; $total += substr($_,0,1).substr($_,-1,1) } print $total,"\n"Perl:$total=0; while (<>) { chomp; #$old=$_; s/one/o1e/g; s/two/t2o/g; s/three/t3e/g; s/four/f4r/g; s/five/f5e/g; s/six/s6x/g; s/seven/s7n/g; s/eight/e8t/g; s/nine/n9e/g; s/[^0-9]//g; $total += substr($_,0,1).substr($_,-1,1) } print $total,"\n"
![]()
Isn’t the answer provided in the solution already? Notice what I replace “eight” and “two” into?How you deal with overlaps?
Eightwo?
@lines=();
# get all lines first
while (<>) {
chomp;
# pad left and right
push(@lines, ".$_.");
}
$total = 0;
$lineno = 0;
foreach $_ (@lines) {
# for each set of numbers per line
while (/\d+/g) {
($l, $r) = (@-, @+);
# assume it is a part number first, add it in
$total += substr($_, $l, $r - $l);
# check that left and right of digits have no symbols
if (substr($_, $l - 1, 1) eq '.' and substr($_, $r, 1) eq '.') {
# check top if possible are only digits or dots (include diagonals)
# skip in if it's first line
if ($lineno == 0 or substr($lines[$lineno - 1], $l - 1, $r - $l + 2) =~ /^[\d\.]*$/) {
# check bottom if possible are only digits or dots (include diagonals)
# skip in if it's last line
if ($lineno == $#lines or substr($lines[$lineno + 1], $l - 1, $r - $l + 2) =~ /^[\d\.]*$/) {
# all fulfill must be non part number
# hence remove this number from the total
$total -= substr($_, $l, $r - $l);
}
}
}
}
$lineno++;
}
print "TOTAL=$total\n";
@lines=();
# get all lines first
while (<>) {
chomp;
# pad front and back of each line
push(@lines, ".$_.");
}
$total = 0;
$lineno = 0;
foreach $_ (@lines) {
# find '*'
while (/\*/g) {
# to contain numbers around each '*'
@numbers = ();
($l, $r) = (@-, @+);
# left of '*' consist of digits ?
push(@numbers, $&) if (substr($_, 0, $l) =~ /\d+$/);
# right of '*' consist of digits ?
push(@numbers, $&) if (substr($_, $r) =~ /^\d+/);
# top of '*' consist of 1 or more sets of digits ?
if ($lineno > 0) {
# for each number found
while ($lines[$lineno - 1] =~ /\d+/g) {
# check that the number touches next to the '*'
push(@numbers, $&) if ($+[0] >= $l and $-[0] <= $l + 1);
}
}
# bottom of '*' consist of 1 or more sets of digits ?
if ($lineno < $#lines) {
# for each number found
while ($lines[$lineno+1] =~ /\d+/g) {
# check that the number touches next to the '*'
push(@numbers, $&) if ($+[0] >= $l and $-[0] <= $l + 1);
}
}
$total += $numbers[0] * $numbers[1] if (@numbers == 2);
}
$lineno++;
}
print "TOTAL=$total\n";
$total = 0;
while (<>) {
$multipler = 0;
chomp;
s/^Card\s+\d+://;
%win_nos = ();
($wins,$have) = split /\|/;
while ($wins =~ /(\d+)/g) {
$win_nos{$1} = 1;
}
while ($have =~ /(\d+)/g) {
$multipler = $multipler == 0 ? 1 : $multipler * 2 if ($win_nos{$1});
}
$total += $multipler;
}
print "$total\n";
# contains the copies discovered along the way
%copies = ();
# this is a limiter to prevent non-existent copies of cards
# from being manifested due to the counting is done
$maxcards = 0;
while (<>) {
$maxcards++;
# obtain the card id, remove the words,
# and add one copy of the card
chomp;
($cardid) = /^Card\s+(\d+):/;
s/^Card\s+(\d+)://;
$copies{$cardid}++;
# extract the winning numbers
%win_nos = ();
($wins,$have) = split /\|/;
while ($wins =~ /(\d+)/g) {
$win_nos{$1} = 1;
}
# find how many wins are there for this card
$wins = 0;
while ($have =~ /(\d+)/g) {
$wins++ if ($win_nos{$1});
}
# knowing there are N wins for this card
# for this cardid+1, carddid+2, ... cardid+N
# add each of them the current copies there are for this card
for ($i = 1; $i <= $wins; $i++) {
$copies{$cardid + $i} += $copies{$cardid};
}
}
# only sum up copies with cardid that is <= $noofcards
# since there is potentially extra copies of cards added due to
# the way how copies of cards are duplicated in the code above
$total = 0;
map { $total += $copies{$_} if ($_ <= $maxcards) } keys(%copies);
print "$total\n";