Created
December 9, 2022 05:58
AoC 2022 day 9 - dart original code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
void main(List<String> args) async { | |
final input = File('day9.input') | |
.readAsStringSync() | |
.split('\n') | |
.map((e) => e.split(' ')) | |
.map((e) => [e[0], int.parse(e[1])]); | |
int offset = 100000; | |
int hx = offset; | |
int hy = offset; | |
List<List<int>> tails = List.generate(9, (index) => [offset, offset]); | |
List<Set<int>> visited = List.generate(9, (index) => {}); | |
addAll(tails, visited, offset); | |
input.forEach((element) { | |
int steps = element[1] as int; | |
switch (element[0]) { | |
case 'R': | |
for (int i = 0; i < steps; i++) { | |
hx++; | |
updateTails(tails, hx, hy); | |
addAll(tails, visited, offset); | |
print('R'); | |
} | |
break; | |
case 'L': | |
for (int i = 0; i < steps; i++) { | |
hx--; | |
updateTails(tails, hx, hy); | |
addAll(tails, visited, offset); | |
print('L'); | |
} | |
break; | |
case 'D': | |
for (int i = 0; i < steps; i++) { | |
hy--; | |
updateTails(tails, hx, hy); | |
addAll(tails, visited, offset); | |
print('D'); | |
} | |
break; | |
case 'U': | |
for (int i = 0; i < steps; i++) { | |
hy++; | |
updateTails(tails, hx, hy); | |
addAll(tails, visited, offset); | |
print('U'); | |
} | |
break; | |
} | |
}); | |
print(visited.first.length); | |
print(visited.last.length); | |
} | |
void updateTails(List<List<int>> tails, int hx, int hy) { | |
tails.insert(0, updateTail(tails.removeAt(0), hx, hy)); | |
for (int i = 1; i < tails.length; i++) { | |
List<int> head = tails[i - 1]; | |
List<int> tail = tails.removeAt(i); | |
tails.insert(i, updateTail(tail, head[0], head[1])); | |
} | |
} | |
void addAll(List<List<int>> tails, List<Set<int>> visited, int s) { | |
for (int i = 0; i < tails.length; i++) { | |
visited[i].add(tails[i][0] * s + tails[i][1]); | |
} | |
} | |
List<int> updateTail(List<int> tail, int hx, int hy) { | |
int tx = tail[0]; | |
int ty = tail[1]; | |
int dx = hx - tx; | |
int dy = hy - ty; | |
switch (dx) { | |
case 2: | |
{ | |
switch (dy) { | |
case 0: | |
print(' t R'); | |
return [tx + 1, ty]; | |
case -1: | |
print(' t RD'); | |
return [tx + 1, ty - 1]; | |
case 1: | |
print(' t RU'); | |
return [tx + 1, ty + 1]; | |
case 2: | |
print(' ! RU'); | |
return [tx + 1, ty + 1]; | |
case -2: | |
print(' ! RD'); | |
return [tx + 1, ty - 1]; | |
} | |
break; | |
} | |
case 1: | |
{ | |
switch (dy) { | |
case 0: | |
case 1: | |
case -1: | |
return [tx, ty]; | |
case 2: | |
print(' t UR'); | |
return [tx + 1, ty + 1]; | |
case -2: | |
print(' t DR'); | |
return [tx + 1, ty - 1]; | |
} | |
break; | |
} | |
case 0: | |
{ | |
switch (dy) { | |
case 0: | |
case 1: | |
case -1: | |
return [tx, ty]; | |
case 2: | |
print(' t U'); | |
return [tx, ty + 1]; | |
case -2: | |
print(' t D'); | |
return [tx, ty - 1]; | |
} | |
break; | |
} | |
case -1: | |
{ | |
switch (dy) { | |
case 0: | |
case 1: | |
case -1: | |
return [tx, ty]; | |
case 2: | |
print(' t LU'); | |
return [tx - 1, ty + 1]; | |
case -2: | |
print(' t LD'); | |
return [tx - 1, ty - 1]; | |
} | |
break; | |
} | |
case -2: | |
{ | |
switch (dy) { | |
case -1: | |
print(' t LD'); | |
return [tx - 1, ty - 1]; | |
case 0: | |
print(' t L'); | |
return [tx - 1, ty]; | |
case 1: | |
print(' t LU'); | |
return [tx - 1, ty + 1]; | |
case 2: | |
print(' ! LU'); | |
return [tx - 1, ty + 1]; | |
case -2: | |
print(' ! LD'); | |
return [tx - 1, ty - 1]; | |
} | |
break; | |
} | |
} | |
print(' Not yet implemented case: $dx $dy'); | |
throw ArgumentError(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment