day01
This commit is contained in:
		
							parent
							
								
									c6f84b7615
								
							
						
					
					
						commit
						da179bdc15
					
				
					 6 changed files with 114 additions and 0 deletions
				
			
		
							
								
								
									
										2
									
								
								2024/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								2024/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
input*.txt
 | 
			
		||||
mise.toml
 | 
			
		||||
							
								
								
									
										4
									
								
								2024/day01/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								2024/day01/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,4 @@
 | 
			
		|||
*.beam
 | 
			
		||||
*.ez
 | 
			
		||||
/build
 | 
			
		||||
erl_crash.dump
 | 
			
		||||
							
								
								
									
										20
									
								
								2024/day01/gleam.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								2024/day01/gleam.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
name = "day01"
 | 
			
		||||
version = "1.0.0"
 | 
			
		||||
 | 
			
		||||
# Fill out these fields if you intend to generate HTML documentation or publish
 | 
			
		||||
# your project to the Hex package manager.
 | 
			
		||||
#
 | 
			
		||||
# description = ""
 | 
			
		||||
# licences = ["Apache-2.0"]
 | 
			
		||||
# repository = { type = "github", user = "", repo = "" }
 | 
			
		||||
# links = [{ title = "Website", href = "" }]
 | 
			
		||||
#
 | 
			
		||||
# For a full reference of all the available options, you can have a look at
 | 
			
		||||
# https://gleam.run/writing-gleam/gleam-toml/.
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
 | 
			
		||||
gleam_yielder = ">= 1.1.0 and < 2.0.0"
 | 
			
		||||
 | 
			
		||||
[dev-dependencies]
 | 
			
		||||
gleeunit = ">= 1.0.0 and < 2.0.0"
 | 
			
		||||
							
								
								
									
										13
									
								
								2024/day01/manifest.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								2024/day01/manifest.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
# This file was generated by Gleam
 | 
			
		||||
# You typically do not need to edit this file
 | 
			
		||||
 | 
			
		||||
packages = [
 | 
			
		||||
  { name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
 | 
			
		||||
  { name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
 | 
			
		||||
  { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[requirements]
 | 
			
		||||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
 | 
			
		||||
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
 | 
			
		||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
 | 
			
		||||
							
								
								
									
										51
									
								
								2024/day01/src/day01.gleam
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								2024/day01/src/day01.gleam
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
import gleam/dict
 | 
			
		||||
import gleam/int
 | 
			
		||||
import gleam/io
 | 
			
		||||
import gleam/list
 | 
			
		||||
import gleam/pair
 | 
			
		||||
import gleam/result
 | 
			
		||||
import gleam/string
 | 
			
		||||
import gleam/yielder
 | 
			
		||||
import stdin.{stdin}
 | 
			
		||||
 | 
			
		||||
pub fn main() {
 | 
			
		||||
  let assert #(pairs, []) =
 | 
			
		||||
    stdin()
 | 
			
		||||
    |> yielder.map(fn(l) {
 | 
			
		||||
      string.trim_end(l)
 | 
			
		||||
      |> string.split_once("   ")
 | 
			
		||||
      |> result.map(fn(p) {
 | 
			
		||||
        p |> pair.map_first(int.parse) |> pair.map_second(int.parse)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
    |> yielder.to_list
 | 
			
		||||
    |> result.partition
 | 
			
		||||
  let #(a, b) = list.unzip(pairs)
 | 
			
		||||
  let assert #(a, []) = result.partition(a)
 | 
			
		||||
  let assert #(b, []) = result.partition(b)
 | 
			
		||||
 | 
			
		||||
  part1(a, b)
 | 
			
		||||
  |> int.to_string
 | 
			
		||||
  |> io.println
 | 
			
		||||
 | 
			
		||||
  part2(a, b)
 | 
			
		||||
  |> int.to_string
 | 
			
		||||
  |> io.println
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn part1(a: List(Int), b: List(Int)) -> Int {
 | 
			
		||||
  list.zip(list.sort(a, int.compare), list.sort(b, int.compare))
 | 
			
		||||
  |> list.map(fn(p) { p.0 - p.1 |> int.absolute_value })
 | 
			
		||||
  |> int.sum
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn part2(a: List(Int), b: List(Int)) -> Int {
 | 
			
		||||
  let count =
 | 
			
		||||
    list.unique(b)
 | 
			
		||||
    |> list.fold(dict.new(), fn(d, i) {
 | 
			
		||||
      dict.insert(d, i, list.count(b, fn(x) { x == i }))
 | 
			
		||||
    })
 | 
			
		||||
  list.fold(a, 0, fn(s, x) {
 | 
			
		||||
    s + x * { dict.get(count, x) |> result.unwrap(0) }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								2024/day01/src/stdin.gleam
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								2024/day01/src/stdin.gleam
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
import gleam/dynamic
 | 
			
		||||
import gleam/result
 | 
			
		||||
import gleam/yielder
 | 
			
		||||
 | 
			
		||||
@external(erlang, "io", "get_line")
 | 
			
		||||
fn ffi_read_line(prompt: String) -> dynamic.Dynamic
 | 
			
		||||
 | 
			
		||||
fn read_line() -> Result(String, Nil) {
 | 
			
		||||
  ffi_read_line("")
 | 
			
		||||
  |> dynamic.from()
 | 
			
		||||
  |> dynamic.string()
 | 
			
		||||
  |> result.replace_error(Nil)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn assert_upwrap(res: Result(a, _)) -> a {
 | 
			
		||||
  let assert Ok(a) = res
 | 
			
		||||
  a
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn stdin() -> yielder.Yielder(String) {
 | 
			
		||||
  yielder.repeatedly(read_line)
 | 
			
		||||
  |> yielder.take_while(result.is_ok)
 | 
			
		||||
  |> yielder.map(assert_upwrap)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue