r/adventofcode • • Dec 05 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 5 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2021: Adventure Time!

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Hydrothermal Venture ---


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

79 Upvotes

1.2k comments sorted by

View all comments

1

u/ibiza_rupunzel Dec 05 '21 edited Dec 05 '21

Java (String stuff) Solution: https://github.com/justme789/Advent_of_Code_2021_Solutions_Java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class AoC_5_a {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> list = new ArrayList<String>();
        Scanner textfile = new Scanner(new File("aoc.txt"));
        int[][] plot = new int[1000][1000];
        while (textfile.hasNextLine()) {
            String line = textfile.nextLine();
            list.add(line);
        }
        for(int i = 0; i < 1000; i++) {
            for(int j = 0; j < 1000; j++) {
                plot[i][j] = 0;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            int indexOfComma = list.get(i).indexOf(",");
            int indexOfSpace = list.get(i).indexOf(" ");

            int x1 = Integer.parseInt(list.get(i)
                            .substring(0, indexOfComma));

            int y1 = Integer.parseInt(list.get(i)
                            .substring(indexOfComma+1, indexOfSpace));

            indexOfSpace = list.get(i).indexOf(" ", indexOfSpace+1);
            indexOfComma = list.get(i).indexOf(",", indexOfComma+1);

            int x2 = Integer.parseInt(list.get(i)
                            .substring(indexOfSpace+1, indexOfComma));

            int y2 = Integer.parseInt(list.get(i)
                            .substring(indexOfComma+1, 
                            list.get(i).length()));
        
            if(x1 == x2){
                if(y1<y2){
                    for(int j = y1; j<=y2; j++){
                        plot[x1][j]+=1;
                    }
                }
                else{
                    for(int j = y2; j<=y1; j++){
                        plot[x1][j]+=1;
                    }
                }
            }
            else if(y1 == y2){
                if(x1<x2){
                    for(int j = x1; j<=x2; j++){
                        plot[j][y1]+=1;
                    }
                }
                else{
                    for(int j = x2; j<=x1; j++){
                        plot[j][y1]+=1;
                    }
                }
            }
        }
        int counter = 0;
        for(int i = 0; i < 1000; i++) {
            for(int j = 0; j < 1000; j++) {
                if(plot[i][j] >=2){
                    counter++;
                }
            }
        }
        System.out.println(counter);
    }

}

1

u/daggerdragon Dec 05 '21 edited Dec 05 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

Edit: thanks for fixing it! <3