File tree 8 files changed +358
-0
lines changed
8 files changed +358
-0
lines changed Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day10 {
9
+
10
+
11
+ public Day10 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day10 test = new Day10 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day11 {
9
+
10
+
11
+ public Day11 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day11 test = new Day11 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day12 {
9
+
10
+
11
+ public Day12 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day12 test = new Day12 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day13 {
9
+
10
+
11
+ public Day13 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day13 test = new Day13 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day14 {
9
+
10
+
11
+ public Day14 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day14 test = new Day14 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .Comparator ;
5
+ import java .util .List ;
6
+
7
+ import utils .StaticUtils ;
8
+
9
+ public class Day7 {
10
+
11
+ private List <Integer > nums ;
12
+
13
+ public Day7 (File file ) {
14
+ nums = StaticUtils .commaSeperatedIntegerFileToList (file );
15
+ nums .sort (Comparator .naturalOrder ());
16
+ }
17
+
18
+ private int run1 () {
19
+
20
+
21
+ int optimalDistance = 0 ;
22
+
23
+ // optimalDistance = median
24
+ int n = nums .size ();
25
+ if (nums .size () % 2 == 0 ) {
26
+ optimalDistance = nums .get ((n + 1 ) / 2 - 1 );
27
+ } else {
28
+ optimalDistance = (nums .get ((n + 1 ) / 2 - 1 ) + nums .get (n / 2 )) / 2 ;
29
+ }
30
+
31
+ final int finalOptimalDistance = optimalDistance ;
32
+ int fuel = nums .stream ().mapToInt (num -> Math .abs (num - finalOptimalDistance )).sum ();
33
+
34
+ return fuel ;
35
+ }
36
+
37
+ // brute force with gauss sum
38
+ private long run2 () {
39
+
40
+ int max = nums .get (nums .size () - 1 );
41
+
42
+ long fuel = 0 ;
43
+ long minFuel = Integer .MAX_VALUE ;
44
+ for (int i = 1 ; i < max ; i ++) {
45
+
46
+ for (int num : nums ) {
47
+ int distance = Math .abs (num - i );
48
+ fuel += (((int ) Math .pow (distance , 2 ) + distance )) / 2 ;
49
+ }
50
+
51
+ if (minFuel > fuel ) {
52
+ minFuel = fuel ;
53
+ fuel = 0 ;
54
+ }
55
+
56
+ }
57
+
58
+
59
+
60
+ return minFuel ;
61
+ }
62
+
63
+ public static void main (String [] args ) {
64
+ Day7 test = new Day7 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day7\\ InputFile1.txt" ));
65
+ System .out .println (test .run1 ());
66
+ System .out .println (test .run2 ());
67
+ }
68
+
69
+
70
+
71
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day8 {
9
+
10
+
11
+ public Day8 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day8 test = new Day8 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
Original file line number Diff line number Diff line change
1
+ package aoc21 ;
2
+
3
+ import java .io .File ;
4
+ import java .util .List ;
5
+
6
+ import utils .StaticUtils ;
7
+
8
+ public class Day9 {
9
+
10
+
11
+ public Day9 (File file ) {
12
+
13
+ }
14
+
15
+ private int run1 () {
16
+
17
+
18
+
19
+
20
+
21
+ return 0 ;
22
+ }
23
+
24
+ private int run2 () {
25
+
26
+
27
+
28
+
29
+
30
+ return 0 ;
31
+ }
32
+
33
+ public static void main (String [] args ) {
34
+ Day9 test = new Day9 (new File ("C:\\ Users\\ tmerdin\\ aoc\\ aoc21\\ day8\\ InputFile2.txt" ));
35
+ System .out .println (test .run1 ());
36
+ System .out .println (test .run2 ());
37
+ }
38
+
39
+
40
+
41
+ }
You can’t perform that action at this time.
0 commit comments