Initial commit, day 1 and 2
commit
2b04ab0cbe
|
@ -0,0 +1,20 @@
|
|||
Copyright (c) 2021 earnest ma <me@earne.link>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
Be Gay, Do Crimes
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,3 @@
|
|||
# AOC 2021 personal solutions
|
||||
|
||||
It's my first year doing [Advent of Code](https://adventofcode.com) (and doing more coding), and I'm doing it in java :/ (because that's what I'm currently learning as part of the ICS4U course).
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,50 @@
|
|||
// In around 20 minutes
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class day01 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int hasIncreasedCounter = 0;
|
||||
int lineCounter = 1;
|
||||
|
||||
int firstnum = 0;
|
||||
int nextnum = 0;
|
||||
|
||||
String line; // convert to int or something
|
||||
|
||||
BufferedReader in = new BufferedReader(new FileReader("day01-input.txt"));
|
||||
|
||||
line = in.readLine();
|
||||
while (line != null) {
|
||||
|
||||
if (lineCounter == 1) { // first line, no firstnum, set
|
||||
firstnum = Integer.valueOf(line).intValue();
|
||||
System.out.println("First entry " + firstnum);
|
||||
}
|
||||
else { // continue, as usual
|
||||
nextnum = Integer.valueOf(line).intValue();
|
||||
|
||||
System.out.println("Comparing " + firstnum + " and " + nextnum);
|
||||
|
||||
if (nextnum > firstnum) {
|
||||
System.out.println("Yes!");
|
||||
hasIncreasedCounter++;
|
||||
}
|
||||
else {
|
||||
System.out.println("Not bigger");
|
||||
}
|
||||
|
||||
firstnum = nextnum; // swap to continue
|
||||
}
|
||||
|
||||
lineCounter++;
|
||||
// continue
|
||||
line = in.readLine();
|
||||
}
|
||||
in.close(); // bye
|
||||
|
||||
System.out.println("Final is " + hasIncreasedCounter);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class day01Part2 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int hasIncreasedCounter = 0;
|
||||
int lineCounter = 1;
|
||||
|
||||
int firstnum = 0;
|
||||
int secondnum = 0;
|
||||
int thirdnum = 0;
|
||||
|
||||
int oldsum = 0;
|
||||
int newsum = 0;
|
||||
|
||||
String line; // convert to int or something
|
||||
|
||||
BufferedReader in = new BufferedReader(new FileReader("day01-input.txt"));
|
||||
|
||||
line = in.readLine();
|
||||
while (line != null) {
|
||||
|
||||
if (lineCounter == 1) { // first lines in file
|
||||
firstnum = Integer.valueOf(line).intValue();
|
||||
}
|
||||
else if (lineCounter == 2) {
|
||||
secondnum = Integer.valueOf(line).intValue();
|
||||
}
|
||||
else if (lineCounter == 3) {
|
||||
thirdnum = Integer.valueOf(line).intValue();
|
||||
}
|
||||
else {
|
||||
oldsum = firstnum + secondnum + thirdnum;
|
||||
|
||||
// swapping done here
|
||||
firstnum = secondnum;
|
||||
secondnum = thirdnum;
|
||||
// read new line as thirdnum
|
||||
thirdnum = Integer.valueOf(line).intValue();
|
||||
|
||||
newsum = firstnum + secondnum + thirdnum;
|
||||
|
||||
System.out.println("Comparing " + oldsum + " and " + newsum);
|
||||
|
||||
if (newsum > oldsum) {
|
||||
System.out.println("Yes!");
|
||||
hasIncreasedCounter++;
|
||||
}
|
||||
else {
|
||||
System.out.println("Not bigger");
|
||||
}
|
||||
|
||||
// oldsum = newsum; // swap to continue
|
||||
}
|
||||
|
||||
lineCounter++;
|
||||
// continue
|
||||
line = in.readLine();
|
||||
}
|
||||
in.close(); // bye
|
||||
|
||||
System.out.println("Final is " + hasIncreasedCounter);
|
||||
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,48 @@
|
|||
// so how do we parse this file?
|
||||
// .split("regex here...")
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class day02 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int posHorizontal = 0;
|
||||
int posDepth = 0;
|
||||
int finalAns = 0; // posHorizontal * posDepth
|
||||
|
||||
String line;
|
||||
|
||||
String direction;
|
||||
int movement;
|
||||
|
||||
BufferedReader in = new BufferedReader(new FileReader("day02-input.txt"));
|
||||
line = in.readLine();
|
||||
while (line != null) {
|
||||
|
||||
String[] lineProcess = line.split(" ");
|
||||
|
||||
direction = lineProcess[0];
|
||||
movement = Integer.valueOf(lineProcess[1]).intValue();
|
||||
|
||||
if (direction.compareTo("forward") == 0) {
|
||||
posHorizontal += movement;
|
||||
|
||||
}
|
||||
else if (direction.compareTo("up") == 0) {
|
||||
posDepth -= movement;
|
||||
}
|
||||
else if (direction.compareTo("down") == 0) {
|
||||
posDepth += movement;
|
||||
}
|
||||
|
||||
line = in.readLine();
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
finalAns = posHorizontal * posDepth;
|
||||
System.out.println(finalAns);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class day02Part2 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int posHorizontal = 0;
|
||||
int posDepth = 0;
|
||||
int aim = 0;
|
||||
int finalAns = 0; // posHorizontal * posDepth
|
||||
|
||||
String line;
|
||||
|
||||
String direction;
|
||||
int movement;
|
||||
|
||||
BufferedReader in = new BufferedReader(new FileReader("day02-input.txt"));
|
||||
line = in.readLine();
|
||||
while (line != null) {
|
||||
|
||||
String[] lineProcess = line.split(" ");
|
||||
|
||||
direction = lineProcess[0];
|
||||
movement = Integer.valueOf(lineProcess[1]).intValue();
|
||||
|
||||
if (direction.compareTo("forward") == 0) {
|
||||
int temp = 0;
|
||||
|
||||
posHorizontal += movement;
|
||||
|
||||
temp = movement * aim;
|
||||
posDepth += temp;
|
||||
}
|
||||
else if (direction.compareTo("up") == 0) {
|
||||
// posDepth -= movement;
|
||||
aim -= movement;
|
||||
|
||||
}
|
||||
else if (direction.compareTo("down") == 0) {
|
||||
// posDepth += movement;
|
||||
aim += movement;
|
||||
}
|
||||
|
||||
line = in.readLine();
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
finalAns = posHorizontal * posDepth;
|
||||
System.out.println(finalAns);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue