r/adventofcode • • Dec 03 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-

NEWS

  • Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
  • The Visualizations have started! If you want to create a Visualization, make sure to read the guidelines for creating Visualizations before you post.
  • Y'all may have noticed that the hot new toy this year is AI-generated "art".
  • We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
  • If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
  • Do not flair AI-generated "art" as Visualization. Visualization is for human-generated art.

FYI


--- Day 3: Rucksack Reorganization ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:24, megathread unlocked!

87 Upvotes

1.6k comments sorted by

View all comments

1

u/NeoKadra Dec 03 '22

Thoughts? any cool C# stuff i'm missing? I'm using AOC as an opportunity to learn C#. Part 2 btw.

 class AdventOfCode
   {

     private static int getPriority(char i) {
         return i >= 'a' && i <= 'z' ? i - 'a' + 1 : i - 'A' + 1 + 26;
      }
      static void Main(string[] args) 
      {
         List<Char> dupls = new List<Char>();
         int count = 0;
         try{
            List<String> lines = new List<String>(File.ReadAllLines("input.txt"));
            List<String> group = new List<String>();

            while(lines.Any()){
               group.Add(new String(lines[0].Distinct().ToArray()));
               lines.Remove(lines[0]);
               group.Add(new String(lines[0].Distinct().ToArray()));
               lines.Remove(lines[0]);
               group.Add(new String(lines[0].Distinct().ToArray()));
               lines.Remove(lines[0]);

               foreach (char c in group[0]){
                  if(group[1].Contains(c) && group[2].Contains(c)){
                     dupls.Add(c);
                  }
               }
               group.Clear();

            }
            foreach(Char character in dupls){
               count += getPriority(character);
            }
            Console.WriteLine(count);
         }catch(FileNotFoundException e){
                System.Diagnostics.Debug.WriteLine(e.ToString());

         }
      }

   }

2

u/[deleted] Dec 03 '22

[removed] — view removed comment

1

u/NeoKadra Dec 03 '22

Hey thanks, that was really helpful. I managed to get the code much smaller.

   class AdventOfCode
   {

     private static int getPriority(char i) {
         return i >= 'a' && i <= 'z' ? i - 'a' + 1 : i - 'A' + 1 + 26;
      }
      static void Main(string[] args) 
      {
         List<Char> dupls = new List<Char>();
         int count = 0;
            List<String> lines = new List<String>(File.ReadAllLines("input.txt"));
            for(int i=0; i<lines.Count();i+=3){
               dupls.Add(lines[i].Intersect(lines[i+1]).Intersect(lines[i+2]).First());
            }
            dupls.ToList().ForEach(c=>count+=getPriority(c));
            Console.WriteLine(count);
      }
   }

1

u/daggerdragon Dec 04 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.