Advent of Code 2023

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
Just done for the day. Nice problems!
Have fun guys!
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
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"

:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
AOC 2023 DAY 2
Perl:
$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";
Perl:
$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";

:)
 

Trader11

Banned
Joined
Oct 14, 2018
Messages
15,691
Reaction score
5,233
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"

:)
How you deal with overlaps?

Eightwo?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
DAY 5 P2 is arduous if you think it wrongly. DAY 6 is a relaxing one though. Have fun guys!
:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
AOC 2023 DAY 3
Perl:
@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";
Perl:
@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";
:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
Day 7 is just a testament to no leetcode is complete without the introduction of cards. :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,497
Reaction score
1,255
AOC 2023 DAY 4
Perl:
$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";
Perl:
# 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";
:)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Community Guidelines and Standards, Terms of Service and Member T&Cs for more information.
Top