Skip to content

Commit 4a21925

Browse files
authored
Merge pull request devcordde#2 from devcordde/master
Updating fork
2 parents 5c5bb69 + 1884c38 commit 4a21925

File tree

51 files changed

+11480
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+11480
-50
lines changed

Day-01/c/JohnnyJayJay/Day01.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/* compile from project root using
2-
cc -Wall -g shared/JohnnyJayJay/aoc.c Day-01/c/JohnnyJayJay/Day01.c -o day01 */
1+
// Read my README in shared/JohnnyJayJay
32

43
#include <stdio.h>
54
#include <stdlib.h>

Day-01/java/MCqwertz/Day1.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
//created by MCqwertz
22

3-
4-
package com.github.mcqwertz.year2020.days;
5-
6-
import com.github.mcqwertz.util.TextFileUtils;
7-
83
import java.io.FileNotFoundException;
4+
import java.util.ArrayList;
5+
import java.util.Scanner;
96
import java.util.function.IntPredicate;
107
import java.util.function.Supplier;
118
import java.util.stream.IntStream;
129

1310

1411
public class Day1 {
1512
public static void main(String[] args) throws FileNotFoundException {
16-
Day1 day = new Day1();
17-
Supplier<IntStream> supplier = TextFileUtils.getNumbers(1);
18-
System.out.println("Task 1: " + day.getFirstPart(supplier));
19-
System.out.println("Task 2: " + day.getSecondPart(supplier));
13+
Supplier<IntStream> supplier = getInput();
14+
System.out.println("Task 1: " + getFirstPart(supplier));
15+
System.out.println("Task 2: " + getSecondPart(supplier));
2016
}
2117

2218
/**
2319
* @param supplier supplier of the stream with all given numbers
2420
* @return the solution of the first problem
2521
*/
26-
private int getFirstPart(Supplier<IntStream> supplier) {
27-
IntPredicate isSearched = arg -> this.isContaining(supplier.get(), 2020 - arg);
22+
private static int getFirstPart(Supplier<IntStream> supplier) {
23+
IntPredicate isSearched = arg -> isContaining(supplier.get(), 2020 - arg);
2824
int result = 1;
2925
//check for each int whether there is suitable second int
3026
for (int i : supplier.get().filter(isSearched).toArray()) {
@@ -36,7 +32,7 @@ private int getFirstPart(Supplier<IntStream> supplier) {
3632
* @param supplier supplier of the stream with all given numbers
3733
* @return the solution of the second problem
3834
*/
39-
private int getSecondPart(Supplier<IntStream> supplier) {
35+
private static int getSecondPart(Supplier<IntStream> supplier) {
4036
int[] array = supplier.get().toArray();
4137
//combine each number with every other number
4238
for (int i : array) {
@@ -50,12 +46,22 @@ private int getSecondPart(Supplier<IntStream> supplier) {
5046
return -1;
5147
}
5248

53-
private boolean isContaining(IntStream stream, int x) {
49+
private static boolean isContaining(IntStream stream, int x) {
5450
for (int i : stream.toArray()) {
5551
if(i == x) {
5652
return true;
5753
}
5854
}
5955
return false;
6056
}
57+
58+
public static Supplier<IntStream> getInput() throws FileNotFoundException {
59+
Scanner scanner = TextFileUtils.getScanner(1);
60+
ArrayList<String> arrayList = new ArrayList<>();
61+
while(scanner.hasNextLine()) {
62+
arrayList.add(scanner.nextLine());
63+
}
64+
scanner.close();
65+
return () -> arrayList.stream().mapToInt(Integer::parseInt);
66+
}
6167
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from file_reader import read_numbers
2+
3+
numbers = read_numbers('day01.txt')
4+
5+
def get_first_numbers():
6+
max_index = len(numbers) - 1
7+
8+
for i in range(max_index):
9+
curr = numbers[i]
10+
11+
for j in range(i + 1, max_index):
12+
next = numbers[j]
13+
if (curr + next) == 2020:
14+
return curr, next
15+
16+
def get_second_numbers():
17+
max_index = len(numbers) - 2
18+
19+
for i in range(max_index):
20+
first = numbers[i]
21+
22+
for j in range(i + 1, max_index):
23+
second = numbers[j]
24+
25+
for k in range(i + 2, max_index):
26+
third = numbers[k]
27+
28+
if (first + second + third) == 2020:
29+
return first, second, third
30+
31+
# Part one
32+
nums = get_first_numbers()
33+
first = nums[0]
34+
second = nums[1]
35+
36+
print(f'{first} + {second} = {first + second}')
37+
print(f'{first} x {second} = {first * second}')
38+
39+
# Part two
40+
nums = get_second_numbers()
41+
first = nums[0]
42+
second = nums[1]
43+
third = nums[2]
44+
45+
print(f'{first} + {second} + {third} = {first + second + third}')
46+
print(f'{first} x {second} x {third} = {first * second * third}')

Day-02/c/JohnnyJayJay/Day02.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
/* compile from project root using
2-
cc -Wall -g shared/JohnnyJayJay/aoc.c Day-02/c/JohnnyJayJay/Day02.c -o day02 */
1+
// Read my README in shared/JohnnyJayJay
32

43
#include <stdio.h>
54
#include <stdlib.h>
65
#include <string.h>
76
#include "../../../shared/JohnnyJayJay/aoc.h"
87

9-
struct db_entry {
8+
typedef struct {
109
int min;
1110
int max;
1211
char c;
1312
char* password;
14-
};
13+
} db_entry;
1514

1615

1716
int main(int argc, char** argv) {
1817
FILE* file = fopen(argv[1], "r");
1918
int lines = count_lines(file);
2019

21-
struct db_entry entries[lines];
20+
db_entry entries[lines];
2221
for (int i = 0; i < lines; i++) {
2322
int min, max;
2423
char c;
25-
char* password = malloc(chars_until(file, '\n') + 1);
24+
char* password = malloc(chars_until(file, '\n', 1) + 1);
2625
fscanf(file, "%d-%d %c: %s", &min, &max, &c, password);
27-
struct db_entry entry = {.min = min, .max = max, .c = c, .password = password};
26+
db_entry entry = {.min = min, .max = max, .c = c, .password = password};
2827
entries[i] = entry;
2928
}
3029

3130
int valid_passwords1 = 0;
3231
int valid_passwords2 = 0;
3332
for (int i = 0; i < lines; i++) {
34-
struct db_entry entry = entries[i];
35-
char* pw = entry.password;
36-
char c = entry.c;
37-
int min = entry.min;
38-
int max = entry.max;
33+
db_entry* entry = &entries[i];
34+
char* pw = entry->password;
35+
char c = entry->c;
36+
int min = entry->min;
37+
int max = entry->max;
3938
int count = charcount(pw, c, 0, strlen(pw));
4039
if (count >= min && count <= max) {
4140
valid_passwords1++;

Day-02/java/MCqwertz/Day2.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//created by MCqwertz
2+
3+
import java.io.FileNotFoundException;
4+
import java.util.ArrayList;
5+
import java.util.Scanner;
6+
import java.util.function.IntPredicate;
7+
8+
public class Day2 {
9+
public static void main(String[] args) throws FileNotFoundException {
10+
String[][] input = getInput();
11+
System.out.println("Part 1: " + getFirstPart(input));
12+
System.out.println("Part 2: " + getSecondPart(input));
13+
}
14+
15+
private static int getFirstPart(String[][] input) {
16+
int i = 0;
17+
for (String[] strings : input) {
18+
IntPredicate isSearched = arg -> arg == strings[2].charAt(0);
19+
int int0 = Integer.parseInt(strings[0]);
20+
int int1 = Integer.parseInt(strings[1]);
21+
int appearances = strings[3].chars().filter(isSearched).toArray().length;
22+
if(appearances <= int1 && appearances >= int0) {
23+
i++;
24+
}
25+
}
26+
return i;
27+
}
28+
29+
private static int getSecondPart(String[][] input) {
30+
int i = 0;
31+
for (String[] strings : input) {
32+
int int0 = Integer.parseInt(strings[0]) - 1;
33+
int int1 = Integer.parseInt(strings[1]) - 1;
34+
char c = strings[2].charAt(0);
35+
if ((strings[3].charAt(int0) == c && strings[3].charAt(int1) != c) ||
36+
(strings[3].charAt(int0) != c && strings[3].charAt(int1) == c)) {
37+
i++;
38+
}
39+
}
40+
return i;
41+
}
42+
43+
public static String[][] getInput() throws FileNotFoundException {
44+
Scanner scanner = TextFileUtils.getScanner(2);
45+
ArrayList<String> arrayList = new ArrayList<>();
46+
while(scanner.hasNextLine()) {
47+
arrayList.add(scanner.nextLine());
48+
}
49+
String[][] input = new String[arrayList.size()][4];
50+
for (int i = 0; i < arrayList.size(); i++) {
51+
String [] strings = arrayList.get(i).split("-")[1].replace(":", " ").split(" ");
52+
input[i] = new String[]{arrayList.get(i).split("-")[0], strings[0], strings[1], strings[3]};
53+
}
54+
scanner.close();
55+
return input;
56+
}
57+
58+
}
59+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from file_reader import read_lines
2+
import re
3+
4+
class Password:
5+
def __init__(self, letter, first_number, second_number, plain):
6+
self.letter = letter
7+
self.first_number = first_number
8+
self.second_number = second_number
9+
self.plain = plain
10+
11+
def read_password_from_string(line):
12+
split = line.split(' ')
13+
amount = split[0].split('-')
14+
15+
first_number = int(amount[0])
16+
second_number = int(amount[1])
17+
18+
letter = split[1][0]
19+
password = split[2]
20+
21+
return Password(letter, first_number, second_number, password)
22+
23+
# Part one
24+
def is_password_valid(password):
25+
letters = re.sub(f'[^{password.letter}]', '', password.plain)
26+
length = len(letters)
27+
return length >= password.first_number and length <= password.second_number
28+
29+
available_passwords = []
30+
for line in read_lines('day02.txt'):
31+
password = read_password_from_string(line)
32+
available_passwords.append(password)
33+
34+
valid_passwords = []
35+
36+
for password in available_passwords:
37+
if is_password_valid(password):
38+
valid_passwords.append(password)
39+
40+
print(len(valid_passwords))
41+
42+
# Part two
43+
def is_password_valid(password):
44+
first_letter = password.plain[password.first_number - 1]
45+
second_letter = password.plain[password.second_number - 1]
46+
47+
return (first_letter == password.letter and not second_letter == password.letter
48+
or not first_letter == password.letter and second_letter == password.letter)
49+
50+
valid_passwords = []
51+
52+
for password in available_passwords:
53+
if is_password_valid(password):
54+
valid_passwords.append(password)
55+
56+
print(len(valid_passwords))

Day-02/python/paul2708/solution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
valid_passwords += 1
2424

2525
aoc_print(f"{valid_passwords} passwords are valid. (old interpretation)")
26+
assert_equals(424, valid_passwords)
2627

2728
# Part two
2829
valid_passwords = 0
@@ -42,3 +43,4 @@
4243
valid_passwords += 1
4344

4445
aoc_print(f"{valid_passwords} passwords are valid. (new interpretation)")
46+
assert_equals(747, valid_passwords)

Day-03/c/JohnnyJayJay/Day03.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ int traverse(u_int32_t* levels, int lines, int width, int x_step, int y_step) {
2020
int main(int argc, char** argv) {
2121
FILE* file = fopen(argv[1], "r");
2222
int lines = count_lines(file);
23-
int width = chars_until(file, '\n');
23+
int width = chars_until(file, '\n', 1);
2424
u_int32_t* levels = malloc(sizeof(u_int32_t) * lines);
2525
for (int i = 0; i < lines; i++) {
2626
u_int32_t level = 0;
27-
char spot;
27+
int spot;
2828
while ((spot = fgetc(file)) != '\n' && spot != EOF) {
2929
level <<= 1;
3030
level |= spot == '#';

Day-03/java/MCqwertz/Day3.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//created by MCqwertz
2+
3+
import java.io.FileNotFoundException;
4+
import java.util.ArrayList;
5+
import java.util.Scanner;
6+
7+
public class Day3 {
8+
public static void main(String[] args) throws FileNotFoundException {
9+
char[][] grid = getInput();
10+
System.out.println("Part 1: " + getTrees(grid, 3, 1));
11+
System.out.println("Part 2: " + getSecondResult(grid));
12+
}
13+
14+
15+
private static long getSecondResult(char[][] grid) {
16+
return getTrees(grid, 1, 1) * getTrees(grid, 3, 1) * getTrees(grid, 5, 1) *
17+
getTrees(grid, 7, 1) * getTrees(grid, 1, 2);
18+
}
19+
20+
private static long getTrees(char[][] grid, int right, int down) {
21+
long trees = 0;
22+
int coulomb = 0;
23+
for (int i = down; i < grid.length; i += down) {
24+
coulomb = coulomb + right;
25+
if (grid[i][coulomb % grid[i].length] == '#') {
26+
trees++;
27+
}
28+
}
29+
return trees;
30+
}
31+
32+
public static char[][] getInput() throws FileNotFoundException {
33+
Scanner scanner = TextFileUtils.getScanner(3);
34+
ArrayList<String> arrayList = new ArrayList<>();
35+
while(scanner.hasNextLine()) {
36+
arrayList.add(scanner.nextLine());
37+
}
38+
int coulombs = arrayList.get(0).toCharArray().length;
39+
char[][] grid = new char[arrayList.size()][coulombs];
40+
int j = 0;
41+
for (int i = 0; i < arrayList.size(); i++){
42+
for (char c : arrayList.get(i).toCharArray()) {
43+
grid[i][j++] = c;
44+
}
45+
j = 0;
46+
}
47+
scanner.close();
48+
return grid;
49+
}
50+
}

0 commit comments

Comments
 (0)