Advertisement
Guest User

AOC Day6 | Java

a guest
Dec 6th, 2023
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | Source Code | 0 0
  1. public class Day6Model {
  2.  
  3.     private long time;
  4.     private long distance;
  5.     private long numberOfRecordWins;
  6.     private long startWins;
  7.     private long endWins;
  8.  
  9.     public Day6Model(long time, long distance) {
  10.         this.time = time;
  11.         this.distance = distance;
  12.         this.numberOfRecordWins = -1;
  13.         this.startWins = -1;
  14.         this.endWins = -1;
  15.     }
  16.    
  17.     public long getNumberOfRecordWins() {
  18.         return numberOfRecordWins;
  19.     }
  20.  
  21.     public void calculateNumberOfRecordWins() {
  22.         numberOfRecordWins = 0L;
  23.         for (int i = 0; i <= time; i++) {
  24.             if (i * (time - i) > distance) {
  25.                 numberOfRecordWins++;
  26.                 endWins = i;
  27.             }
  28.             if (numberOfRecordWins == 1L) {
  29.                 startWins = i;
  30.             }
  31.         }
  32.     }
  33.  
  34.     public void calculateBigWins() {
  35.         long inner = (time * time) - (4 * distance);
  36.         double rootOfInner = Math.sqrt(inner);
  37.         double bound = Math.abs((time * -1 + rootOfInner) / -2);
  38.         numberOfRecordWins = (long) (time - (2 * bound)) + 1;
  39.         startWins = (time / 2) - (numberOfRecordWins / 2);
  40.         endWins = (time / 2) + (numberOfRecordWins / 2);
  41.     }
  42. }
  43.  
  44. public static List<Day6Model> parseDay6Part1File(File day6File) throws IOException {
  45.     List<Day6Model> parsedList = new ArrayList<>();
  46.     try (BufferedReader reader = new BufferedReader(new FileReader(day6File.getPath()))) {
  47.         String timeLine = reader.readLine();
  48.         String distanceLine = reader.readLine();
  49.         parseFile(timeLine, distanceLine, parsedList);
  50.     } catch (Exception ex) {
  51.         LOG.error(ex.getMessage());
  52.         throw new IOException("Invalid file provided");
  53.     }
  54.     return parsedList;
  55. }
  56.  
  57. public static Day6Model parseDay6Part2File(File day6File) throws IOException {
  58.     Day6Model day6Model;
  59.     try (BufferedReader reader = new BufferedReader(new FileReader(day6File.getPath()))) {
  60.         String timeLine = reader.readLine();
  61.         String distanceLine = reader.readLine();
  62.         String time = timeLine.split("\\s+", 2)[1].replace(" ", "");
  63.         String distance = distanceLine.split("\\s+", 2)[1].replace(" ", "");
  64.         day6Model = new Day6Model(Long.parseLong(time), Long.parseLong(distance));
  65.     } catch (Exception ex) {
  66.         LOG.error(ex.getMessage());
  67.         throw new IOException("Invalid file provided");
  68.     }
  69.     return day6Model;
  70. }
  71.  
  72. private static void parseFile(String timeLine, String distanceLine, List<Day6Model> parsedList) {
  73.     String[] timeSplit = timeLine.split("\\s+", 5);
  74.     String[] distanceSplit = distanceLine.split("\\s+", 5);
  75.     for (int i = 1; i < timeSplit.length; i++) {
  76.         long time = Long.parseLong(timeSplit[i].strip());
  77.         long distance = Long.parseLong(distanceSplit[i].strip());
  78.         parsedList.add(new Day6Model(time, distance));
  79.     }
  80. }
  81.  
  82. public static long day6Part1Solve(List<Day6Model> parsedFile) {
  83.     for (Day6Model day6Model : parsedFile) {
  84.         day6Model.calculateNumberOfRecordWins();
  85.     }
  86.     return parsedFile.stream()
  87.         .map(Day6Model::getNumberOfRecordWins)
  88.         .reduce(1L, (a, b) -> a * b);
  89. }
  90.  
  91.  
  92. public static long day6Part2Solve(Day6Model day6Model) {
  93.     day6Model.calculateBigWins();
  94.     return day6Model.getNumberOfRecordWins();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement