Skip to content

Instantly share code, notes, and snippets.

@mikezs
Last active December 12, 2018 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezs/10a49d4826cc29ac3e6fdd5d80c2d833 to your computer and use it in GitHub Desktop.
Save mikezs/10a49d4826cc29ac3e6fdd5d80c2d833 to your computer and use it in GitHub Desktop.
AdventOfCode 2015
//part1
void main() {
int floor = 0;
for (int i = 0; i < input.length; ++i) {
if (input[i] == ")") --floor;
else if (input[i] == "(") ++floor;
}
print(floor);
}
//part2
void main() {
int floor = 0;
for (int i = 0; i < input.length; ++i) {
if (input[i] == ")") --floor;
else if (input[i] == "(") ++floor;
if (floor == -1) {
print(i+1);
break;
}
}
}
// Part 1
import "dart:math";
void main() {
int value = 0;
input.split("\n").forEach((element) {
List<int> parts = element.split("x").map((i) => int.parse(i)).toList();
int side1 = parts[0]*parts[1];
int side2 = parts[1]*parts[2];
int side3 = parts[0]*parts[2];
value += 2*side1 + 2*side2 + 2*side3 + min(min(side1, side2), side3);
});
print(value);
}
// Part 2
void main() {
int value = 0;
input.split("\n").forEach((element) {
List<int> parts = element.split("x").map((i) => int.parse(i)).toList();
parts.sort((a, b) => a.compareTo(b));
value += 2*parts[0]+2*parts[1]+parts[0]*parts[1]*parts[2];
});
print(value);
}
// Part 1
void main() {
int x = 0;
int y = 0;
Map<String, int> locations = Map<String, int>();
locations[key(x,y)] = 1;
input.split("").forEach((direction) {
if (direction == "<") --x;
else if (direction == ">") ++x;
else if (direction == "v") --y;
else if (direction == "^") ++y;
if (locations[key(x,y)] == null) locations[key(x,y)] = 0;
locations[key(x,y)] += 1;
});
print(locations.length);
}
String key(int x, int y) {
return "$x,$y";
}
// Part 2
void main() {
int santaX = 0;
int santaY = 0;
int roboX = 0;
int roboY = 0;
Map<String, int> locations = Map<String, int>();
// Both deliver present before moving
locations[key(0,0)] = 2;
int index = 0;
input.split("").forEach((direction) {
int x;
int y;
// Setup our coordinates for whoever is delivering
if (index % 2 == 0) {
x = santaX;
y = santaY;
} else {
x = roboX;
y = roboY;
}
// Update our coordinates with a direction
if (direction == "<") --x;
else if (direction == ">") ++x;
else if (direction == "v") --y;
else if (direction == "^") ++y;
if (locations[key(x,y)] == null) locations[key(x,y)] = 0;
locations[key(x,y)] += 1;
// Update our locations with the new coordinates
if (index % 2 == 0) {
santaX = x;
santaY = y;
} else {
roboX = x;
roboY = y;
}
++index;
});
print(locations.length);
}
String key(int x, int y) {
return "$x,$y";
}
// Part 1
void main() {
int count = 0;
input.split("\n").forEach((line) {
if (vowels(line) >= 3 && doubleLetters(line) && !forbiddenStrings(line)) ++count;
});
print(count);
}
int vowels(String str) {
int vowels = 0;
str.split("").forEach((l) {
if (l == "a" || l == "e" || l == "i" || l == "o" || l == "u") ++vowels;
});
return vowels;
}
bool doubleLetters(String str) {
for (int i = 0; i < str.length - 1; ++i) {
if (str[i] == str[i+1]) return true;
}
return false;
}
bool forbiddenStrings(String str) {
return str.contains("ab") || str.contains("cd") || str.contains("pq") || str.contains("xy");
}
// Part 2
void main() {
int count = 0;
input.split("\n").forEach((line) {
if (repeatedPairs(line) && repeating(line)) ++count;
});
print(count);
}
bool repeatedPairs(String str) {
for (int i = 0; i < str.length - 2; ++i) {
String search = "${str[i]}${str[i+1]}";
if (str.contains(search, i+2)) return true;
}
return false;
}
bool repeating(String str) {
for (int i = 0; i < str.length - 2; ++i) {
if (str[i] == str[i+2]) return true;
}
return false;
}
// Part 1
enum Operation { on, off, toggle }
void main() {
List<List<bool>> lights = List<List<bool>>(1000);
for (int i = 0; i < 1000; ++i) {
lights[i] = List<bool>(1000);
for (int j = 0; j < 1000; ++j) {
lights[i][j] = false;
}
}
input.split("\n").forEach((line) {
List<String> parts = line.split(" ");
Operation op;
List<int> start;
List<int> end;
if (parts[1] == "off") {
op = Operation.off;
start = parts[2].split(",").map((c) => int.parse(c)).toList();
end = parts[4].split(",").map((c) => int.parse(c)).toList();
} else if (parts[1] == "on") {
op = Operation.on;
start = parts[2].split(",").map((c) => int.parse(c)).toList();
end = parts[4].split(",").map((c) => int.parse(c)).toList();
} else if (parts[0] == "toggle") {
op = Operation.toggle;
start = parts[1].split(",").map((c) => int.parse(c)).toList();
end = parts[3].split(",").map((c) => int.parse(c)).toList();
}
for (int i = start[0]; i <= end[0]; ++i) {
for (int j = start[1]; j <= end[1]; ++j) {
if (op == Operation.on) lights[i][j] = true;
else if (op == Operation.off) lights[i][j] = false;
else if (op == Operation.toggle) lights[i][j] = !lights[i][j];
}
}
});
int count = 0;
for (int i = 0; i < 1000; ++i) {
for (int j = 0; j < 1000; ++j) {
if (lights[i][j]) ++count;
}
}
print(count);
}
// Part 2
enum Operation { on, off, toggle }
void main() {
List<List<int>> lights = List<List<int>>(1000);
for (int i = 0; i < 1000; ++i) {
lights[i] = List<int>(1000);
for (int j = 0; j < 1000; ++j) {
lights[i][j] = 0;
}
}
input.split("\n").forEach((line) {
List<String> parts = line.split(" ");
Operation op;
List<int> start;
List<int> end;
if (parts[1] == "off") {
op = Operation.off;
start = parts[2].split(",").map((c) => int.parse(c)).toList();
end = parts[4].split(",").map((c) => int.parse(c)).toList();
} else if (parts[1] == "on") {
op = Operation.on;
start = parts[2].split(",").map((c) => int.parse(c)).toList();
end = parts[4].split(",").map((c) => int.parse(c)).toList();
} else if (parts[0] == "toggle") {
op = Operation.toggle;
start = parts[1].split(",").map((c) => int.parse(c)).toList();
end = parts[3].split(",").map((c) => int.parse(c)).toList();
}
for (int i = start[0]; i <= end[0]; ++i) {
for (int j = start[1]; j <= end[1]; ++j) {
if (op == Operation.on) lights[i][j] += 1;
else if (op == Operation.off && lights[i][j] > 0) lights[i][j] -= 1;
else if (op == Operation.toggle) lights[i][j] += 2;
}
}
});
int count = 0;
for (int i = 0; i < 1000; ++i) {
for (int j = 0; j < 1000; ++j) {
count += lights[i][j];
}
}
print(count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment