Skip to content

Commit 1c85d49

Browse files
committed
overriding
1 parent 4699bc9 commit 1c85d49

File tree

5 files changed

+103
-10
lines changed

5 files changed

+103
-10
lines changed

advent-of-code-2024/src/main/java/org/example/day4/CeresSearch.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,64 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.Scanner;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
1921

2022
public class CeresSearch {
2123

24+
final static String xmas = "XMAS";
25+
2226
public static void main(String[] args) {
2327

2428
Scanner scanner = TxtReader.scanner("ceres-search.txt");
25-
List<ArrayList<String>> list = CeresSearch.parse(scanner);
26-
System.out.println(list);
29+
ArrayList<String> list = CeresSearch.parse(scanner);
30+
int partOne = CeresSearch.partOne(list);
31+
System.out.println(partOne);
2732
}
2833

29-
public static List<ArrayList<String>> parse(Scanner scanner) {
30-
List<ArrayList<String>> list = new ArrayList<>();
34+
public static int partOne(ArrayList<String> list) {
3135

32-
while (scanner.hasNext()) {
33-
String[] splitLine = scanner.nextLine().split("");
34-
ArrayList<String> lineList = new ArrayList<>();
35-
for (String str : splitLine) {
36-
lineList.add(str);
36+
int sum = 0;
37+
38+
for (int i = 0; i < list.size(); i++) {
39+
String currentList = list.get(i);
40+
41+
// 1
42+
Pattern firstPattern = Pattern.compile("(?=XMAS)");
43+
Matcher firstMatcher = firstPattern.matcher(currentList);
44+
while (firstMatcher.find()) {
45+
sum++;
46+
}
47+
48+
// 2
49+
Pattern secondPattern = Pattern.compile("(?=SAMX)");
50+
Matcher secondMatcher = secondPattern.matcher(currentList);
51+
while (secondMatcher.find()) {
52+
sum++;
3753
}
38-
list.add(lineList);
54+
55+
// 3
56+
for (int j = 0; j < currentList.length(); j++) {
57+
System.out.println(currentList.charAt(j));
58+
59+
}
60+
61+
62+
63+
64+
}
65+
66+
return sum;
67+
68+
69+
}
70+
71+
72+
public static ArrayList<String> parse(Scanner scanner) {
73+
ArrayList<String> list = new ArrayList<>();
74+
75+
while (scanner.hasNext()) {
76+
list.add(scanner.nextLine());
3977
}
4078

4179
return list;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.poly.basic;
2+
3+
public class CastingMain4 {
4+
5+
public static void main(String[] args) {
6+
7+
8+
}
9+
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example.poly.basic;
2+
3+
public class CastingMain5 {
4+
5+
public static void main(String[] args) {
6+
Parent parent1 = new Parent();
7+
call(parent1);
8+
9+
Parent parent2 = new Child();
10+
call(parent2);
11+
12+
}
13+
14+
private static void call(Parent parent) {
15+
parent.parentMethod();
16+
17+
if (parent instanceof Child) {
18+
System.out.println("Child 인스턴스가 맞음");
19+
((Child) parent).childMethod();
20+
}
21+
22+
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.poly.overridnng;
2+
3+
public class Child extends Parent {
4+
5+
public String value = "child";
6+
7+
@Override
8+
public void method() {
9+
System.out.println("Child.method");
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.poly.overridnng;
2+
3+
public class Parent {
4+
5+
public String value = "parent";
6+
7+
public void method() {
8+
System.out.println("Parent.method");
9+
}
10+
}

0 commit comments

Comments
 (0)