Skip to content

Commit

Permalink
advent of code 2023, days 1-4
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 4, 2023
1 parent d32a16a commit 7cf8384
Show file tree
Hide file tree
Showing 34 changed files with 2,021 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.*;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.marginallyclever.adventofcode;
package com.marginallyclever.adventofcode.y2022;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/marginallyclever/adventofcode/y2023/Day1.java
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 src/main/java/com/marginallyclever/adventofcode/y2023/Day2.java
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 src/main/java/com/marginallyclever/adventofcode/y2023/Day2b.java
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);
}
}
Loading

0 comments on commit 7cf8384

Please sign in to comment.