Created
December 3, 2020 08:03
-
-
Save msx80/90e6cd9f6e8196a1e8699ff072c08d9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoc; | |
import java.awt.Point; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class Day3 { | |
static int part1(char[][] data, Point move, int width) { | |
int cnt = 0; | |
Point cur = new Point(0,0); | |
while (cur.y < data.length) { | |
if (data[cur.y][cur.x] == '#') { | |
cnt++; | |
} | |
cur.translate(move.x,move.y); | |
cur.x = cur.x % width; | |
} | |
return cnt; | |
} | |
public static void main(String[] args) throws IOException { | |
char[][] records = Files.readAllLines(Paths.get("input3.txt")) | |
.stream() | |
.map(s -> s.toCharArray()) | |
.collect(Collectors.toList()) | |
.toArray(char[][]::new); | |
int width = records[0].length; | |
int res1 = part1(records, new Point(3,1), width); | |
System.out.println(res1); | |
long res2 = Stream | |
.of(new Point(1,1), new Point(3,1), new Point(5,1),new Point(7,1),new Point(1,2)) | |
.mapToLong(slope -> part1(records, slope, width)) | |
.reduce((a,b) -> a*b) | |
.getAsLong(); | |
System.out.println(res2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment