Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 3d6ba1c

Browse files
authored
Merge pull request #1 from LovinLifee/master
day 1 code
2 parents b0d2d12 + 79e658f commit 3d6ba1c

File tree

6 files changed

+396
-0
lines changed

6 files changed

+396
-0
lines changed

.gitignore

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
2+
# Created by https://www.gitignore.io/api/java,eclipse,intellij
3+
# Edit at https://www.gitignore.io/?templates=java,eclipse,intellij
4+
5+
### Eclipse ###
6+
.metadata
7+
bin/
8+
tmp/
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*~.nib
13+
local.properties
14+
.settings/
15+
.loadpath
16+
.recommenders
17+
18+
# External tool builders
19+
.externalToolBuilders/
20+
21+
# Locally stored "Eclipse launch configurations"
22+
*.launch
23+
24+
# PyDev specific (Python IDE for Eclipse)
25+
*.pydevproject
26+
27+
# CDT-specific (C/C++ Development Tooling)
28+
.cproject
29+
30+
# CDT- autotools
31+
.autotools
32+
33+
# Java annotation processor (APT)
34+
.factorypath
35+
36+
# PDT-specific (PHP Development Tools)
37+
.buildpath
38+
39+
# sbteclipse plugin
40+
.target
41+
42+
# Tern plugin
43+
.tern-project
44+
45+
# TeXlipse plugin
46+
.texlipse
47+
48+
# STS (Spring Tool Suite)
49+
.springBeans
50+
51+
# Code Recommenders
52+
.recommenders/
53+
54+
# Annotation Processing
55+
.apt_generated/
56+
57+
# Scala IDE specific (Scala & Java development for Eclipse)
58+
.cache-main
59+
.scala_dependencies
60+
.worksheet
61+
62+
### Eclipse Patch ###
63+
# Eclipse Core
64+
.project
65+
66+
# JDT-specific (Eclipse Java Development Tools)
67+
.classpath
68+
69+
# Annotation Processing
70+
.apt_generated
71+
72+
.sts4-cache/
73+
74+
### Intellij ###
75+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
76+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
77+
78+
# User-specific stuff
79+
.idea/**/workspace.xml
80+
.idea/**/tasks.xml
81+
.idea/**/usage.statistics.xml
82+
.idea/**/dictionaries
83+
.idea/**/shelf
84+
85+
# Generated files
86+
.idea/**/contentModel.xml
87+
88+
# Sensitive or high-churn files
89+
.idea/**/dataSources/
90+
.idea/**/dataSources.ids
91+
.idea/**/dataSources.local.xml
92+
.idea/**/sqlDataSources.xml
93+
.idea/**/dynamic.xml
94+
.idea/**/uiDesigner.xml
95+
.idea/**/dbnavigator.xml
96+
97+
# Gradle
98+
.idea/**/gradle.xml
99+
.idea/**/libraries
100+
101+
# Gradle and Maven with auto-import
102+
# When using Gradle or Maven with auto-import, you should exclude module files,
103+
# since they will be recreated, and may cause churn. Uncomment if using
104+
# auto-import.
105+
# .idea/modules.xml
106+
# .idea/*.iml
107+
# .idea/modules
108+
# *.iml
109+
# *.ipr
110+
111+
# CMake
112+
cmake-build-*/
113+
114+
# Mongo Explorer plugin
115+
.idea/**/mongoSettings.xml
116+
117+
# File-based project format
118+
*.iws
119+
120+
# IntelliJ
121+
out/
122+
123+
# mpeltonen/sbt-idea plugin
124+
.idea_modules/
125+
126+
# JIRA plugin
127+
atlassian-ide-plugin.xml
128+
129+
# Cursive Clojure plugin
130+
.idea/replstate.xml
131+
132+
# Crashlytics plugin (for Android Studio and IntelliJ)
133+
com_crashlytics_export_strings.xml
134+
crashlytics.properties
135+
crashlytics-build.properties
136+
fabric.properties
137+
138+
# Editor-based Rest Client
139+
.idea/httpRequests
140+
141+
# Android studio 3.1+ serialized cache file
142+
.idea/caches/build_file_checksums.ser
143+
144+
### Intellij Patch ###
145+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
146+
147+
# *.iml
148+
# modules.xml
149+
# .idea/misc.xml
150+
# *.ipr
151+
152+
# Sonarlint plugin
153+
.idea/**/sonarlint/
154+
155+
# SonarQube Plugin
156+
.idea/**/sonarIssues.xml
157+
158+
# Markdown Navigator plugin
159+
.idea/**/markdown-navigator.xml
160+
.idea/**/markdown-navigator/
161+
162+
### Java ###
163+
# Compiled class file
164+
*.class
165+
166+
# Log file
167+
*.log
168+
169+
# BlueJ files
170+
*.ctxt
171+
172+
# Mobile Tools for Java (J2ME)
173+
.mtj.tmp/
174+
175+
# Package Files #
176+
*.jar
177+
*.war
178+
*.nar
179+
*.ear
180+
*.zip
181+
*.tar.gz
182+
*.rar
183+
184+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
185+
hs_err_pid*
186+
187+
# End of https://www.gitignore.io/api/java,eclipse,intellij

resources/Day1Inputs.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
143845
2+
86139
3+
53043
4+
124340
5+
73213
6+
108435
7+
126874
8+
131397
9+
85618
10+
107774
11+
66872
12+
94293
13+
51015
14+
51903
15+
147655
16+
112891
17+
100993
18+
143374
19+
83737
20+
145868
21+
144768
22+
89793
23+
124127
24+
135366
25+
94017
26+
81678
27+
102325
28+
75394
29+
103852
30+
81896
31+
148050
32+
142780
33+
50503
34+
110691
35+
117851
36+
137382
37+
92841
38+
138222
39+
128414
40+
146834
41+
59968
42+
136456
43+
122397
44+
147157
45+
83595
46+
59916
47+
75690
48+
125025
49+
147797
50+
112494
51+
76247
52+
100221
53+
63389
54+
59070
55+
97466
56+
91905
57+
126234
58+
76561
59+
128170
60+
102778
61+
82342
62+
131097
63+
51609
64+
148204
65+
74812
66+
64925
67+
127927
68+
79056
69+
73307
70+
78431
71+
88770
72+
97688
73+
103564
74+
76001
75+
105232
76+
145361
77+
77845
78+
87518
79+
117293
80+
110054
81+
135599
82+
85005
83+
85983
84+
118255
85+
103031
86+
142440
87+
140505
88+
99614
89+
69593
90+
69161
91+
78795
92+
54808
93+
115582
94+
117976
95+
148858
96+
84193
97+
147285
98+
89038
99+
92677
100+
106574

resources/Day2Inputs.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.togetherjava.adventofcode;
2+
3+
import java.util.function.Supplier;
4+
import java.util.stream.Stream;
5+
6+
import com.togetherjava.adventofcode.util.ResourceLoader;
7+
8+
public class Day1 {
9+
10+
public static void main(String[] args) {
11+
Supplier<Stream<Double>> input = () -> ResourceLoader.getInput("Day1Inputs.txt").stream().map(Double::parseDouble);
12+
double part1 = input.get().map(Day1::calculateFuel).reduce(0D, Double::sum);
13+
double part2 = input.get().map(Day1::recursiveCalculateFuel).reduce(0D, Double::sum);
14+
System.out.println("Part 1 answer: " + part1);
15+
System.out.println("Part 2 answer: " + part2);
16+
}
17+
18+
public static double recursiveCalculateFuel(double mass) {
19+
double fuel = calculateFuel(mass);
20+
if (fuel <= 0) {
21+
return 0D;
22+
}
23+
return fuel + recursiveCalculateFuel(fuel);
24+
}
25+
26+
public static double calculateFuel(double mass) {
27+
return Math.floor(mass / 3.0) - 2;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.togetherjava.adventofcode;
2+
3+
import java.util.Arrays;
4+
5+
import com.togetherjava.adventofcode.util.ResourceLoader;
6+
7+
public class Day2 {
8+
9+
public static void main(String[] args) {
10+
String[] input = ResourceLoader.getInput("Day2Inputs.txt").get(0).split(",");
11+
int[] data = new int[input.length];
12+
for (int i = 0; i < data.length; i++) {
13+
data[i] = Integer.parseInt(input[i]);
14+
}
15+
System.out.println("Part 1)");
16+
int[] part1 = Arrays.copyOf(data, data.length);
17+
//part 1 noun = 2 verb = 12
18+
System.out.println(calculate(part1, 12, 2)[0]);
19+
for(int verb = 0; verb < 100; verb++) {
20+
for(int noun = 0; noun < 100; noun++) {
21+
int[] part2 = Arrays.copyOf(data, data.length);
22+
int[] temp = calculate(part2, noun, verb);
23+
if(temp[0] == 19690720) {
24+
System.out.println("Part 2)");
25+
System.out.printf("FOUND NOUN = %d VERB = %d\n", noun, verb);
26+
System.out.printf("100 * %d + %d = %d", noun, verb, 100 * noun + verb);
27+
}
28+
}
29+
}
30+
}
31+
32+
public static int[] calculate(int[] data, int noun, int verb) {
33+
data[1] = noun;
34+
data[2] = verb;
35+
int i = 0;
36+
while (i < data.length) {
37+
int opcode = data[i];
38+
if (opcode == 1) {
39+
int aPosition = data[i + 1];
40+
int bPosition = data[i + 2];
41+
int positionToStore = data[i + 3];
42+
int a = data[aPosition];
43+
int b = data[bPosition];
44+
data[positionToStore] = a + b;
45+
i += 4;
46+
} else if (opcode == 2) {
47+
int aPosition = data[i + 1];
48+
int bPosition = data[i + 2];
49+
int positionToStore = data[i + 3];
50+
int a = data[aPosition];
51+
int b = data[bPosition];
52+
data[positionToStore] = a * b;
53+
i += 4;
54+
} else if (opcode == 99) {
55+
break;
56+
}
57+
}
58+
return data;
59+
}
60+
}

0 commit comments

Comments
 (0)