Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Day6Model {
- private long time;
- private long distance;
- private long numberOfRecordWins;
- private long startWins;
- private long endWins;
- public Day6Model(long time, long distance) {
- this.time = time;
- this.distance = distance;
- this.numberOfRecordWins = -1;
- this.startWins = -1;
- this.endWins = -1;
- }
- public long getNumberOfRecordWins() {
- return numberOfRecordWins;
- }
- public void calculateNumberOfRecordWins() {
- numberOfRecordWins = 0L;
- for (int i = 0; i <= time; i++) {
- if (i * (time - i) > distance) {
- numberOfRecordWins++;
- endWins = i;
- }
- if (numberOfRecordWins == 1L) {
- startWins = i;
- }
- }
- }
- public void calculateBigWins() {
- long inner = (time * time) - (4 * distance);
- double rootOfInner = Math.sqrt(inner);
- double bound = Math.abs((time * -1 + rootOfInner) / -2);
- numberOfRecordWins = (long) (time - (2 * bound)) + 1;
- startWins = (time / 2) - (numberOfRecordWins / 2);
- endWins = (time / 2) + (numberOfRecordWins / 2);
- }
- }
- public static List<Day6Model> parseDay6Part1File(File day6File) throws IOException {
- List<Day6Model> parsedList = new ArrayList<>();
- try (BufferedReader reader = new BufferedReader(new FileReader(day6File.getPath()))) {
- String timeLine = reader.readLine();
- String distanceLine = reader.readLine();
- parseFile(timeLine, distanceLine, parsedList);
- } catch (Exception ex) {
- LOG.error(ex.getMessage());
- throw new IOException("Invalid file provided");
- }
- return parsedList;
- }
- public static Day6Model parseDay6Part2File(File day6File) throws IOException {
- Day6Model day6Model;
- try (BufferedReader reader = new BufferedReader(new FileReader(day6File.getPath()))) {
- String timeLine = reader.readLine();
- String distanceLine = reader.readLine();
- String time = timeLine.split("\\s+", 2)[1].replace(" ", "");
- String distance = distanceLine.split("\\s+", 2)[1].replace(" ", "");
- day6Model = new Day6Model(Long.parseLong(time), Long.parseLong(distance));
- } catch (Exception ex) {
- LOG.error(ex.getMessage());
- throw new IOException("Invalid file provided");
- }
- return day6Model;
- }
- private static void parseFile(String timeLine, String distanceLine, List<Day6Model> parsedList) {
- String[] timeSplit = timeLine.split("\\s+", 5);
- String[] distanceSplit = distanceLine.split("\\s+", 5);
- for (int i = 1; i < timeSplit.length; i++) {
- long time = Long.parseLong(timeSplit[i].strip());
- long distance = Long.parseLong(distanceSplit[i].strip());
- parsedList.add(new Day6Model(time, distance));
- }
- }
- public static long day6Part1Solve(List<Day6Model> parsedFile) {
- for (Day6Model day6Model : parsedFile) {
- day6Model.calculateNumberOfRecordWins();
- }
- return parsedFile.stream()
- .map(Day6Model::getNumberOfRecordWins)
- .reduce(1L, (a, b) -> a * b);
- }
- public static long day6Part2Solve(Day6Model day6Model) {
- day6Model.calculateBigWins();
- return day6Model.getNumberOfRecordWins();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement