Add day2 problem + solution
This commit is contained in:
parent
bde3a8968d
commit
1dd6a46bb6
4 changed files with 1125 additions and 0 deletions
35
day2/p2.nim
Normal file
35
day2/p2.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import os
|
||||
import std/strutils
|
||||
|
||||
type
|
||||
Command = enum
|
||||
Forward = "forward",
|
||||
Down = "down",
|
||||
Up = "up"
|
||||
|
||||
proc main() =
|
||||
let fileName = paramStr(1)
|
||||
|
||||
var
|
||||
horizontal = 0
|
||||
depth = 0
|
||||
aim = 0
|
||||
|
||||
for val in fileName.lines:
|
||||
let
|
||||
commandAndUnits = val.split(" ")
|
||||
command = parseEnum[Command](commandAndUnits[0])
|
||||
units = commandAndUnits[1].parseInt
|
||||
|
||||
case command
|
||||
of Forward:
|
||||
horizontal += units
|
||||
depth += aim * units
|
||||
of Up:
|
||||
aim -= units
|
||||
of Down:
|
||||
aim += units
|
||||
|
||||
echo "Horizontal x Depth: ", horizontal * depth
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue