What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
This challenge takes its roots from the world-famous game Pacman. To finish the game, pacman needs to gather all pacgum on the map.
The goal of this chalenge is to have a time-limited pacman. Pacman must gather as much pacgum as possible in the given time. To simplify, we will say that 1 move (no diagonals) = 1 unit of time.
Formal Inputs & Outputs
Input description
You will be given a number, the time pacman has to gather as much pacgum as possible, and a table, being the map pacman has to explore. Every square of this map can be one of those things :
A number N between (1 and 9) of pacgums that pacman can gather in one unit of time.
"X" squares cannot be gone through.
"C" will be where pacman starts.
"O" (the letter, not zero ) will be a warp to another "O". There can be only 2 "O" on one map;
Output description
Your program should output the maximum number of pacgums pacman can gather in the given time.
Examples
Example Input
Input 1 :
4
XXXXX
X197X
X2C6X
X345X
XXXXX
Input 2 :
3
XXXXXXXXXXXXXX
X111C2OXO2111X
XXXXXXXXXXXXXX
Example outputs :
Output 1 : 27
Output 2 : 4
Challenge Input :
Challenge Input 1 :
10
XXXXXXXXXXXXX
X23543561195X
X9X3X17C12X4X
X515OX1183X6X
X7865X48O585X
XXXXXXXXXXXXX
Challenge Input 2 :
20
XXXXXXXXXXXXXX
XXC1212121213X
X4X21212O2121X
X44X232323232X
X444X43434343X
X4444XXXXXX77X
X4444O6789X99X
XXXXXXXXXXXXXX
Notes
You can specify the number oflines and columns of the table next to it to ease the workload.
As for the warp, you can either choose to ignore it or teleport yourself, you don't always teleport.
Solution
in Java
Main.java
public class Main {
public static void main(String[] args) {
String map =
"XXXXXXXXXXXXXX\n" +
"XXC1212121213X\n" +
"X4X21212O2121X\n" +
"X44X232323232X\n" +
"X444X43434343X\n" +
"X4444XXXXXX77X\n" +
"X4444O6789X99X\n" +
"XXXXXXXXXXXXXX";
int time = 20;
Pacman pacman = new Pacman(map);
int maximumPacgums = pacman.getMaximumPacgums(time);
System.out.println(maximumPacgums);
}
}
Pacman.java
class Pacman {
private Node current;
public Pacman(String map) {
MapParser mp = new MapParser();
current = mp.parse(map);
}
public int getMaximumPacgums(int time) {
return getMaximumPoints(current, time);
}
private Integer getMaximumPoints(Node node, int time) {
return getMaximumPoints(node, time, true);
}
private Integer getMaximumPoints(Node node, int time, boolean allowWrap) {
if (node == null || time < 0 || node.isUsed()) {
return 0;
}
node.toggleUsed();
List<Integer> list = new ArrayList<>();
list.add(getMaximumPoints(node.getUp(), time - 1));
list.add(getMaximumPoints(node.getLeft(), time - 1));
list.add(getMaximumPoints(node.getRight(), time - 1));
list.add(getMaximumPoints(node.getDown(), time - 1));
if (allowWrap) {
list.add(getMaximumPoints(node.getWrap(), time, false));
}
node.toggleUsed();
int currentValue = (node != this.current) ? node.getValue() : 0;
return Collections.max(list) + currentValue;
}
}
MapParser.java
class MapParser {
private static final int C = -1;
private static final int X = -2;
private static final int O = 0;
public Node parse(String map) {
return parseNodes(map);
}
private Node parseNodes(String map) {
return parseNodes(parseMap(map));
}
private Node parseNodes(String[][] mapArr) {
Node[][] mapNodes = new Node[mapArr.length][mapArr[0].length];
Node wrap = null;
for (int i = 0; i < mapArr.length; i++) {
for (int j = 0; j < mapArr[i].length; j++) {
int nodeValue = getValue(mapArr[i][j]);
if (nodeValue != X) {
Node node = new Node();
node.setValue(nodeValue);
if (node.getValue() == O) {
if (wrap == null) {
wrap = node;
} else {
wrap.setWrap(node);
node.setWrap(wrap);
}
}
mapNodes[i][j] = node;
} else {
mapNodes[i][j] = null;
}
}
}
return parseNodes(mapNodes);
}
private Node parseNodes(Node[][] mapArr) {
Node current = null;
for (int i = 0; i < mapArr.length; i++) {
for (int j = 0; j < mapArr[i].length; j++) {
Node node = mapArr[i][j];
if (node != null) {
node.setUp(getUpNode(mapArr, i, j));
node.setDown(getDownNode(mapArr, i, j));
node.setLeft(getLeftNode(mapArr, i, j));
node.setRight(getRightNode(mapArr, i, j));
if (node.getValue() == C) {
current = node;
}
}
}
}
return current;
}
private Node getNode(Node[][] mapArr, int i, int j) {
if (!valideBounds(mapArr, i, j)) {
return null;
}
return mapArr[i][j];
}
private Node getRightNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i, j + 1);
}
private Node getLeftNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i, j - 1);
}
private Node getDownNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i + 1, j);
}
private Node getUpNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i - 1, j);
}
private boolean valideBounds(Node[][] mapArr, int i, int j) {
return !(i < 0 || i >= mapArr.length || j < 0 || j >= mapArr[i].length);
}
private int getValue(String value) {
switch (value) {
case "C":
return C;
case "O":
return O;
case "X":
return X;
default:
return Integer.parseInt(value);
}
}
private String[][] parseMap(String map) {
String[] rows = map.split("\n");
String[][] mapArr = new String[rows.length][];
for (int i = 0; i < rows.length; i++) {
String row = rows[i];
mapArr[i] = row.split("");
}
return mapArr;
}
}
Node.java
class Node {
private Node up = null, down = null, left = null, right = null, wrap = null;
private int value;
private boolean used;
public Node getUp() {
return up;
}
public void setUp(Node up) {
this.up = up;
}
public Node getDown() {
return down;
}
public void setDown(Node down) {
this.down = down;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Node getWrap() {
return wrap;
}
public void setWrap(Node wrap) {
this.wrap = wrap;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isUsed() {
return used;
}
public void toggleUsed(){
this.used = !this.used;
}
public void setUsed(boolean used) {
this.used = used;
}
}
This challenge takes its roots from the world-famous game Pacman. To finish the game, pacman needs to gather all pacgum on the map.
The goal of this chalenge is to have a time-limited pacman. Pacman must gather as much pacgum as possible in the given time. To simplify, we will say that 1 move (no diagonals) = 1 unit of time.
Formal Inputs & Outputs
Input description
You will be given a number, the time pacman has to gather as much pacgum as possible, and a table, being the map pacman has to explore. Every square of this map can be one of those things :
A number N between (1 and 9) of pacgums that pacman can gather in one unit of time.
"X" squares cannot be gone through.
"C" will be where pacman starts.
"O" (the letter, not zero ) will be a warp to another "O". There can be only 2 "O" on one map;
Output description
Your program should output the maximum number of pacgums pacman can gather in the given time.
Examples
Example Input
Input 1 :
4
XXXXX
X197X
X2C6X
X345X
XXXXX
Input 2 :
3
XXXXXXXXXXXXXX
X111C2OXO2111X
XXXXXXXXXXXXXX
Example outputs :
Output 1 : 27
Output 2 : 4
Challenge Input :
Challenge Input 1 :
10
XXXXXXXXXXXXX
X23543561195X
X9X3X17C12X4X
X515OX1183X6X
X7865X48O585X
XXXXXXXXXXXXX
Challenge Input 2 :
20
XXXXXXXXXXXXXX
XXC1212121213X
X4X21212O2121X
X44X232323232X
X444X43434343X
X4444XXXXXX77X
X4444O6789X99X
XXXXXXXXXXXXXX
Notes
You can specify the number oflines and columns of the table next to it to ease the workload.
As for the warp, you can either choose to ignore it or teleport yourself, you don't always teleport.
Solution
in Java
Main.java
public class Main {
public static void main(String[] args) {
String map =
"XXXXXXXXXXXXXX\n" +
"XXC1212121213X\n" +
"X4X21212O2121X\n" +
"X44X232323232X\n" +
"X444X43434343X\n" +
"X4444XXXXXX77X\n" +
"X4444O6789X99X\n" +
"XXXXXXXXXXXXXX";
int time = 20;
Pacman pacman = new Pacman(map);
int maximumPacgums = pacman.getMaximumPacgums(time);
System.out.println(maximumPacgums);
}
}
Pacman.java
class Pacman {
private Node current;
public Pacman(String map) {
MapParser mp = new MapParser();
current = mp.parse(map);
}
public int getMaximumPacgums(int time) {
return getMaximumPoints(current, time);
}
private Integer getMaximumPoints(Node node, int time) {
return getMaximumPoints(node, time, true);
}
private Integer getMaximumPoints(Node node, int time, boolean allowWrap) {
if (node == null || time < 0 || node.isUsed()) {
return 0;
}
node.toggleUsed();
List<Integer> list = new ArrayList<>();
list.add(getMaximumPoints(node.getUp(), time - 1));
list.add(getMaximumPoints(node.getLeft(), time - 1));
list.add(getMaximumPoints(node.getRight(), time - 1));
list.add(getMaximumPoints(node.getDown(), time - 1));
if (allowWrap) {
list.add(getMaximumPoints(node.getWrap(), time, false));
}
node.toggleUsed();
int currentValue = (node != this.current) ? node.getValue() : 0;
return Collections.max(list) + currentValue;
}
}
MapParser.java
class MapParser {
private static final int C = -1;
private static final int X = -2;
private static final int O = 0;
public Node parse(String map) {
return parseNodes(map);
}
private Node parseNodes(String map) {
return parseNodes(parseMap(map));
}
private Node parseNodes(String[][] mapArr) {
Node[][] mapNodes = new Node[mapArr.length][mapArr[0].length];
Node wrap = null;
for (int i = 0; i < mapArr.length; i++) {
for (int j = 0; j < mapArr[i].length; j++) {
int nodeValue = getValue(mapArr[i][j]);
if (nodeValue != X) {
Node node = new Node();
node.setValue(nodeValue);
if (node.getValue() == O) {
if (wrap == null) {
wrap = node;
} else {
wrap.setWrap(node);
node.setWrap(wrap);
}
}
mapNodes[i][j] = node;
} else {
mapNodes[i][j] = null;
}
}
}
return parseNodes(mapNodes);
}
private Node parseNodes(Node[][] mapArr) {
Node current = null;
for (int i = 0; i < mapArr.length; i++) {
for (int j = 0; j < mapArr[i].length; j++) {
Node node = mapArr[i][j];
if (node != null) {
node.setUp(getUpNode(mapArr, i, j));
node.setDown(getDownNode(mapArr, i, j));
node.setLeft(getLeftNode(mapArr, i, j));
node.setRight(getRightNode(mapArr, i, j));
if (node.getValue() == C) {
current = node;
}
}
}
}
return current;
}
private Node getNode(Node[][] mapArr, int i, int j) {
if (!valideBounds(mapArr, i, j)) {
return null;
}
return mapArr[i][j];
}
private Node getRightNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i, j + 1);
}
private Node getLeftNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i, j - 1);
}
private Node getDownNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i + 1, j);
}
private Node getUpNode(Node[][] mapArr, int i, int j) {
return getNode(mapArr, i - 1, j);
}
private boolean valideBounds(Node[][] mapArr, int i, int j) {
return !(i < 0 || i >= mapArr.length || j < 0 || j >= mapArr[i].length);
}
private int getValue(String value) {
switch (value) {
case "C":
return C;
case "O":
return O;
case "X":
return X;
default:
return Integer.parseInt(value);
}
}
private String[][] parseMap(String map) {
String[] rows = map.split("\n");
String[][] mapArr = new String[rows.length][];
for (int i = 0; i < rows.length; i++) {
String row = rows[i];
mapArr[i] = row.split("");
}
return mapArr;
}
}
Node.java
class Node {
private Node up = null, down = null, left = null, right = null, wrap = null;
private int value;
private boolean used;
public Node getUp() {
return up;
}
public void setUp(Node up) {
this.up = up;
}
public Node getDown() {
return down;
}
public void setDown(Node down) {
this.down = down;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Node getWrap() {
return wrap;
}
public void setWrap(Node wrap) {
this.wrap = wrap;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isUsed() {
return used;
}
public void toggleUsed(){
this.used = !this.used;
}
public void setUsed(boolean used) {
this.used = used;
}
}
Comments
Post a Comment