/ Programming

Advent of Code 2017 - Day 2

There's a programming Advent calendar that I found on Hacker News called Advent of Code that offers interesting code puzzles. I'm going to re-post the questions on here with my answers. Try to solve them yourself first! This second one I solved using Java (trying to mix it up a bit).

Here's the Challenge

Day 2: Corruption Checksum

As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!"

The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.

For example, given the following spreadsheet:

5 1 9 5
7 5 3
2 4 6 8

The first row's largest and smallest values are 9 and 1, and their difference is 8.
The second row's largest and smallest values are 7 and 3, and their difference is 4.
The third row's difference is 6.
In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.

What is the checksum for the spreadsheet in your puzzle input? Here's the input:

62	1649	1731	76	51	1295	349	719	52	1984	2015	2171	981	1809	181	1715
161	99	1506	1658	84	78	533	242	1685	86	107	1548	670	960	1641	610
95	2420	2404	2293	542	2107	2198	121	109	209	2759	1373	1446	905	1837	111
552	186	751	527	696	164	114	530	558	307	252	200	481	142	205	479
581	1344	994	1413	120	112	656	1315	1249	193	1411	1280	110	103	74	1007
2536	5252	159	179	4701	1264	1400	2313	4237	161	142	4336	1061	3987	2268	4669
3270	1026	381	185	293	3520	1705	1610	3302	628	3420	524	3172	244	295	39
4142	1835	4137	3821	3730	2094	468	141	150	3982	147	4271	1741	2039	4410	179
1796	83	2039	1252	84	1641	2165	1218	1936	335	1807	2268	66	102	1977	2445
96	65	201	275	257	282	233	60	57	200	216	134	72	105	81	212
3218	5576	5616	5253	178	3317	6147	5973	2424	274	4878	234	200	4781	5372	276
4171	2436	134	3705	3831	3952	2603	115	660	125	610	152	4517	587	1554	619
2970	128	2877	1565	1001	167	254	2672	59	473	2086	181	1305	162	1663	2918
271	348	229	278	981	1785	2290	516	473	2037	737	2291	2521	1494	1121	244
2208	2236	1451	621	1937	1952	865	61	1934	49	1510	50	1767	59	194	1344
94	2312	2397	333	1192	106	2713	2351	2650	2663	703	157	89	510	1824	125

Here's the code I used (it's not super efficient and has no error checking... I wrote it before dinner so that's my only defense):

import java.util.*;
import java.util.stream.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
import java.lang.*;

class Program {
  public static void main(String[] args) {
    int result = Files.readAllLines(Paths.get("data.txt"), StandardCharsets.UTF_8)
      .parallelStream()
      .mapToInt(s -> {
        int min = Arrays.asList(s.split("\\s+"))
          .parallelStream()
          .mapToInt(Integer::parseInt)
          .min().getAsInt();

        int max = Arrays.asList(s.split("\\s+"))
          .parallelStream()
          .mapToInt(Integer::parseInt)
          .max().getAsInt();

        return max - min;
      })
      .sum();

    System.out.println(result);
  }
}

Your puzzle answer was: 44216

Part Two

"Great work; looks like we're on the right track after all. Here's a star for your effort." However, the program seems a little worried. Can programs be worried?

"Based on what we're seeing, it looks like all the User wanted is some information about the evenly divisible values in the spreadsheet. Unfortunately, none of us are equipped for that kind of calculation - most of us specialize in bitwise operations."

It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.

For example, given the following spreadsheet:

5 9 2 8
9 4 7 3
3 8 6 5

In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.
In the second row, the two numbers are 9 and 3; the result is 3.
In the third row, the result is 2.
In this example, the sum of the results would be 4 + 3 + 2 = 9.

What is the sum of each row's result in your puzzle input? The input is the same as before.

Here's the code:

import java.util.*;
import java.util.stream.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
import java.lang.*;

class Program {
  public static void main(String[] args) throws IOException {
    int result = Files.readAllLines(Paths.get("data.txt"), StandardCharsets.UTF_8)
      .parallelStream()
      .mapToInt(s -> {
        Integer[] nums = Arrays.asList(s.split("\\s+"))
          .parallelStream()
          .mapToInt(Integer::parseInt)
          .boxed()
          .collect(Collectors.toList())
          .toArray(new Integer[] {});

        for(int i = 0; i < nums.length; i++) {
          for(int j = 0; j < nums.length; j++) {
            if(nums[i] >= nums[j] && nums[i] % nums[j] == 0 && i != j) {
              return nums[i] / nums[j];
            }
            else if(nums[j] >= nums[i] && nums[j] % nums[i] == 0 && i != j) {
              return nums[j] / nums[i];
            }
          }
        }

        return 0;
      })
      .sum();

    System.out.println(result);
  }
}

Your puzzle answer was: 320