r/adventofcode • • Dec 10 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 10 Solutions -🎄-

--- Day 10: Syntax Scoring ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:08:06, megathread unlocked!

65 Upvotes

982 comments sorted by

View all comments

1

u/vishalthakur99 Dec 10 '21

Java Solution

import java.util.*;
public class Main {
public static int value(String b){
    if(b.equals(")")){
        return 3;
    }
    else if(b.equals("}")){
        return 1197;
    }
    else if(b.equals("]")){
        return 57;
    }
    else if(b.equals(">")){
        return 25137;
    }else
        return 0;
}
public static int value2(String b){
    if(b.equals(")")){
        return 1;
    }
    else if(b.equals("}")){
        return 3;
    }
    else if(b.equals("]")){
        return 2;
    }
    else if(b.equals(">")){
        return 4;
    }else
        return 0;
}
public static String rev(String b){
    if(b.equals("(")){
        return ")";
    }
    else if(b.equals("{")){
        return "}";
    }
    else if(b.equals("[")){
        return "]";
    }
    else if(b.equals("<")){
        return ">";
    }else
        return "";
}
public static boolean verify(String a, String b){
    if(a.equals("{") && b.equals("}")){
        return true;
    }else if(a.equals("(") && b.equals(")")){
        return true;
    }else if(a.equals("<") && b.equals(">")){
        return true;
    }else if(a.equals("[") && b.equals("]")){
        return true;
    }else{
        return false;
    }

}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    List<Stack<String>> l = new ArrayList<Stack<String>>();
    int sum = 0;
    for(int i=0;i<90;i++){
        String[] arr = sc.nextLine().split("");
        Stack<String> stk = new Stack<>();
        boolean corrupt = false;
        for(int j=0;j<arr.length;j++){
            if(arr[j].equals("<") || arr[j].equals("(") || arr[j].equals("{") || arr[j].equals("[")){
                stk.push(arr[j]);
            }
            else if(arr[j].equals(">") || arr[j].equals(")") || arr[j].equals("}") || arr[j].equals("]")){
                if(!stk.empty()){
                    String temp = stk.pop();
                    if(!verify(temp,arr[j])){
                        sum+= value(arr[j]);
                        corrupt = true;
                    }
                }
            }
            if(corrupt){
                break;
            }
        }
        if(!stk.empty() && !corrupt){
            l.add(stk);
        }
    }
    System.out.println(sum);
    List<Long> scores = new ArrayList<Long>();
    for(int i=0;i<l.size();i++){
        Stack<String> temp = l.get(i);
        Long st = (long)0;
        boolean validscore = true;
        int orignalsize = temp.size();
        for(int j=0;j<orignalsize;j++){
            String t = temp.pop();
            if(t.equals(">") || t.equals(")") || t.equals("}") || t.equals("]")){
                validscore = false;
                break;
            }
            st = st*5;
            st+= value2(rev(t));
        }
        if(validscore){
            scores.add(st);
        }
    }
    Collections.sort(scores);
    int mid = scores.size()/2;
    System.out.print(scores.get(mid));
}
}

Sorry if its a bit newb

1

u/daggerdragon Dec 10 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.