Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Advent of Code 2022 with Ruby day 01
Ankit Pariyar
Ankit Pariyar

Posted on • Edited on

Advent of Code 2022 with Ruby day 01

Problem

  • We have text file which look like this
451480096703181148813905
Enter fullscreen modeExit fullscreen mode
  • Above number represents Calories of the food carried by 3 Elves. Blank line separate the food carried by each Elves

  • We have to find total Calories of the food carried by Elf carrying the most Calories

Solution

First let's break down problem

  • Reading text file
  • Find out total calories carried by each Elves
  • Find out maximum out of that calories
calories_carried_by_each_elves=0max_calories=0File.foreach('day_01.txt')do|calories|ifcalories.to_i.positive?calories_carried_by_each_elves+=calories.to_ielsemax_calories=calories_carried_by_each_elvesifcalories_carried_by_each_elves>max_caloriescalories_carried_by_each_elves=0endendputsmax_calories
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Work
    Ruby on Rails developer
  • Joined

More fromAnkit Pariyar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp