2023 Day 8 Part 1
This commit is contained in:
parent
d13d30dd57
commit
3bddc3760c
1 changed files with 58 additions and 0 deletions
58
2023/day08.nim
Normal file
58
2023/day08.nim
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import std/algorithm
|
||||||
|
import std/enumerate
|
||||||
|
import std/math
|
||||||
|
import std/sequtils
|
||||||
|
import std/sets
|
||||||
|
import std/strscans
|
||||||
|
import std/strutils
|
||||||
|
import std/sugar
|
||||||
|
import std/tables
|
||||||
|
|
||||||
|
type Move {.pure.} = enum
|
||||||
|
L
|
||||||
|
R
|
||||||
|
|
||||||
|
proc parseMove(c: char): Move =
|
||||||
|
case c:
|
||||||
|
of 'L': L
|
||||||
|
of 'R': R
|
||||||
|
else: raise newException(ValueError, $c)
|
||||||
|
|
||||||
|
func parseMoves(line: string): seq[Move] =
|
||||||
|
line.map(parseMove)
|
||||||
|
|
||||||
|
type Node = object
|
||||||
|
id: string
|
||||||
|
left: string
|
||||||
|
right: string
|
||||||
|
|
||||||
|
proc parseNode(line: string): Node =
|
||||||
|
if not scanf(line, "$+ = ($+, $+)$.", result.id, result.left, result.right):
|
||||||
|
raise newException(ValueError, line)
|
||||||
|
|
||||||
|
type Map = object
|
||||||
|
moves: seq[Move]
|
||||||
|
nodes: Table[string, Node]
|
||||||
|
|
||||||
|
proc parseMap(text: string): Map =
|
||||||
|
let parts = text.strip.split("\n\n")
|
||||||
|
result.moves = parts[0].parseMoves
|
||||||
|
for node in parts[1].strip.splitLines.map(parseNode):
|
||||||
|
result.nodes[node.id] = node
|
||||||
|
|
||||||
|
proc dbg[T](v: T): T = (echo(v); v)
|
||||||
|
|
||||||
|
proc part1(): int =
|
||||||
|
let text = readFile("input/day08.txt")
|
||||||
|
var map = parseMap(text)
|
||||||
|
|
||||||
|
var mv = 0
|
||||||
|
var loc = "AAA"
|
||||||
|
while loc != "ZZZ":
|
||||||
|
case map.moves[mv mod len(map.moves)]:
|
||||||
|
of L: loc = map.nodes[loc].left
|
||||||
|
of R: loc = map.nodes[loc].right
|
||||||
|
mv = mv + 1
|
||||||
|
return mv
|
||||||
|
|
||||||
|
echo part1()
|
||||||
Loading…
Reference in a new issue