aboutsummaryrefslogtreecommitdiffstats
path: root/day9/solution.nim
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2024-06-08 13:50:47 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2024-06-08 13:50:47 +0300
commit7be570c4a6e86fb7060f0bc06910ca57003dfe90 (patch)
treea998f976596cb902cd8988d74f756b72660bc29c /day9/solution.nim
parentDay 21 (part1) (diff)
downloadaoc22-main.tar.gz
aoc22-main.zip
Update file names and init.shHEADmain
Diffstat (limited to 'day9/solution.nim')
-rw-r--r--day9/solution.nim61
1 files changed, 0 insertions, 61 deletions
diff --git a/day9/solution.nim b/day9/solution.nim
deleted file mode 100644
index ef25492..0000000
--- a/day9/solution.nim
+++ /dev/null
@@ -1,61 +0,0 @@
1import strutils
2import sequtils
3import sets
4
5type Move = tuple[Direction: char, Distance: int]
6type Position = tuple[r: int, c: int]
7
8proc parse(content: seq[string]): seq[Move] =
9 let moves = map(content,
10 proc (line: string): Move =
11 let temp = line.split(" ")
12 (temp[0][0], temp[1].parseInt())
13 )
14 return moves
15
16proc move(rope: var seq[Position], uniqPositions: var HashSet[Position]): void =
17 for i in 1 ..< rope.len():
18 # rope[i-1] = leader
19 # rope[i] = follower
20 let roffset = rope[i-1].r - rope[i].r
21 let coffset = rope[i-1].c - rope[i].c
22
23 if roffset in [2, -2]:
24 rope[i].r += int(roffset/2)
25 if coffset in [-1, 1]:
26 rope[i].c = rope[i-1].c
27
28 if coffset in [2, -2]:
29 rope[i].c += int(coffset/2)
30 if roffset in [-1, 1]:
31 rope[i].r = rope[i-1].r
32
33 uniqPositions.incl(rope[rope.len()-1])
34
35proc emulate(moves: seq[Move], len = 2): int =
36 var rope = newSeq[Position](len)
37 var uniqPositions: HashSet[Position]
38 init(uniqPositions)
39 echo rope
40 for move in moves:
41 for i in 1 .. move.Distance:
42 case move.Direction:
43 of 'U':
44 rope[0].r -= 1
45 of 'R':
46 rope[0].c += 1
47 of 'D':
48 rope[0].r += 1
49 of 'L':
50 rope[0].c -= 1
51 else:
52 assert(false)
53 move(rope, uniqPositions)
54
55 return len(uniqPositions)
56
57let content = readFile("./input.txt").strip().splitLines()
58let moves = parse(content)
59
60echo emulate(moves)
61echo emulate(moves, 10)