Day 2
This commit is contained in:
parent
0b8a1447c6
commit
62d4515b31
3 changed files with 2671 additions and 0 deletions
2500
day2/input1.txt
Normal file
2500
day2/input1.txt
Normal file
File diff suppressed because it is too large
Load diff
84
day2/p1.nim
Normal file
84
day2/p1.nim
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
import os
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
|
# A, X - rock
|
||||||
|
# B, Y - paper
|
||||||
|
# C, Z - scissors
|
||||||
|
|
||||||
|
const
|
||||||
|
ROCK_POINTS = 1
|
||||||
|
PAPER_POINTS = 2
|
||||||
|
SCISSORS_POINTS = 3
|
||||||
|
|
||||||
|
ROCK = "A"
|
||||||
|
PAPER = "B"
|
||||||
|
SCISSORS = "C"
|
||||||
|
|
||||||
|
LOSE_POINTS = 0
|
||||||
|
DRAW_POINTS = 3
|
||||||
|
WIN_POINTS = 6
|
||||||
|
|
||||||
|
YOU_ROCK = "X"
|
||||||
|
YOU_PAPER = "Y"
|
||||||
|
YOU_SCISSORS = "Z"
|
||||||
|
|
||||||
|
proc rock(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of YOU_ROCK:
|
||||||
|
ROCK_POINTS + DRAW_POINTS
|
||||||
|
of YOU_PAPER:
|
||||||
|
PAPER_POINTS + WIN_POINTS
|
||||||
|
of YOU_SCISSORS:
|
||||||
|
SCISSORS_POINTS + LOSE_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc paper(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of YOU_ROCK:
|
||||||
|
ROCK_POINTS + LOSE_POINTS
|
||||||
|
of YOU_PAPER:
|
||||||
|
PAPER_POINTS + DRAW_POINTS
|
||||||
|
of YOU_SCISSORS:
|
||||||
|
SCISSORS_POINTS + WIN_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc scissors(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of YOU_ROCK:
|
||||||
|
ROCK_POINTS + WIN_POINTS
|
||||||
|
of YOU_PAPER:
|
||||||
|
PAPER_POINTS + LOSE_POINTS
|
||||||
|
of YOU_SCISSORS:
|
||||||
|
SCISSORS_POINTS + DRAW_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc calcScore(left, right: string): int =
|
||||||
|
case left:
|
||||||
|
of ROCK:
|
||||||
|
rock(right)
|
||||||
|
of PAPER:
|
||||||
|
paper(right)
|
||||||
|
of SCISSORS:
|
||||||
|
scissors(right)
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
let fileName = paramStr(1)
|
||||||
|
|
||||||
|
var sum = 0
|
||||||
|
var count = 0
|
||||||
|
|
||||||
|
for line in fileName.lines:
|
||||||
|
let sp = line.split(" ")
|
||||||
|
sum += calcScore(sp[0], sp[1])
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
echo sum
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
87
day2/p2.nim
Normal file
87
day2/p2.nim
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
import os
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
|
# A - rock
|
||||||
|
# B - paper
|
||||||
|
# C - scissors
|
||||||
|
# X = lose
|
||||||
|
# Y = draw
|
||||||
|
# Z = win
|
||||||
|
|
||||||
|
const
|
||||||
|
ROCK_POINTS = 1
|
||||||
|
PAPER_POINTS = 2
|
||||||
|
SCISSORS_POINTS = 3
|
||||||
|
|
||||||
|
ROCK = "A"
|
||||||
|
PAPER = "B"
|
||||||
|
SCISSORS = "C"
|
||||||
|
|
||||||
|
LOSE_POINTS = 0
|
||||||
|
DRAW_POINTS = 3
|
||||||
|
WIN_POINTS = 6
|
||||||
|
|
||||||
|
LOSE = "X"
|
||||||
|
DRAW = "Y"
|
||||||
|
WIN = "Z"
|
||||||
|
|
||||||
|
proc rock(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of LOSE:
|
||||||
|
SCISSORS_POINTS + LOSE_POINTS
|
||||||
|
of DRAW:
|
||||||
|
ROCK_POINTS + DRAW_POINTS
|
||||||
|
of WIN:
|
||||||
|
PAPER_POINTS + WIN_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc paper(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of LOSE:
|
||||||
|
ROCK_POINTS + LOSE_POINTS
|
||||||
|
of DRAW:
|
||||||
|
PAPER_POINTS + DRAW_POINTS
|
||||||
|
of WIN:
|
||||||
|
SCISSORS_POINTS + WIN_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc scissors(symStr: string): int =
|
||||||
|
case symStr:
|
||||||
|
of LOSE:
|
||||||
|
PAPER_POINTS + LOSE_POINTS
|
||||||
|
of DRAW:
|
||||||
|
SCISSORS_POINTS + DRAW_POINTS
|
||||||
|
of WIN:
|
||||||
|
ROCK_POINTS + WIN_POINTS
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
proc calcScore(left, right: string): int =
|
||||||
|
case left:
|
||||||
|
of ROCK:
|
||||||
|
rock(right)
|
||||||
|
of PAPER:
|
||||||
|
paper(right)
|
||||||
|
of SCISSORS:
|
||||||
|
scissors(right)
|
||||||
|
else:
|
||||||
|
0
|
||||||
|
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
let fileName = paramStr(1)
|
||||||
|
|
||||||
|
var sum = 0
|
||||||
|
var count = 0
|
||||||
|
|
||||||
|
for line in fileName.lines:
|
||||||
|
let sp = line.split(" ")
|
||||||
|
sum += calcScore(sp[0], sp[1])
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
echo sum
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue