-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 14.java
165 lines (151 loc) · 5.33 KB
/
Day 14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
class Main {
private static List<Integer> values = new ArrayList<Integer>();
private static List<Integer> keys = new ArrayList<Integer>();
public static void main(String[] args) {
HashMap<String,HashMap<Integer,Long>> map = populateArray();
//System.out.println(firstStar(map));
HashMap<Integer,Long> sums = firstStar(map);
long ans = 0;
int loopThrough = 0;
for (Integer key : sums.keySet()){
ans += sums.get(key);
loopThrough++;
}
//System.out.println(ans);
//System.out.println(map.size());
//System.out.println(sums.size());
//System.out.println(sums);
//System.out.println(loopThrough);
//findDuplicates(values);
}
public static long getDecimal(String binary){
long ans = 0;
int position = 0;
for (int i = binary.length(); i > 0; i--){
if (binary.substring(i-1, i).equals("1")){
ans += Math.pow(2,position);
}
position++;
}
//System.out.println(ans);
return ans;
}
public static String getBinary(long decimal){
String ans = "";
for (int i = 35; i >= 0; i--){
if (decimal - Math.pow(2,i) > 0){
ans+= "1";
decimal-= Math.pow(2,i);
} else if (decimal == Math.pow(2,i) && decimal != 0){
ans+= "1";
decimal-= Math.pow(2,i);
} else {
ans += "0";
}
}
return ans;
}
public static HashMap<Integer,Long> firstStar(HashMap<String,HashMap<Integer,Long>> outerMap){
HashMap<Integer,Long> sumAns = new HashMap<Integer,Long>();
int addedValueTime = 0;
long sumWithout = 0;
for (String x : outerMap.keySet()){
HashMap<Integer,Long> innerMap = outerMap.get(x);
for (Integer key : innerMap.keySet()){
long value = (innerMap.get(key));
String ans = "";
//System.out.println(value);
String bin = getBinary(value);
//System.out.println(bin);
//System.out.println(x);
for (int i = 0; i < bin.length(); i++){
if (bin.substring(i, i+1).equals("0") && x.substring(i, i+1).equals("X")){
ans+= "0";
} else if (bin.substring(i, i+1).equals("1") && x.substring(i, i+1).equals("1")){
ans+= "1";
}else if (bin.substring(i, i+1).equals("0") && x.substring(i, i+1).equals("1")){
ans+= "1";
} else if(bin.substring(i, i+1).equals("1") && x.substring(i, i+1).equals("X")){
ans+="1";
} else if(bin.substring(i, i+1).equals("") && x.substring(i, i+1).equals("X")){
}else {
ans+= "0";
}
}
long temp = getDecimal(ans);
//System.out.println("temp: " + temp);
innerMap.put(key, temp);
sumAns.put(key, temp);
values.add(key);
keys.add(key);
addedValueTime++;
sumWithout+=temp;
System.out.println(x + "," + key + "," + bin + "," + ans + "," + temp);
}
outerMap.put(x,innerMap);
}
//System.out.println(outerMap);
// System.out.println("sums added together without takign into accoutn duplicates: " + sumWithout);
//System.out.println("added to the other map: " + addedValueTime);
return sumAns;
}
public static HashMap<String,HashMap<Integer,Long>> populateArray(){
HashMap<String,HashMap<Integer,Long>> outerMap = new HashMap<String,HashMap<Integer,Long>>();
try{
File myObj = new File("puzzle.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
while (myReader.hasNextLine()) {
if (data.contains("mask")){
String temp ="";
HashMap<Integer,Long> innerMap = new HashMap<Integer,Long>();
while (myReader.hasNextLine()) {
String innerData = myReader.nextLine();
if (innerData.contains("mask")){
temp = innerData;
break;
} else {
String[] splitArray = innerData.replaceAll("\\s","").split("[\\[\\]=]");
//System.out.println(Arrays.toString(splitArray));
innerMap.put(Integer.parseInt(splitArray[1]),Long.parseLong(splitArray[3]));
}
}
String[] splitArray = data.replaceAll("\\s","").split("=");
outerMap.put(splitArray[1], innerMap);
data = temp;
}
}
}
myReader.close();
} catch (Exception e){
System.out.println("error");
}
//System.out.println(outerMap);
//System.out.println("size" + outerMap.size());
return outerMap;
}
public static Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{
final Set<Integer> setToReturn = new HashSet<>();
final Set<Integer> set1 = new HashSet<>();
for (Integer yourInt : listContainingDuplicates)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
//System.out.println("list size: " + listContainingDuplicates.size());
//System.out.println("not: " + set1);
//Collections.sort(setToReturn);
//System.out.println("duplicated values: " + setToReturn);
//System.out.println(setToReturn.size());
//System.out.println(set1.size());
return setToReturn;
}
}