-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d32a16a
commit 7cf8384
Showing
34 changed files
with
2,021 additions
and
12 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day1.java → ...inallyclever/adventofcode/y2022/Day1.java
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
2 changes: 1 addition & 1 deletion
2
.../marginallyclever/adventofcode/Day10.java → ...nallyclever/adventofcode/y2022/Day10.java
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
2 changes: 1 addition & 1 deletion
2
.../marginallyclever/adventofcode/Day11.java → ...nallyclever/adventofcode/y2022/Day11.java
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
2 changes: 1 addition & 1 deletion
2
.../marginallyclever/adventofcode/Day12.java → ...nallyclever/adventofcode/y2022/Day12.java
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
2 changes: 1 addition & 1 deletion
2
.../marginallyclever/adventofcode/Day13.java → ...nallyclever/adventofcode/y2022/Day13.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day2.java → ...inallyclever/adventofcode/y2022/Day2.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day3.java → ...inallyclever/adventofcode/y2022/Day3.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day4.java → ...inallyclever/adventofcode/y2022/Day4.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day5.java → ...inallyclever/adventofcode/y2022/Day5.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day6.java → ...inallyclever/adventofcode/y2022/Day6.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day8.java → ...inallyclever/adventofcode/y2022/Day8.java
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
2 changes: 1 addition & 1 deletion
2
...m/marginallyclever/adventofcode/Day9.java → ...inallyclever/adventofcode/y2022/Day9.java
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
76 changes: 76 additions & 0 deletions
76
src/main/java/com/marginallyclever/adventofcode/y2023/Day1.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.marginallyclever.adventofcode.y2023; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class Day1 { | ||
|
||
public static void main(String[] args) { | ||
Day1 me = new Day1(); | ||
me.processFile("day1.txt"); | ||
} | ||
|
||
private void processFile(String filename) { | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(Day1.class.getResourceAsStream(filename))))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
processLine(line); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
int sum = 0; | ||
|
||
private void processLine(String line) { | ||
int value=0; | ||
// scan forward | ||
for(int i=0;i<line.length();++i) { | ||
Character c = line.charAt(i); | ||
if(Character.isDigit(c)) { | ||
value = Character.getNumericValue(c)*10; | ||
System.out.println(c+"="+value); | ||
break; | ||
} | ||
int j = matchDigitName(line.substring(i)); | ||
if(j!=-1) { | ||
value = j*10; | ||
System.out.println(line.substring(i)+"="+value); | ||
break; | ||
} | ||
} | ||
// scan backward | ||
for(int i=line.length()-1;i>=0;--i) { | ||
Character c = line.charAt(i); | ||
if(Character.isDigit(c)) { | ||
value += Character.getNumericValue(c); | ||
System.out.println(c+"="+value); | ||
break; | ||
} | ||
int j = matchDigitName(line.substring(i)); | ||
if(j!=-1) { | ||
value += j; | ||
System.out.println(line.substring(i)+"="+value); | ||
break; | ||
} | ||
} | ||
sum += value; | ||
System.out.println("sum="+sum); | ||
} | ||
|
||
private int matchDigitName(String substring) { | ||
if(substring.startsWith("one")) return 1; | ||
if(substring.startsWith("two")) return 2; | ||
if(substring.startsWith("three")) return 3; | ||
if(substring.startsWith("four")) return 4; | ||
if(substring.startsWith("five")) return 5; | ||
if(substring.startsWith("six")) return 6; | ||
if(substring.startsWith("seven")) return 7; | ||
if(substring.startsWith("eight")) return 8; | ||
if(substring.startsWith("nine")) return 9; | ||
return -1; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/marginallyclever/adventofcode/y2023/Day2.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.marginallyclever.adventofcode.y2023; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class Day2 { | ||
|
||
public static void main(String[] args) { | ||
Day2 me = new Day2(); | ||
me.processFile("day2.txt"); | ||
} | ||
|
||
private void processFile(String filename) { | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(Day2.class.getResourceAsStream(filename))))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
processLine(line); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
int sum = 0; | ||
final int maxR = 12; | ||
final int maxG = 13; | ||
final int maxB = 14; | ||
|
||
/** | ||
* line is in format | ||
* <pre>Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green | ||
* Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue</pre> | ||
*/ | ||
private void processLine(String line) { | ||
// read the header | ||
if(!line.startsWith("Game ")) { | ||
throw new RuntimeException("Expected line to start with 'Game ' but got '"+line+"'"); | ||
} | ||
line = line.substring(5); | ||
int colon = line.indexOf(":"); | ||
String s1 = line.substring(0,colon); | ||
int gameNumber = Integer.parseInt(s1); | ||
System.out.println("Game "+gameNumber); | ||
line = line.substring(colon+1); | ||
|
||
int r=0; | ||
int g=0; | ||
int b=0; | ||
|
||
// split by `;` | ||
String [] parts = line.split(";"); | ||
for(String part : parts) { | ||
// split part by `,` | ||
String [] colorSection = part.split(","); | ||
for(String section : colorSection) { | ||
section = section.trim(); | ||
// split section by ' ' | ||
String [] colorCount = section.split(" "); | ||
int count = Integer.parseInt(colorCount[0]); | ||
String colorName = colorCount[1]; | ||
|
||
switch(colorName) { | ||
case "red": r = count; break; | ||
case "green": g = count; break; | ||
case "blue": b = count; break; | ||
default: throw new RuntimeException("Unknown color '"+colorName+"'"); | ||
} | ||
} | ||
if(r>maxR) return; | ||
if(g>maxG) return; | ||
if(b>maxB) return; | ||
} | ||
System.out.println(" red="+r+" green="+g+" blue="+b); | ||
System.out.println("Game "+gameNumber+" is valid."); | ||
sum += gameNumber; | ||
System.out.println("sum is "+sum); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/marginallyclever/adventofcode/y2023/Day2b.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.marginallyclever.adventofcode.y2023; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class Day2b { | ||
|
||
public static void main(String[] args) { | ||
Day2b me = new Day2b(); | ||
me.processFile("day2.txt"); | ||
} | ||
|
||
private void processFile(String filename) { | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(Day2b.class.getResourceAsStream(filename))))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
processLine(line); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
int sum = 0; | ||
int minR = 0; | ||
int minG = 0; | ||
int minB = 0; | ||
|
||
/** | ||
* line is in format | ||
* <pre>Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green | ||
* Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue</pre> | ||
*/ | ||
private void processLine(String line) { | ||
// read the header | ||
if(!line.startsWith("Game ")) { | ||
throw new RuntimeException("Expected line to start with 'Game ' but got '"+line+"'"); | ||
} | ||
line = line.substring(5); | ||
int colon = line.indexOf(":"); | ||
String s1 = line.substring(0,colon); | ||
int gameNumber = Integer.parseInt(s1); | ||
System.out.println("Game "+gameNumber); | ||
line = line.substring(colon+1); | ||
|
||
minR = minG = minB = 0; | ||
int r=0; | ||
int g=0; | ||
int b=0; | ||
|
||
// split by `;` | ||
String [] parts = line.split(";"); | ||
for(String part : parts) { | ||
// split part by `,` | ||
String [] colorSection = part.split(","); | ||
for(String section : colorSection) { | ||
section = section.trim(); | ||
// split section by ' ' | ||
String [] colorCount = section.split(" "); | ||
int count = Integer.parseInt(colorCount[0]); | ||
String colorName = colorCount[1]; | ||
|
||
switch(colorName) { | ||
case "red": r = count; break; | ||
case "green": g = count; break; | ||
case "blue": b = count; break; | ||
default: throw new RuntimeException("Unknown color '"+colorName+"'"); | ||
} | ||
} | ||
if(r>minR) minR = r; | ||
if(g>minG) minG = g; | ||
if(b>minB) minB = b; | ||
} | ||
System.out.println(" red="+minR+" green="+minG+" blue="+minB); | ||
int power = minR*minG*minB; | ||
System.out.println("Game "+gameNumber+" power is "+power); | ||
sum += power; | ||
System.out.println("sum is "+sum); | ||
} | ||
} |
Oops, something went wrong.